无名管道(pipe)使用实例
2012-11-04 16:14
441 查看
无名管道(pipe)的创建实例,一下程序在子进程中写入数据,在父进程中读取数据
View Code
View Code
#include <unistd.h> #include <stdio.h> #include <stdlib.h> int main() { pid_t pid; int pipedes[2]; char s[14] = "test message!"; char d[14] = {0}; if(pipe(pipedes) == -1)//创建管道 { perror("pipe"); exit(1); } if((pid = fork()) == -1)//创建新进程 { perror("fork"); exit(1); } else if(pid == 0)//子进程 { printf("write data to the pipe\n"); if(write(pipedes[1], s, 14) == -1)//写数据到管道 { perror("write"); exit(1); } else { printf("the written data is %s\n", s); } } else if(pid > 0)//父进程 { sleep(2);//休眠2秒钟,让子进程先运行 printf("read data from the pipe\n"); if(read(pipedes[0], d, 14) == -1)//读取管道数据 { perror("read"); exit(1); } printf("data from pipe is %s\n", d); } return 0; }
相关文章推荐
- 无名管道pipe的使用
- Python multiprocessing模块中的Pipe管道使用实例
- 传统的进程间通信方式 1.无名管道(pipe) 2.有名管道(fifo) 3.信号(signal); 闹钟信号应用实例
- 13-(1-4)进程管道关于popen(-r-w)及pipe的程序使用实例
- 无名管道pipe的使用
- 使用无名管道pipe实现进程间通信
- 《Lua程序设计》9.2 管道(pipe)与过滤器(filter) 包含使用协同函数实现“生产者——消费者”问题的实例代码
- 实例——Linux管道pipe的使用
- 管道pipe与dup结合使用,应用实例
- 无名管道pipe的使用
- 无名管道pipe简单使用
- Linux管道pipe使用实例
- 无名管道pipe使用方法
- 使用pipe()与fork()函数通过管道实现父子进程之间的通信
- Linux 管道(pipe)原理及使用
- 程序:进程间通信——无名管道实例
- Linux内核中无名管道pipe和有名管道fifo的分析
- linux shell 管道命令(pipe)使用及与shell重定向区别
- Linux编程 进程间通信------无名管道(PIPE)
- linux shell 管道命令(pipe)使用及与shell重定向区别