进程间的通讯-无名管道例子

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>

/*无名管道应用例子*/


/*定义*/
int temp;
void handle();

void handle(){
	temp=0;
}

main(){
	int pro1;
	int fileDesc[2];
	/*保存从管道读出来的数据*/
	char outPipe[50];
	/*向管道写进去的数据*/
	char inPipe[50]="welcome to hr!";

	/*创建无名管道*/
	pipe(fileDesc);

	/*创建子进程*/
	while((pro1=fork())==-1);
	if(pro1==0){
		//子进程写进数据
		write(fileDesc[1],inPipe,50);
		printf("i am child, i have written some data\n");
		exit(0);
	}else{
		//父进程读数据
		read(fileDesc[0],outPipe,50);
		printf("%s\n",outPipe);
		printf("i am father, i have read some data\n");
		exit(0);
	}
	
	
}
 

猜你喜欢

转载自macrotea.iteye.com/blog/788131