8月5日 IO DAY 8

#include <stdio.h>
#include <head.h>

int main(int argc, const char *argv[])
{
	//创建有名管道
	if(mkfifo("./fifo", 0664) < 0)
	{
		if (errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("mififo success\n");

	

	char buf[128] = "";
	char s[128] = "";
	ssize_t res1 = 0;
	ssize_t res2 = 0;
	while (1)
	{
		int fd = open("./fifo", O_WRONLY);
		if (fd < 0)
		{
			ERR_MSG("open");
			return -1;
		}
		//printf("open success fd = %d\n", fd);
		printf("请输入》》》");
		fgets(buf, sizeof(buf), stdin);
		buf[strlen(buf) - 1] = 0;

		if (res1 = write(fd, buf, sizeof(buf)) < 0)
		{
			ERR_MSG("write");
			return -1;
		}
		printf("写入数据成功 res1 = %ld\n", res1);
		if (strcmp(buf, "quit") == 0)
		{
			break;
		}
		close(fd);

		int fs = open("./fifo", O_RDONLY);
		if (fs < 0)
		{
			ERR_MSG("open");
			return -1;
		}
		bzero(s, sizeof(s));
		res2 = read(fs, s, sizeof(s));
		if (res2 < 0)
		{
			ERR_MSG("read");
			return -1;
		}
		if (0 == res2)
		{
			printf("写端关闭\n");
			break;
		}
		printf("res2 = %ld : %s\n", res2, s);
		if (strcmp(s, "quit") == 0)
		{
			break;
		}
		close(fs);
	}
	
	
	return 0;
}



#include <stdio.h>
#include <head.h>

int main(int argc, const char *argv[])
{
	//创建有名管道
	if(mkfifo("./fifo", 0664) < 0)
	{
		if (errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("mififo success\n");

	
	//printf("open success fd = %d\n", fd);

	char buf[128] = "";
	char s[128] = "";
	ssize_t res1 = 0;
	ssize_t res2 = 0;
	while (1)
	{
		int fd = open("./fifo", O_RDONLY);
		if (fd < 0)
		{
			ERR_MSG("open");
			return -1;
		}
		bzero(buf, sizeof(buf));
		res1 = read(fd, buf, sizeof(buf));
		if (res1 < 0)
		{
			ERR_MSG("read");
			return -1;
		}
		if (0 == res1)
		{
			printf("写端关闭\n");
			break;
		}
		printf("res1 = %ld : %s\n", res1, buf);
		if (strcmp(buf, "quit") == 0)
		{
			break;
		}
		close(fd);

		int fs = open("./fifo", O_WRONLY);
		if (fs < 0)
		{
			ERR_MSG("open");
			return -1;
		}
		printf("请输入》》》");
		fgets(s, sizeof(s), stdin);
		s[strlen(s) - 1] = 0;

		if (res2 = write(fs, s, sizeof(s)) < 0)
		{
			ERR_MSG("write");
			return -1;
		}
		printf("写入数据成功 res2 = %ld\n", res2);
		
		if (strcmp(s, "quit") == 0)
		{
			break;
		}
		close(fs);
	}
	
	
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值