두 개의 형제 프로세스 인 Child1에서 Child3으로 문자열 "Hi"를 보내려고합니다. 코드는 실행되지만 Child3의 Child1에서 입력을받지 못합니다.명명 된 파이프를 사용하여 두 프로세스간에 문자열 보내기
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>
#define MSGSIZE 1024
int main (int argc, char *argv[]){
int fd;
char * myfifo = "/desktop/myfifo";
char l[MSGSIZE];
pid_t child1, child3;
mkfifo(myfifo, 0666);
child1 = fork();
if (child1 == 0) {
printf("I am Child 1: %d \n", (int)getpid());
fd = open(myfifo, O_WRONLY);
write(fd, "Hi", MSGSIZE);
close(fd);
}
else {
if (child1 > 0) {
printf("I am parent: %d \n", (int)getpid());
wait(0);
}
child3 = fork();
if (child3 == 0) {
printf("I am Child 3: %d \n", (int)getpid());
fd = open(myfifo, O_RDONLY);
read(fd, l, MSGSIZE);
printf("Received: %s \n", l);
close(fd);
}
}
wait(0);
unlink(myfifo);
return 0;
}
누군가가 올바른 방향으로 나를 가리킬 수 있기를 바랍니다.
당신의 파이프가 제대로 만들어 졌는지 확인하지 않았다고 생각하십니까? 항상 시스템 콜을 확인하십시오! 오류는 무시할 수 없습니다. – Stargateur