반응형
#include <stdio.h>
#include <unistd.h>
#include <sys/times.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
int main() {
int fd1[2], fd2[2];
pid_t pid;
char buf[257];
int len, status;
if(pipe(fd1)== -1){
perror("pipe");
exit(1);
}
if(pipe(fd2)==-1){
perror("pipe");
exit(1);
}
switch(pid = fork()){
case -1 :
perror("fork");
exit(1);
break;
case 0:
close(fd1[1]);
close(fd2[0]);
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));
break;
default:
close(fd1[0]);
close(fd2[1]);
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;
}
반응형
'TIL > os' 카테고리의 다른 글
tmpnam, tempnam, mktemp 임시 파일명 생성 api (0) | 2022.10.14 |
---|---|
FIFO로 server client간 데이터 주고 받기 (0) | 2022.10.04 |
wait를 이용하여 자식 프로세스의 종료를 기다리기 (0) | 2022.10.03 |
exec를 이용하여 fork 후 자식 프로세스는 다른 프로그램 실행하기 (0) | 2022.10.03 |
자식 프로세스 fork 하기 (0) | 2022.10.03 |