파이프 두 개 생성하면 됨
int main(void) {
int fd1[2], fd2[2];
pid_t pid;
char buf[257];
int len, status;
pipe(fd1);
pipe(fd2);
switch (pid = fork()) {
case -1 :
perror("fork");
exit(1);
break;
case 0 : /* child */
close(fd1[1]);
close(fd2[0]);
//fd1로는 읽고 fd2로는 쓰기
write(1, "Child Process:", 15); //표준 출력
len = read(fd1[0], buf, 256); //읽기
write(1, buf, len); //파이프로부터 읽은 것 표준 출력
strcpy(buf, "Good\n");
write(fd2[1], buf, strlen(buf)); //fd2에 쓰기
break;
default :
close(fd1[0]);
close(fd2[1]);
//fd1로는 쓰고 fd2로는 읽기
buf[0] = '\0';
write(fd1[1], "Hello\n", 6);
write(1, "Parent Process:", 15); //표준 출력
len = read(fd2[0], buf, 256);
write(1, buf, len);
waitpid(pid, &status, 0);
break;
}
return 0;
}
파이프 두 개 생성하면 됨
int main(void) { int fd1[2], fd2[2]; pid_t pid; char buf[257]; int len, status; pipe(fd1); pipe(fd2); switch (pid = fork()) { case -1 : perror("fork"); exit(1); break; case 0 : /* child */ close(fd1[1]); close(fd2[0]); //fd1로는 읽고 fd2로는 쓰기 write(1, "Child Process:", 15); //표준 출력 len = read(fd1[0], buf, 256); //읽기 write(1, buf, len); //파이프로부터 읽은 것 표준 출력 strcpy(buf, "Good\n"); write(fd2[1], buf, strlen(buf)); //fd2에 쓰기 break; default : close(fd1[0]); close(fd2[1]); //fd1로는 쓰고 fd2로는 읽기 buf[0] = '\0'; write(fd1[1], "Hello\n", 6); write(1, "Parent Process:", 15); //표준 출력 len = read(fd2[0], buf, 256); write(1, buf, len); waitpid(pid, &status, 0); break; } return 0; }