pipe c 1 include unistd h 13 2

  • Slides: 27
Download presentation

pipe. c 1 #include <unistd. h> 13 2 #define MAXLINE 100 14 3 /*

pipe. c 1 #include <unistd. h> 13 2 #define MAXLINE 100 14 3 /* 파이프를 통해 자식에서 부모로 15 데이터를 보내는 프로그램 */ 4 if ((pid = fork()) == 0) { /* 자식 프로세스 */ close(fd[0]); sprintf(message, "Hello from PID getpid()); %dn", 5 int main( ) 16 length = strlen(message)+1; 6{ 17 write(fd[1], message, length); int n, length, fd[2]; 18 8 int pid; 19 close(fd[1]); 9 char message[MAXLINE], 20 n = read(fd[0], line, MAXLINE); line[MAXLINE]; 21 printf("[%d] %s", getpid(), line); 10 22 11 12 pipe(fd); /* 파이프 생성 */ } 23 24 25 } 7 } else { /* 부모 프로세스 */ 7 exit(0);

stdpipe. c 1 #include <stdio. h> 13 if ((pid = fork()) == 0) {

stdpipe. c 1 #include <stdio. h> 13 if ((pid = fork()) == 0) { 2 #include <unistd. h> 14 close(fd[0]); 3 #define MAXLINE 100 15 dup 2(fd[1], 1); 4 16 close(fd[1]); 5 /* 파이프를 통해 자식에서 실행되 는명령어 출력을 받아 프린트 */ 17 printf("Hello! pipen"); 18 printf("Bye! pipen"); 6 int main(int argc, char* argv[]) 19 7{ 20 close(fd[1]); } else { //자식 프로세스 // 부모 프로세스 8 int n, pid, fd[2]; 21 printf("자식 프로세스로부터 받은 결과n"); 9 char line[MAXLINE]; 22 while ((n = read(fd[0], line, MAXLINE))> 0) 10 11 pipe(fd); /* 파이프 생성 */ 23 12 24 write(STDOUT_FILENO, line, n); } 25 26 10 27 } exit(0);

