APUE-通过管道同步父子进程
2018-03-03 18:41
405 查看
#include "ourhdir.h" #include <sys/types.h> // pid_t // fd1[2] 父进程读,子进程写 // fd2[2] 父进程写,子进程读 static int fd1[2], fd2[2]; // TELL_WAIT函数是在未创建子进程的时候调用的 // 子进程创建之后,父子进程都有fd1 fd2 void TELL_WAIT(void) { if (pipe(fd1) < 0 || pipe(fd2) < 0) { err_sys("pipe error"); } } void TELL_PARENT(pid_t pid) { if (write(fd1[1], 'c', 1) != 1) { err_sys("son write error"); } } void TELL_CHILD(pid_t pid) { if (write(fd2[1], 'p', 1) != 1) { err_sys("parent write error"); } } // 子进程读取管道,读取不到会被阻塞,进入等待 void WAIT_PARENT(void) { char c; if (read(fd2[0], &c, 1) != 1) { err_sys("son read error"); } if (c != 'p') { err_quit("WAIT_PARENT: incorrect data"); } } // 父进程读取管道,同子进程 void WAIT_CHILD(void) { char c; if (read(fd1[0], &c, 1) != 1) { err_sys("parent read error"); } if (c != 'c') { err_quit("WAIT_CHILD: incorrect data"); } }
相关文章推荐
- UNIX环境高级编程学习之第十五章进程间通信 - 通过匿名管道实现父子进程同步
- 父子进程通过管道实现文件复制
- APUE习题10.6(父子进程同步)
- 父子进程通过管道传递数据
- linux c之通过管道父子进程实现同步通信
- C语言进程通过管道同步
- linux 下父子进程通过管道传输数据
- fork()+pipe() --> 父子进程间通过管道通信
- UNIX环境高级编程学习之第十五章进程间通信 - 通过半双工匿名管道实现父子进程通信
- 使用pipe()与fork()函数通过管道实现父子进程之间的通信
- 父子进程通过管道通讯------命名管道
- 【说解】在shell中通过mkfifo创建命名管道来控制多个进程并发执行
- C++和C#进程间通过命名管道来通信(附源码)—上
- 匿名管道实现父子进程之间的通信....
- Linux 父子进程在没有同步时出现的问题
- [Linux管道和IPC]在父子进程中使用管道
- 匿名管道实现父子进程之间的通信....
- Linux下通过共享内存进行进程间通信,进程间同步使用信号量来实现
- 进程间同步(Process Synchronization)-2 通过semaphore方式来实现进程间同步的例子
- C++和C#进程之间通过命名管道通信(附源码)—下