2022-12-14作业(1)

实现AB进程随时收发数据

代码实现如下:

fifo_r.c:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>

int main(int argc, const char *argv[])
{
	//清空umask
	umask(0);

	//创建fifo文件
	if(mkfifo("./my_fifo", 0664) < 0)
	{
		//文件已经存在导致的错误是允许的
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("create FIFO success\n");
	//创建fifo文件
	if(mkfifo("./my_fifo1", 0664) < 0)
	{
		//文件已经存在导致的错误是允许的
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("create FIFO1 success\n");

	//打开,只写
	int fd = open("./my_fifo",O_WRONLY);
	if(fd < 0)
	{
		perror("open");
		return -1;
	}
	printf("open FIFO success\n");

	//打开,只读
	int fd1 = open("./my_fifo1",O_RDONLY);
	if(fd1 < 0)
	{
		perror("open");
		return -1;
	}
	printf("open FIFO1 success\n");

	char buf[128] = "";
	ssize_t res = 0;

	pid_t pid = fork();
	if(pid > 0)
	{
		while(1)
		{
			//往管道中写入数据
			bzero(buf,sizeof(buf));    //清空字符串
			printf("请输入>>>");
			fgets(buf, sizeof(buf), stdin);   //等待终端输入
			buf[strlen(buf)-1] = 0;  

			res = write(fd,buf, sizeof(buf));
			if(res < 0)
			{
				perror("write");
				return -1;
			}
			printf("发送数据成功 res = %ld\n", res);
			if(strcmp(buf,"quit") == 0)
				break;
		}
		//关闭
		close(fd);
		wait(NULL);
	}
	else if(0 == pid)
	{
		while(1)
		{
			//从管道中读取数据
			bzero(buf,sizeof(buf));    //清空字符串
			res = read(fd1,buf,sizeof(buf));
			if(res < 0)
			{
				perror("read");
				return -1;
			}
			else if(0 == res)   //写端均关闭
			{
				fprintf(stderr,"对方进程退出\n");
				break;
			}
			printf("对方说 : %s\n", buf);
			if(strcmp(buf,"quit") == 0)
				break;
		}
		//关闭
		close(fd1);
	}
	else
	{
		perror("fork");
		return -1;
	}

	return 0;
}

fifo_w.c:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>

int main(int argc, const char *argv[])
{
	//清空umask
	umask(0);

	//创建fifo文件
	if(mkfifo("./my_fifo", 0664) < 0)
	{
		//文件已经存在导致的错误是允许的
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("create FIFO success\n");
	//创建fifo文件
	if(mkfifo("./my_fifo1", 0664) < 0)
	{
		//文件已经存在导致的错误是允许的
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("create FIFO1 success\n");

	//打开,只读
	int fd = open("./my_fifo",O_RDONLY);
	if(fd < 0)
	{
		perror("open");
		return -1;
	}
	printf("open FIFO success\n");

	char buf[128] = "";
	ssize_t res = 0;

	//打开,只写
	int fd1 = open("./my_fifo1",O_WRONLY);
	if(fd1 < 0)
	{
		perror("open");
		return -1;
	}
	printf("open FIFO1 success\n");

	pid_t pid = fork();
	if(pid > 0)
	{
		while(1)
		{
			//从管道中读取数据  
			bzero(buf,sizeof(buf));    //清空字符串
			res = read(fd,buf,sizeof(buf));	
			if(res < 0)
			{
				perror("read");
				return -1;
			}
			else if(0 == res)    //写端均关闭
			{
				fprintf(stderr,"对方进程退出\n");
				break;
			}
			printf("对方说 : %s\n", buf);
			if(strcmp(buf,"quit") == 0)
				break;
		}
		//关闭
		close(fd);
		wait(NULL);
	}
	else if(0 == pid)
	{
		while(1)
		{
			//往管道中写入数据
			bzero(buf,sizeof(buf));    //清空字符串
			printf("请输入>>>");
			fgets(buf, sizeof(buf), stdin);   //等待终端输入
			buf[strlen(buf)-1] = 0;  

			res = write(fd1,buf, sizeof(buf));
			if(res < 0)
			{
				perror("write");
				return -1;
			}
			printf("发送数据成功 res = %ld\n", res);
			if(strcmp(buf,"quit") == 0)
				break;
		}
		//关闭
		close(fd1);
	}
	else
	{
		perror("fork");
		return -1;
	}

	return 0;
}

运行结果如下:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值