[Linux管道和IPC]在父子进程中使用管道
2017-11-18 19:06
274 查看
#include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <stdlib.h> #include <errno.h> int main(int argc,char *argv[]) { int n, fd[2]; pid_t pid; char buffer[25]; //缓冲区 if(pipe(fd)<0) //创建一个管道,两个文件描述符在fd数组中 { printf("创建管道失败!\n "); exit(0); } if((pid=fork())<0) //创建一个子进程 { printf("创建子进程失败!\n "); exit(0); } else if (pid>0) //父进程 { close(fd[0]); write(fd[1],"This is a pipe test!\n",22); //向管道写入数据,注意回车换行符 } else //子进程 { close(fd[1]); //关闭 n = read(fd[0],buffer,25); //从通道中读出数据 printf("%s",buffer); //将数据写到标准输出设备 } exit(0); }
相关文章推荐
- [Linux管道和IPC]使用信号量和共享内存进行父子进程通信
- [Linux管道和IPC]在兄弟进程中使用管道
- [Linux] 由管道父进程向子进程发送数据 (父子间IPC)
- linux进程通信之使用匿名管道进行父子进程通信
- [Linux] 由管道父进程向子进程发送数据 (父子间IPC)
- 使用pipe()与fork()函数通过管道实现父子进程之间的通信
- linux中IPC管道实现进程间传输数据
- Linux间进程通信-----之使用命名管道
- Linux中fork的使用(03)---使用if...else...区分父子进程特有的内容
- linux的IPC进程通信方式-匿名管道(一)
- Linux C——使用父子进程实现TCP通信
- Linux下的IPC-命名管道的使用
- Linux进程IPC浅析[进程间通信概述与管道]
- Linux下的IPC-命名管道的使用
- Linux(七):进程通信IPC(一)之简单的匿名管道编写
- Linux c==使用父子进程实现TCP通信
- Linux下的有名管道(06)---使用两个管道实现两个进程之间的通信(手机模式)
- 神奇的linux:父子进程管道通讯
- Linux编程实践----进程间的通信IPC--管道
- 使用windowsAPI穿件匿名管道pipe,父子进程间数据传递