作业

#include 
#include 
#include 
#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;
}

你可能感兴趣的:(java,算法,数据结构)