55555555555

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
int main(void){
int p,i,fd[2],fs[2];
int re1=0;
int re2=0;
char fd_out[50];
char fs_out[50];
char fd_in[50]=“child send to parent : Hello,parent!”;
char fs_in[50]=“parent respond to child:Received.”;
/若无名管道创建失败,提示错误信息。/
while(pipe(fd) < 0 || pipe(fs)<0)
{
perror(“Fail to create pipe!”);
exit(EXIT_FAILURE);
}
/若子进程创建失败,提示错误信息。/
while((p = fork())<0)
{
perror(“Fail to create child process!”);
exit(EXIT_FAILURE);
}
/子进程创建成功/
if(p==0)
{
while(re1<0)
{
printf(“Receive Error!”);
exit(EXIT_FAILURE);
}
/从管道fd的写口写入数据,并从fs的读口读数据/
close (fd[0]);//关闭管道fd的读口
close (fs[1]);//关闭管道fs的写口

	for(i=0;i<5;i++){
		write(fd[1],fd_in,50);
		sleep(1);
		re1=read(fs[0],fs_out,50);
		if(re1>0)
			printf("%s\n",fs_out);
		else if(re1 == 0)
			printf("First\n");
	}
	exit(EXIT_SUCCESS);
}else{
	/*从管道fs的写口写入数据,并从fd的读口读数据*/
	for(i=0;i<5;i++){
		close (fd[1]);//关闭fd的写口
		close (fs[0]);//关闭fs的读口
		re2=read(fd[0],fd_out,50);
		if(re2>0)
		{
			sleep(1);
			printf("%s\n",fd_out);
			write(fs[1],fs_in,50);
		}
	}
}
sleep(1);		
printf("------------------Program-----End-------------------\n");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

viper困告了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值