サンプルコードです:
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void child(void)
{
int fd = 0;
if ((fd = open("./fifo.txt", O_WRONLY)) < 0) {
return;
}
write(fd, "hello world!", 12);
}
void parent(void)
{
int fd = 0;
if ((fd = open("./fifo.txt", O_RDONLY)) < 0) {
return;
}
char buf[36] = {0};
read(fd, buf, 36);
printf("%s\n", buf);
}
int main(void)
{
pid_t pid = 0;
if (mkfifo("./fifo.txt", S_IRUSR | S_IWUSR) < 0) {
printf("Can not create fifo\n");
return 1;
}
pid = fork();
if (pid == 0) {
printf("child process\n");
child();
} else if (pid < 0) {
printf("fork error\n");
return -1;
}
parent();
}
使用[straceの(1)](http://man7.org/linux/man-pages/man1/strace.1.html)あなたのプログラムに。誤って[perror(3)](http://man7.org/linux/man-pages/man3/perror.3.html)と呼んでください。 –
"他のプロセスが同じFIFOを書き込み用に開くまで、通常読み込み用のFIFOを開き、その逆も同様です。" https://linux.die.net/man/3/mkfifo – maxik