20230807

 a.A进程先发送─句话给B进程,B进程接收后打印b.B进程再回复一句话给A进程,A进程接收后打印c.重复1.2步骤,当收到quit后,要结束AB进程d.提示:两根管道

#include <head.h>

int main()
{
	int k1 = mkfifo("./fifo",0664);//创建有名管道文件fifo
	if(k1 < 0)
	{
		if(errno != 17)
		{
			perror("mkfido");
			return -1;
		}
	}
	printf("mkfifo fifo success\n");
	
	int k12 = mkfifo("./fifo1",0664);//创建有名管道文件fifo1
	if(k12 < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("mkfifo fifo1 success\n");
	
		
	int m1 = open("fifo1",O_RDONLY);//打开建立好的有名管道文件
	if(m1 < 0)
	{
		perror("open: ");
		return -1;
	}
	printf("fifo1 open read success\n");


	int k2 = open("fifo",O_WRONLY);//打开建立好的有名管道文件
	if(k2 < 0)
	{
		perror("open: ");
		return -1;
	}
	printf("fifo open write success\n");

	char buf[128] = "";
	char buf1[128] = "";
	int res;
	int res1;
	while(1)
	{
			printf("请输入==> ");
			fgets(buf,sizeof(buf),stdin);
			if(strcmp(buf,"quit") == 0)
			{
				return 0;
			}
		
			buf[strlen(buf) -1] = '\0';

			if((res = write(k2,buf,sizeof(buf))) < 0)
			{
				perror("write");
				return -1;
			}

			bzero(buf1,sizeof(buf1));
			res1 = read(m1,buf1,sizeof(buf1));
			if(strcmp(buf1,"quit") == 0)
			{
				printf("__%d__\n",__LINE__);
				return 0;
			}
		
			if(res1 < 0)
			{
				perror("read");
				return -1;
			}
			if(0 == res1)
			{
				break;
			}
			printf("%s",buf1);
	}





	return 0;
}
--------------------------------------------------
#include <head.h>

int main()
{

	int m1 = open("fifo1",O_WRONLY);
	if(m1 < 0)
	{
		perror("open");
		return -1;
	}
	printf("fifo1 open write success\n");

	int k2 = open("fifo",O_RDONLY);
	if(k2 < 0)
	{
		perror("open");
		return -1;
	}
	printf("fifo open read success\n");
	char buf[128] = "";
	char buf1[128] = "";
	int res;
	int res1;
	while(1)
	{
			bzero(buf,sizeof(buf));
			res = read(k2,buf,sizeof(buf));
			if(strcmp(buf,"quit") == 0)
			{
				printf("__%d__\n",__LINE__);
				break;
			}
		
			if(res < 0)
			{
				perror("read");
				return -1;
			}
			if(0 == res)
			{
				break;
			}
			printf("%s\n",buf);

			printf("请输入==> ");
			fgets(buf1,sizeof(buf1),stdin);
			if(strcmp(buf1,"quit") == 0)
			{
				printf("__%d__\n",__LINE__);
				break;
			}
	
			buf[strlen(buf) -1] = '\0';

			if((res1 = write(m1,buf1,sizeof(buf1))) < 0)
			{
				perror("write");
				return -1;
			}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值