Pipe 简单的例子
来源:互联网 发布:dota2起源2多核优化 编辑:程序博客网 时间:2024/05/19 09:42
如下是Pipe的一个简单的例子。管道的通信是单项的。只能一端写,一端读。管道通信只能在有共同祖先的两个进程之间。在两个进程中,进行数据传送。pipe(fd)创建管道,fd【0】作为read端,fd【1】作为写入端。当fd[1]不存在的时候,认为已经读到结尾,read返回0.如果fd[1]存在,写入数据后,将写入端关闭,但是数据一直存在,直到读出。数据总是写在管道的尾部,而读的时候,则是从头部开始读取。#include #include #include #include #include int fd[2]; int main() {pid_t pid; char buffer[PIPE_BUF]; memset(buffer, 0, PIPE_BUF); if(pipe(fd) < 0) {printf("Failed to create a pipe!!\n");return 0;}if (0 == (pid = fork())) {printf("sub pid = %d\n", pid);close(fd[1]);// close the write pipesleep(1);// has time for write if (read(fd[0], buffer, 32) > 0){ printf("receive data from server, %s\n!!",buffer);}close(fd[0]);} else {printf("master pid = %d\n", pid);close(fd[0]);// close the write pipeif (-1 != write(fd[1], "Hello World", strlen("Hello World"))) { // write data to pipeprintf("send data to cline hello world!!!\n");}close(fd[1]);// close writewaitpid(pid, NULL, 0);}return 1;}
1 0
- Pipe 简单的例子
- linux pipe 管道简单例子
- PIPE函数的例子
- 一个简单的使用Pipe和thread的例子
- 同一个进程使用pipe(管道)的例子
- linux c之pipe的使用例子
- 一个FIFO pipe的简单Java实现
- POJ 1039 Pipe 简单的叉乘
- Angular2 中管道@Pipe的简单用法
- 父进程与子进程communicate..利用PIPE的例子
- Creates a pipe for reading and writing的例子程序
- 使用匿名管道pipe例子
- linux操作系统编程——简单的pipe管道
- 简单的Timer例子
- c的简单例子
- AJAX的简单例子
- 一个简单的例子
- 重载的例子.....简单
- 求解二维数组的最大/最小元素
- JAVA常用数学函数
- POJ 2689 Prime Distance 素数筛选法应用
- BlueTooth: 蓝牙协议栈
- 最全的ORACLE-SQL笔记
- Pipe 简单的例子
- 初识Hash
- 二叉树性质盘点
- java 栈(stack)与堆(heap)
- 事件ID:7026(“下列引导或系统启动驱动程序无法加载: cdrom”)的解决方法
- 测试数据集
- EAS BOS 单据列表界面排序的代码实现
- 百度URL解码关键字
- sencha touch 实现键盘对list的操作