进程间管道通信基础
来源:互联网 发布:网络经过路由器变慢了 编辑:程序博客网 时间:2024/05/22 02:21
练习:使用管道实现兄弟进程间通信。 兄:ls 弟: wc -l 父:等待回收子进程。
要求,使用“循环创建N个子进程”模型创建兄弟进程,使用循环因子i标示。注意管道读写行为
#include <stdio.h>#include <sys/types.h>#include <sys/wait.h>#include <errno.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>int main(){ int ret,i,num =2; int fd[2]; pid_t pid,w_pid; int status; if ( -1 == (ret = pipe(fd) )) { perror("pipe faile"); exit(1); } for(i = 1; i < 3; i++) { pid = fork(); if(0 == pid) break; }//兄 写,弟 读 if( 0 == pid ) {//兄 if(1 == i) { close(fd[0]); dup2(fd[1],STDOUT_FILENO);//作为管道输入 execlp("ls","ls",NULL); }//弟 if( 2 == i) { close(fd[1]); dup2(fd[0],STDIN_FILENO); execlp("wc","wc",NULL); } } else {#if 0 close(fd[0]); close(fd[1]); for(i=0; i<2; i++) wait(NULL);#endif#if 1//轮循方式回收子进程 do { close(fd[0]); close(fd[1]); w_pid = waitpid(-1,NULL,WNOHANG); if(w_pid > 0) num--; }while(num>0); printf("finished\n");#endif } return 0;}
0 0
- 进程间管道通信基础
- 进程间管道通信
- 进程间通信: 管道
- 进程间通信--管道
- 进程间通信----管道
- 进程间通信--管道
- 进程间通信--管道
- 进程间通信----管道
- 进程间通信----管道
- 进程间通信--管道
- 进程间通信--管道
- 进程间通信--管道
- 进程间通信-管道
- 进程间管道通信
- 进程间管道通信
- 进程间通信--管道
- 进程间通信--管道
- 进程间通信--管道
- excel下载
- JAVA_Cookie删除问题
- 自动生成主键32UUID
- linux lnmp源码安装
- jq 更新图片验证码
- 进程间管道通信基础
- git 提交失败git error Another git process seems to be running in this repository
- Android 带checkbox的listView 实现多选,全选,反选
- 网卡 搭建新通道 DM9000芯片 ARP协议实现
- WinScp几个极大提高开发效率的小功能
- Open Inventor 简介
- ORACLE在存储过程中调用dbms_包的权限问题
- 新版PF使用说明文档
- cocos code ide 运行时模拟器的选择