pipe实例
来源:互联网 发布:淘宝帐号登录 编辑:程序博客网 时间:2024/06/09 13:34
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int fds[2];
int main(void){
if(-1 == pipe(fds)){
printf("pipe fail\r\n");
exit(0);
}
pid_t pid = fork();
if(pid == 0){
//child process
int nChild = 2;
printf("%d:child\r\n", nChild);
while(1){
char sBuffer[512] = { 0 };
int nLen = read(fds[0], sBuffer, 512);
printf("read:%d:%s\r\n", nLen, sBuffer);
}
printf("%d:exit\r\n", nChild);
}
else if(pid > 0){
//parent process
int nParent = 1;
printf("%d:parent\r\n", nParent);
int n = 3;
while(n-- > 0){
char sBuffer[10];
sprintf(sBuffer, "send my num:%d\n", n);
printf("%d:%s\n", nParent, sBuffer);
write(fds[1], sBuffer, strlen(sBuffer));
sleep(3);
}
close(fds[1]);
sleep(10);
printf("%d:exit\r\n", nParent);
}
else{
printf("fork error!\r\n");
exit(-1);
}
return 0;
}
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int fds[2];
int main(void){
if(-1 == pipe(fds)){
printf("pipe fail\r\n");
exit(0);
}
pid_t pid = fork();
if(pid == 0){
//child process
int nChild = 2;
printf("%d:child\r\n", nChild);
while(1){
char sBuffer[512] = { 0 };
int nLen = read(fds[0], sBuffer, 512);
printf("read:%d:%s\r\n", nLen, sBuffer);
}
printf("%d:exit\r\n", nChild);
}
else if(pid > 0){
//parent process
int nParent = 1;
printf("%d:parent\r\n", nParent);
int n = 3;
while(n-- > 0){
char sBuffer[10];
sprintf(sBuffer, "send my num:%d\n", n);
printf("%d:%s\n", nParent, sBuffer);
write(fds[1], sBuffer, strlen(sBuffer));
sleep(3);
}
close(fds[1]);
sleep(10);
printf("%d:exit\r\n", nParent);
}
else{
printf("fork error!\r\n");
exit(-1);
}
return 0;
}
- pipe实例
- pipe实例
- Pipe实例
- Spark pipe 实例
- Spark pipe 实例
- Spark pipe 实例
- Spark pipe 实例
- Spark pipe PHP 实例
- pipe小实例
- Linux管道pipe使用实例
- Linux下pipe通信实例
- pipe()
- pipe
- pipe
- pipe
- pipe
- PIPE
- pipe
- Python中 内置函数
- sqllite 你懂得 生成db文件
- C读取文件内容
- C#正则表达式整理备忘
- 【smarty项目源码】模拟smarty模版文件的解析过程
- pipe实例
- Eclipse Workbench Structure分析
- 如何在 Windows XP 中定位域控制器
- 深入理解Hadoop集群和网络
- Start with a Walking Skeleton
- lucene 中的TooManyClause 异常
- 数学之美 系列七 -- 信息论在信息处理中的应用
- 转一张git和svn常用命令的对比图l
- Junit入门