[Linux] 由管道父进程向子进程发送数据 (父子间IPC)
2013-12-17 17:38
435 查看
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cstdlib> #include <unistd.h> using std::cin; using std::cout; using std::endl; using std::string; typedef string String; const int MAXLINE = 1005; int main() { int n; int fd[2]; pid_t pid; char line[MAXLINE]; cout << "hehe\n"; cout << fd[0] << ' ' << fd[1] << endl; if (pipe(fd) < 0) { fprintf(stderr, "iipe error"); } if ((pid = fork()) < 0) { fprintf(stderr, "fork error"); } else if(pid > 0) { // parent cout << "par : " << fd[0] << ' ' << fd[1] << endl; close(fd[0]); write(fd[1], "hello world\n", 12); } else { cout << "son: " << fd[0] << ' ' << fd[1] << endl; close(fd[1]); n = read(fd[0], line, MAXLINE); cout << "n : " << n << endl; cout << "line : " << line << endl; write(STDOUT_FILENO, line, n); } exit(0); }
相关文章推荐
- [Linux] 由管道父进程向子进程发送数据 (父子间IPC)
- [Linux管道和IPC]在父子进程中使用管道
- [Linux管道和IPC]使用信号量和共享内存进行父子进程通信
- linux中IPC管道实现进程间传输数据
- linux 下父子进程通过管道传输数据
- Linux 利用管道父子进程间传递数据
- Linux 利用管道父子进程间传递数据
- Linux下父进程通过管道发送文件名给子进程,子进程获取文件名后首先判断文件是否存在,不存在,通过管道返回错误信息,存在,将文件内容返回给父进程
- linux的IPC进程通信方式-匿名管道(一)
- 使用windowsAPI穿件匿名管道pipe,父子进程间数据传递
- linux 命名管道fifo实现进程间小数据通信
- Linux进程IPC浅析[进程间通信概述与管道]
- [Linux管道和IPC]消息队列发送客户端
- Linux(九):进程通信IPC(三)之验证管道容量
- Linux编程实践----进程间的通信IPC--管道
- linux c之管道的介绍、创建关闭和简单读写(父进程向子进程写入数据)
- [Linux管道和IPC]在兄弟进程中使用管道
- linux下父子进程间的通信——管道
- pipe,父子进程实现读写交换数据(linux)
- Linux 进程IPC之命名管道