Bash変態文法最速マスター? 改め bashのプロセス置換 改め 名前付きパイプ
こちら
FIFO(名前付きパイプ) - Hayrodge Library
を参考にして、WAIT()関数の位置を修正したら期待通りに
http://www.hayrodge.jp/?front=treedata%2Fc_lang%2F%E3%83%97%E3%83%AD%E3%82%BB%E3%82%B9%E9%96%93%E9%80%9A%E4%BF%A1%2F%E2%91%A4FIFO%28%E5%90%8D%E5%89%8D%E4%BB%98%E3%81%8D%E3%83%91%E3%82%A4%E3%83%97%29&sub1=c_lang_sub&navi=C&node=1
[hirasawa@centos-hira ~]$ cat mkmyfifo.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/wait.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> void errOutput(char *s){ char errbuf[256] = {0}; sprintf(errbuf, "%s[%d]", s, errno); perror(errbuf); return ; } int main(void) { int fd; ssize_t size; char buf[BUFSIZ]; /* 8192byte */ char *msg; if(mkfifo("myfifo", 0666) == -1){ errOutput("mkfifo"); exit(1); } fd = open("myfifo", O_RDONLY | O_NONBLOCK); if(fd == -1){ errOutput("open"); exit(1); } msg = "hello, thankyou"; if(fork() == 0){ fd = open("myfifo", O_WRONLY | O_NONBLOCK); if(fd == -1){ errOutput("open"); exit(1); } size = write(fd, msg, strlen(msg)); printf("wrote bytes %d\n", size); close(fd); exit(1); } //sleep(2); wait(NULL); size = read(fd, buf, BUFSIZ); printf(">%*s\n", size, buf); /* 最小幅 size 桁で表示 */ //wait(NULL); close(fd); /* FIFO削除 */ if(unlink("myfifo") == -1){ errOutput("open"); } return 0; } [hirasawa@centos-hira ~]$ ./mkmyfifo wrote bytes 15 >hello, thankyou [hirasawa@centos-hira ~]$