2つの兄弟プロセスであるChild1からChild3に "Hi"という文字列を送信しようとしています。コードは実行されますが、私はChild3のChild1からの入力を受け取りません。名前付きパイプを使用して2つのプロセス間で文字列を送信する
#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