해바

Lab #14 본문

System Programming

Lab #14

Bacha 2019. 11. 18. 13:42

FIFO를 이용하여 다음과 같은 프로그램을 작성하라.

  • Fifo-serv 프로그램에서는 "myfifo"라는 이름의 FIFO를 생성한다.
  • Fifo-clnt 프로그램에서는 stdin으로부터 한번에 최대 1024byte 크기의 데이터를 읽어들인 후, 데이터를 "myfifo"에 쓴다.
  • Fifo-serv 에서 "myfifo"에 쓰여진 데이터를 읽어서 stdout에 출력한다.
  • mkfifo, open, close, read, write 등의 system call을 사용하면 작성할 수 있다.
  • 테스트 입력으로 다음을 사용한다 :
        This is junior-level
        System Programming course
        Fall semester of year 2019

 

 

// Fifo-serv.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

#define BUFSIZE 1024

int main() {
    int fd;
    char buf[BUFSIZE];
    char* myfifo = "/tmp/myfifo";

    mkfifo(myfifo, 0666);
    while (1) {
        if((fd = open(myfifo, O_RDONLY)) == -1) perror("open");
        memset(buf, '\0', BUFSIZE);
        read(fd, buf, BUFSIZE);
        write(STDOUT_FILENO, buf, strlen(buf));
        close(fd);
    }
    
    return 0;
}

 

// Fifo-clnt.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

#define BUFSIZE 1024

int main() {
    int fd;
    char buf[BUFSIZE];
    char* myfifo = "/tmp/myfifo";

    mkfifo(myfifo, 0666);
    while(1) {
        if((fd = open(myfifo, O_WRONLY)) == -1) perror("open");
        fgets(buf, BUFSIZE, stdin);
        write(fd, buf, strlen(buf));
        close(fd);
    }

    return 0;
}

 

 

컴파일 :

%gcc Fifo-serv.c -o serv

%gcc Fifo-clnt.c -o clnt

 

실행 :

Terminal 1

%./serv

 

Terminal 2

%./clnt

 

client에서 입력 테스트 실행

정상출력 확인 후 종료(ex. Ctrl + C)

'System Programming' 카테고리의 다른 글

Lab #15  (0) 2019.12.02
Lab #13  (0) 2019.11.18
Lab #12  (0) 2019.11.18
Lab #11  (0) 2019.11.18
Lab #10  (0) 2019.11.18
Comments