linux管道通信(pipe)
来源:互联网 发布:微分方程数值算法 编辑:程序博客网 时间:2024/05/22 10:58
linux pipe适合于父子进程之间进行通信。如下面代码所示:
#include <stdio.h>#include <string.h>#include <unistd.h>int main(){ /*define pipe*/ int fd[2]; /*define process id */ pid_t pid = -1; char input[13] = "hello world\n"; char recv[13]; memset(recv,0,13); /*create pipe*/ int res = pipe(fd); if (-1 == res) { printf("create pipe failed\n"); return -1; } /*create sub-process*/ pid = fork(); if (-1 == pid) { printf("fork failed \n"); return -1; } else if (0 == pid) { close(fd[1]); //close input; read(fd[0],recv,13); printf("%s\n",recv); close(fd[0]); //close output; return 0; } else { close(fd[0]); //close output; write(fd[1],input,13); close(fd[1]); //close input } return 0;}
当调用fork函数后,fork将会返回两个值(记住记性了,是两个值)。
当返回值为 -1时,表示fork函数调用失败。
当返回值为0时, 表示子进程。
当返回值>0时,表示父进程。
下面推荐一个写的很详细的blog:http://blog.csdn.net/zqtsx/article/details/9048663
0 0
- linux管道通信(pipe)
- Linux进程通信-管道(pipe)
- Linux--进程间通信(一)-管道(pipe)通信
- Linux 管道(pipe)
- linux 线程或进程之间通过管道通信(pipe)
- linux进程间通信之管道(pipe与fifo)
- 深入理解Linux进程间通信(IPC)-- 管道pipe
- 【Linux系统编程】进程间通信--无名管道(pipe)
- Linux进程通信(一)——pipe管道
- Linux编程之pipe管道通信(1)
- Linux编程之pipe管道通信(2)
- Linux进程间通信之管道(pipe)
- Linux环境进程间通信 --- 管道Pipe
- Linux 进程间管道pipe通信
- Linux进程间通信之管道(pipe)
- Linux进程间通信之管道(pipe)
- Linux进程通信IPC--管道Pipe/Named Pipe
- Linux进程间通信(一)之无名管道(PIPE)和有名管道(FIFO)
- 黑马程序员--交通灯及银行调度业务
- BZOJ2456 mode 抵消思想
- linux awk命令详解
- 环境变量中的path、classpath、Java_home
- 二叉排序树
- linux管道通信(pipe)
- c++使用顺序结构实现栈
- DrawCall优化
- POJ 3083
- 开源HTML5游戏引擎Kiwi.js_安装使用
- 项目管理软件
- C++类中静态变量和静态方法使用介绍!
- 一种应用程序命令执行架构设计
- 蹩脚的自学笔记the swift programming language