作业

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "myhead.h"
int main(int argc, const char *argv[])
{
	if(mkfifo("./fife",0664)<0)
	{
		if(errno!=17)
		{
			ERR_MSG("mkfifo");
			return -1;
		}
	}
	printf("make success \n");
	if(mkfifo("./sfife",0664)<0)
	{
		if(errno!=17)
		{
			ERR_MSG("mkfifo");
			return -1;
		}
	}
	printf("make success \n");

	pid_t cpid;
	cpid=fork();
	if(cpid<0)
	{
		ERR_MSG("fork");
		return -1;
	}
	if(0==cpid)
	{
		int fd=open("./fife",O_RDONLY);
		if(fd<0)
		{
			ERR_MSG("open");
			return -1;
		}
		int sfd=open("./sfife",O_WRONLY);
		if(sfd<0)
		{
			ERR_MSG("open");
			return -1;
		}
		char str[128]="0";
		while(1)
		{
			bzero(str,sizeof(str));
			int res=read(fd,str,sizeof(str));
			if(res<0)
			{
				ERR_MSG("read");
				return -1;
			}
			if(res==0)
			{
				printf("数据读取完毕\n");
				return -1;
			}
			printf("父对子:%s\n",str);

			bzero(str,sizeof(str));
			printf("子对父--->>");
			fgets(str,sizeof(str),stdin);
			str[strlen(str)-1]=0;
			if(write(sfd,str,sizeof(str))<0)
			{
				ERR_MSG("write");
				return -1;
			}
		}
	}
	else if(cpid>0)
	{

		int fd =open("./fife",O_WRONLY);
		if(fd<0)
		{
			ERR_MSG("open");
			return -1;
		}
		printf("%d\n",__LINE__);
		int sfd=open("./sfife",O_RDONLY);
		if(sfd<0)
		{
			ERR_MSG("open");
			return -1;
		}
		printf("%d\n",__LINE__);

		while(1)
		{
			char buff[128]="0";
			bzero(buff,sizeof(buff));
			printf("父对子--->>");
			fgets(buff,sizeof(buff),stdin);
			buff[strlen(buff)-1]=0;
			if(write(fd,buff,sizeof(buff))<0)
			{
				ERR_MSG("write");
				return -1;
			}
			printf("父发子成功\n");

			int res =read(sfd,buff,sizeof(buff));
			if(res<0)
			{
				ERR_MSG("read");
				return -1;
			}
			if(0==res)
			{
				printf("数据读取完毕\n");
				return -1;
			}
			printf("子对父:%s\n",buff);
		}

	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值