IO进程线程作业-洪庆乐

1)要求AB进程做通信

  1. A进程发送一句话,B进程接收打印

  2. 然后B进程发送给A进程一句话,A进程接收打印

  3. 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;

2)在第一题的基础上实现,AB进程能够随时收发数据(附加题)

第一题:

A进程: 

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


int main(int argc, const char *argv[])
{
	if(mkfifo("./fifo",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	if(mkfifo("./fifo1",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	//以只写的方式打开
	int fd = open("./fifo",O_WRONLY);
	if(fd < 0)
	{
		perror("open");
		return -1;
	}

	int fd1 = open("./fifo1",O_RDONLY);
	if(fd < 0)
	{
		perror("open");
		return -1;
	}

	char str[128];
	ssize_t res = 0;
	while(1)
	{
		bzero(str, sizeof(str));
		printf("A进程说>>>");

		//从标准输入读取数据,其实就是从终端获取数据
		fgets(str, sizeof(str), stdin);
		str[strlen(str)-1] = '\0'; 	//将获取到的字符串最后一个字节的\n修改成\0

		//向管道中写入数据
		if(write(fd, str, sizeof(str)) < 0)
		{
			perror("write");
			return -1;
		}
		//忽略大小写的比较
		if(strcasecmp(str, "quit") == 0)
			break;

		//从管道中读取数据
		bzero(str, sizeof(str));

		//当管道中没有数据的时候,read函数阻塞
		res = read(fd1, str, sizeof(str));
		if(res < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res)
		{
			printf("写端关闭\n");
			break;
		}
		printf("A进程接收到B进程说:%s\n", str);

		if(strcasecmp(str, "quit") == 0)
			break;
	}

	//阻塞等待子进程退出
	wait(NULL);
	close(fd);
	close(fd1);

	return 0;
}

B进程:

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


int main(int argc, const char *argv[])
{
	if(mkfifo("./fifo",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	if(mkfifo("./fifo1",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	//以只写的方式打开
	int fd = open("./fifo",O_RDONLY);
	if(fd < 0)
	{
		perror("open");
		return -1;
	}

	int fd1 = open("./fifo1",O_WRONLY);
	if(fd < 0)
	{
		perror("open");
		return -1;
	}

	char str[128];
	ssize_t res = 0;
	while(1)
	{
		//从管道中读取数据
		bzero(str, sizeof(str));

		//当管道中没有数据的时候,read函数阻塞
		res = read(fd, str, sizeof(str));
		if(res < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res)
		{
			printf("写端关闭\n");
			break;
		}
		printf("B进程接收到A进程说:%s\n", str);

		if(strcasecmp(str, "quit") == 0)
			break;

		bzero(str, sizeof(str));
		printf("B进程说>>>");

		//从标准输入读取数据,其实就是从终端获取数据
		fgets(str, sizeof(str), stdin);
		str[strlen(str)-1] = '\0'; 	//将获取到的字符串最后一个字节的\n修改成\0

		//向管道中写入数据
		if(write(fd1, str, sizeof(str)) < 0)
		{
			perror("write");
			return -1;
		}
		//忽略大小写的比较
		if(strcasecmp(str, "quit") == 0)
			break;

	}

	//阻塞等待子进程退出
	wait(NULL);
	close(fd);
	close(fd1);

	return 0;
}

第二题(实现QQ类型):

A进程:

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


int main(int argc, const char *argv[])
{
	if(mkfifo("./fifo",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	if(mkfifo("./fifo1",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	
	int pid;
	int pidw;
	int pidr;
	ssize_t res = 0;
	char arr[128] = {0};
	char brr[128] = {0};

	pid = fork();

	if(pid > 0)
	{
		pidw = open("./fifo1",O_WRONLY);
		if(pidw < 0)
		{
			perror("open");
			return -1;
		}
		while(1)
		{
			bzero(arr, sizeof(arr));
			scanf("%s",arr);
			write(pidw,arr,sizeof(arr));

			if(strcmp(arr,"quit") == 0)
			{
				break;
			}
		}
		close(pidw);
        wait(NULL);
	}

	if(pid == 0)
	{
		pidr = open("./fifo",O_RDONLY);
		if(pidr < 0)
		{
			perror("open");
			return -1;
		}
		while(1)
		{
			bzero(brr,sizeof(brr));
			res = read(pidr,brr,sizeof(brr));
			if(res < 0)
			{
				perror("read");
				return -1;
			}
			else if(0 == res)
			{
				printf("写端关闭\n");
				break;
			}
			printf("A进程接收到B进程说:%s\n", brr);

			if(strcmp(brr,"quit") == 0)
			{
				break;
			}	
		}
		close(pidr);
	}
    return 0;
}

B进程:

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


int main(int argc, const char *argv[])
{
	if(mkfifo("./fifo",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	if(mkfifo("./fifo1",0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	
	int pid;
	int pidw;
	int pidr;
	ssize_t res = 0;
	char arr[128] = {0};
	char brr[128] = {0};

	pid = fork();

	if(pid == 0)
	{
		pidw = open("./fifo",O_WRONLY);
		if(pidr < 0)
		{
			perror("open");
			return -1;
		}
		while(1)
		{
			bzero(arr, sizeof(arr));
			scanf("%s",arr);
			write(pidw,arr,sizeof(arr));

			if(strcmp(arr,"quit") == 0)
			{
				break;
			}
		}
		close(pidw);
	}

	if(pid > 0)
	{
		pidr = open("./fifo1",O_RDONLY);
		if(pidr < 0)
		{
			perror("open");
			return -1;
		}
		while(1)
		{
			bzero(brr,sizeof(brr));
			res = read(pidr,brr,sizeof(brr));
			if(res < 0)
			{
				perror("read");
				return -1;
			}
			else if(0 == res)
			{
				printf("写端关闭\n");
				break;
			}
			printf("B进程接收到A进程说:%s\n", brr);

			if(strcmp(brr,"quit") == 0)
			{
				break;
			}	
		}
		close(pidr);
        wait(NULL);
	}
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值