pexec 1. c if ((pid = fork()) == 0) { //자식 프로세스 1 #include

pexec 1. c if ((pid = fork()) == 0) { //자식 프로세스 1 #include <stdio. h> 13 2 #include <unistd. h> 14 close(fd[0]); 3 #define MAXLINE 100 15 dup 2(fd[1], 1); 4 16 close(fd[1]); 5 /* 파이프를 통해 자식에서 실행되 는명령어 출력을 받아 프린트 */ 17 execvp(argv[1], &argv[1]); 6 int main(int argc, char* argv[]) 19 close(fd[1]); 7{ 20 printf("자식 프로세스로부터 받은 결과n"); 8 int n, pid, fd[2]; 9 char line[MAXLINE]; } else { // 부모 프로세스 18 21 10 22 11 pipe(fd); /* 파이프 생성 */ 23 12 24 while ((n = read(fd[0], line, MAXLINE))> 0) 25 26 } 12 write(STDOUT_FILENO, line, n); } exit(0);

shellpipe. c #include <stdio. h> #include <string. h> #include <unistd. h> #define READ 0

shellpipe. c #include <stdio. h> #include <string. h> #include <unistd. h> #define READ 0 #define WRITE 1 int main(int argc, char* argv[]) { char str[1024]; char *command 1, *command 2; int fd[2]; printf("[shell]"); fgets(str, sizeof(str), stdin); str[strlen(str)-1] =''; if(strchr(str, '|') != NULL) { // 파이프 사용하는 경우 command 1 = strtok (str, "| "); command 2 = strtok (NULL, "| "); 14 }

shellpipe. c pipe(fd); if (fork() ==0) { close(fd[READ]); dup 2(fd[WRITE], 1); // 쓰기용 파이프를

shellpipe. c pipe(fd); if (fork() ==0) { close(fd[READ]); dup 2(fd[WRITE], 1); // 쓰기용 파이프를 표준출력에 복제 close(fd[WRITE]); execlp(command 1, NULL); perror("pipe"); } else { close(fd[WRITE]); dup 2(fd[READ], 0); // 읽기용 파이프를 표준입력에 복제 close(fd[READ]); execlp(command 2, NULL); perror("pipe"); } }15

pexec 2. c #include <stdio. h> #define MAXLINE 100 /* popen() 함수를 이용해 자식에서

pexec 2. c #include <stdio. h> #define MAXLINE 100 /* popen() 함수를 이용해 자식에서 실행되는 명령어 출력을 받아 프린트 */ int main(int argc, char* argv[]) { char line[MAXLINE]; FILE *fpin; if ((fpin = popen(argv[1], "r")) == NULL) { perror("popen 오류"); return 1; } printf("자식 프로세스로부터 받은 결과n"); while (fgets(line, MAXLINE, fpin)) fputs(line, stdout); pclose(fpin); return 0; } 18

이름 있는 파이프를 만드는 방법 l p 옵션과 함께 mknod 명령어 $mknod my. Pipe

이름 있는 파이프를 만드는 방법 l p 옵션과 함께 mknod 명령어 $mknod my. Pipe p $chmod ug+rw my. Pipe $ls -l my. Pipe prw-rw-r-- 1 chang faculty 0 4월 11일 13: 03 my. Pipe l mkfifo() 시스템 호출 #include <sys/types. h> #include <sys/stat. h> int mkfifo(const char *pathname, mode_t mode); 이름 있는 파이프를 생성한다. 성공하면 0을 실패하면 -1을 리턴한다. 21

npreader. c while (read. Line(fd, str)) #include <stdio. h> printf("%s n", str); #include <sys/types.

npreader. c while (read. Line(fd, str)) #include <stdio. h> printf("%s n", str); #include <sys/types. h> #include <sys/stat. h> close(fd); #include <fcntl. h> return 0; #define MAXLINE 100 } /* 이름 있는 파이프를 통해 읽은 내 용을 프린트한다. */ int read. Line(int fd, char *str) int main( ) { { int n; int fd; do { char str[MAXLINE]; n = read(fd, str, 1); unlink("my. Pipe"); } while (n > 0 && *str++ != NULL); mkfifo("my. Pipe", 0660); fd = open("my. Pipe", O_RDONLY); 22 return (n > 0); }

npwriter. c do { #include <sys/types. h> #include <sys/stat. h> fd = open("my. Pipe",

npwriter. c do { #include <sys/types. h> #include <sys/stat. h> fd = open("my. Pipe", O_WRONLY); #include <fcntl. h> if (fd == -1) sleep(1); #define MAXLINE 100 } while (fd == -1); /* 이름 있는 파이프를 통해 메시지를 출력한다. */ for (i = 0; i <= 3; i++) { int main( ) write(fd, message, length); { sleep(3); int fd, length, i; } char message[MAXLINE]; sprintf(message, "Hello from PID %d", getpid()); length = strlen(message)+1; 23 close(fd); return 0; }

chatserver. c #include <sys/types. h> #include <sys/stat. h> #include <fcntl. h> #include <stdio. h>

chatserver. c #include <sys/types. h> #include <sys/stat. h> #include <fcntl. h> #include <stdio. h> #include <string. h> #include <stdlib. h> #include <unistd. h> #define MAXLINE 256 main() { int fd 1, fd 2, n; char msg[MAXLINE]; if (mkfifo(". /chatfifo 1", 0666) == -1) { perror("mkfifo"); exit(1); } if (mkfifo(". /chatfifo 2", 0666) == -1) { perror("mkfifo"); exit(2); 25 } fd 1 = open(". /chatfifo 1", O_WRONLY); fd 2 = open(". /chatfifo 2", O_RDONLY); if (fd 1 == -1 || fd 2 == -1) { perror("open"); exit(3); } printf("* 서버 시작 n"); while(1) { printf("[서버] : "); fgets(msg, MAXLINE, stdin); n = write(fd 1, msg, strlen(msg)+1); if (n == -1) { perror("write"); exit(1); } n = read(fd 2, msg, MAXLINE); printf("[클라이언트] -> %sn", msg);

chatclient. c if(fd 1 == -1 || fd 2 == -1) { #include <sys/types.

chatclient. c if(fd 1 == -1 || fd 2 == -1) { #include <sys/types. h> #include <sys/stat. h> perror("open"); #include <fcntl. h> exit(1); } #include <stdio. h> #include <string. h> #include <stdlib. h> printf("* 클라이언트 시작 n"); #include <unistd. h> while(1) { #define MAXLINE 256 n = read(fd 1, inmsg, MAXLINE); main() { printf("[서버] -> %sn", inmsg); int fd 1, fd 2, n; printf("[클라이언트] : "); char inmsg[MAXLINE]; fgets(inmsg, MAXLINE, stdin); fd 1 = open(". /chatfifo 1", O_RDONLY); fd 2 = open(". /chatfifo 2", O_WRONLY); 26 write(fd 2, inmsg, strlen(inmsg)+1); } }