work 1/8

创建

#include<myhead.h>
int main(int argc, const char *argv[])
{
	if(mkfifo("./myfifo1",0664)!=0)
	{
		perror("mkfifo1 error");
		return -1;
	}





	if(mkfifo("./myfifo2",0664)!=0)
	{
		perror("mkfifo2 error");
		return -1;
	}

	printf("myfifo1 myfifo2 create sueecss\n");

	getchar();

	system("rm myfifo1");
	system("rm myfifo2");
	return 0;

}

a

#include<myhead.h>
int main(int argc, const char *argv[])
{
	pid_t pid1;
	pid1=fork();
	if(pid1>0)
	{
	//管道1
	int wfd=-1;
	wfd=open("./myfifo1",O_WRONLY);
	if(wfd==-1)
	{
		perror("open error");
		return -1;
	}

	printf("写端打开成功\n");

	char wbuf[128]="";
	while(1)
	{
		bzero(wbuf,sizeof(wbuf));

		printf("请输入>>>>");
		fflush(stdout);
		read(0,wbuf,sizeof(wbuf));
		wbuf[strlen(wbuf)-1]='\0';
		
		write(wfd,wbuf,sizeof(wbuf));

		if(strcmp(wbuf,"quit")==0)
		{
			break;
		}
	}
	printf("myfifo1 create sueecss\n");
	close(wfd);

	}
	else if(pid1==0)
	{
//管道2
	int rfd1=-1;
	rfd1=open("./myfifo2",O_RDONLY);
	if(rfd1==-1)
	{
		perror("open error");
		return -1;
	}

	//printf("读端打开成功\n");

	char rbuf1[128]="";
	while(1)
	{
		bzero(rbuf1,sizeof(rbuf1));

		read(rfd1,rbuf1,sizeof(rbuf1));

		printf("收到消息:%s\n",rbuf1);
		if(strcmp(rbuf1,"quit")==0||rbuf1==0)
		{
			break;
		}
	}	
	close(rfd1);

	}
	wait(NULL);

	return 0;
}

b

#include<myhead.h>
int main(int argc, const char *argv[])
{
	pid_t pid;
	pid=fork();
	if(pid>0)
	{
	//管道1
	int rfd=-1;
	rfd=open("./myfifo1",O_RDONLY);
	if(rfd==-1)
	{
		perror("open error");
		return -1;
	}

	//printf("读端打开成功\n");

	char rbuf[128]="";
	while(1)
	{
		bzero(rbuf,sizeof(rbuf));

		read(rfd,rbuf,sizeof(rbuf));

		printf("收到消息:%s\n",rbuf);
		if(strcmp(rbuf,"quit")==0||rbuf==0)
		{
			break;
		}
	}
	close(rfd);

	}
	else if(pid==0)
	{
	//管道2
	int wfd1=-1;
	wfd1=open("./myfifo2",O_WRONLY);
	if(wfd1==-1)
	{
		perror("open error");
		return -1;
	}

	printf("写端打开成功\n");

	char wbuf1[128]="";
	while(1)
	{
		bzero(wbuf1,sizeof(wbuf1));

		printf("请输入>>>>");
		fflush(stdout);
		read(0,wbuf1,sizeof(wbuf1));
		wbuf1[strlen(wbuf1)-1]='\0';
		
		write(wfd1,wbuf1,sizeof(wbuf1));

		if(strcmp(wbuf1,"quit")==0)
		{
			break;
		}
	}
	close(wfd1);

	}
	wait(NULL);

	return 0;
}

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值