IOday8PIC通信

作业

题目一

  1. 要求实现AB进程对话
    1. A进程先发送一句话给B进程,B进程接收后打印 
    2. B进程再回复一句话给A进程,A进程接收后打印
    3. 重复1.2步骤,当收到quit后,要结束AB进程
    4. 提示:两根管道

 A进程

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<head.h>
int main(int argc, const char *argv[])
{
//创建有名通道1
	if((mkfifo("./myfifo",0666) < 0) && errno != 17)
	{
		perror("mkfifo");
		return -1;
	}
	printf("mkfifo success\n");
//打开有名通道1
	int fd1 = open("./myfifo",O_RDWR);
	if(fd1 < 0)
	{
		perror("open");
		return -1;
	}
	printf("open success fd = %d\n",fd1);
//创建有名通道2
	if((mkfifo("./myfifo2",0666) < 0) && errno != 17)
	{
		perror("mkfifo");
		return -1;
	}
	printf("mkfifo success\n");
//打开有名通道2
	int fd2 = open("./myfifo2",O_RDWR);
	if(fd2 < 0)
	{
		perror("open");
		return -1;
	}
	printf("open success fd2 = %d\n",fd2);
	char buf[128] = "";
	while(1)
	{
//通道1中A传给B
		printf("请输入\n");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = '\0';
		if(write(fd1,buf,sizeof(buf))<0)
		{
			perror("write");
			return -1;
		}
//输入quit退出程序
		int re = strcmp(buf,"quit");
		if(re == 0)
		{
			printf("A进程退出\n");
			return -1;
		}
		printf("写入成功\n");
		bzero(buf,sizeof(buf));
//通道2中B传给A
		if(read(fd2,buf,sizeof(buf))<0)
		{
			perror("read");
			return -1;
		}
//接收到B的quit一起退出程序
		re = strcmp(buf,"quit");
		if(re == 0)
		{
			printf("A进程退出\n");
			return -1;
		}
		printf("读取成功\n");
		printf("%s\n",buf);
	}
	return 0;
}

B进程(代码思路和A程序相差不大)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<head.h>
int main(int argc, const char *argv[])
{
	if((mkfifo("./myfifo",0666) < 0) && errno != 17)
	{
		perror("mkfifo");
		return -1;
	}

	printf("mkfifo success\n");
	int fd1 = open("./myfifo",O_RDWR);
	if(fd1 < 0)
	{
		perror("open");
		return -1;
	}
	printf("open success fd1 = %d\n",fd1);
	if((mkfifo("./myfifo2",0666) < 0) && errno != 17)
	{
		perror("mkfifo");
		return -1;
	}

	printf("mkfifo success\n");
	int fd2 = open("./myfifo2",O_RDWR);
	if(fd2 < 0)
	{
		perror("open");
		return -1;
	}
	printf("open success fd2 = %d\n",fd2);
	char buf[128] = "";
	while(1)
	{
		bzero(buf,sizeof(buf));
		if(read(fd1,buf,sizeof(buf))<0)
		{
			perror("read");
			return -1;
		}
		int re = strcmp(buf,"quit");
		if(re == 0)
		{
			printf("B进程退出\n");
			return -1;
		}
		printf("读取成功\n");
		printf("%s\n",buf);
		printf("请输入\n");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = '\0';
		if(write(fd2,buf,sizeof(buf))<0)
		{
			perror("write");
			return -1;
		}
		re = strcmp(buf,"quit");
		if(re == 0)
		{
			printf("B进程退出\n");
			return -1;
		}
		printf("写入成功\n");
	}
	return 0;
}

题目二

在第1题的基础上实现,A能随时发信息给B,B能随时接收A发送的数据

A进程

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<head.h>
//子线程用来读取通道2
void *RD(void* arg)
{
	int *fd2 = (int *)arg;
	char buf[128] = "";
//读取通道2中B传来的信息
	while(1)
	{
		bzero(buf,sizeof(buf));
		if(read(*fd2,buf,sizeof(buf))<0)
		{
			perror("read");
			break;
		}
		int re = strcmp(buf,"quit");
//接收到B传来的quit退出程序
		if(re == 0)
		{
			printf("A进程退出\n");
			exit(1);
		}
		printf("读取成功\n");
		printf("%s\n",buf);
	}
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建通道1并打开
	if((mkfifo("./myfifo",0666) < 0) && errno != 17)
	{
		perror("mkfifo");
		return -1;
	}

	printf("mkfifo success\n");
	int fd1 = open("./myfifo",O_RDWR);
	if(fd1 < 0)
	{
		perror("open");
		return -1;
	}
	printf("open success fd = %d\n",fd1);
//创建通道2并打开
	if((mkfifo("./myfifo2",0666) < 0) && errno != 17)
	{
		perror("mkfifo");
		return -1;
	}

	printf("mkfifo success\n");
	int fd2 = open("./myfifo2",O_RDWR);
	if(fd2 < 0)
	{
		perror("open");
		return -1;
	}
	printf("open success fd2 = %d\n",fd2);
//创建子线程,并传通道2进线程
	pthread_t tid;
	if(pthread_create(&tid,NULL,RD,(void*)&fd1)<0)
	{
		perror("create");
		return -1;
	}
	char buf[128] = "";
//用通道1实现A传给B
	while(1)
	{
		printf("请输入\n");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = '\0';
		if(write(fd2,buf,sizeof(buf))<0)
		{
			perror("write");
			break;
		}
		int re = strcmp(buf,"quit");
//写quit退出程序
		if(re == 0)
		{
			printf("A进程退出\n");
			exit(1);
		}
		printf("写入成功\n");
	}
	pthread_join(tid,NULL);
	return 0;
}

B进程(代码思路和A相差不大)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<head.h>
void *RD(void* arg)
{
	int *fd2 = (int *)arg;
	char buf[128] = "";
	while(1)
	{
		bzero(buf,sizeof(buf));
		if(read(*fd2,buf,sizeof(buf))<0)
		{
			perror("read");
			break;
		}
		int re = strcmp(buf,"quit");
		if(re == 0)
		{
			printf("B进程退出\n");
			exit(1);
		}
		printf("读取成功\n");
		printf("%s\n",buf);
	}
	pthread_exit(NULL);
}

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

	printf("mkfifo success\n");
	int fd1 = open("./myfifo",O_RDWR);
	if(fd1 < 0)
	{
		perror("open");
		return -1;
	}
	printf("open success fd1 = %d\n",fd1);
	if((mkfifo("./myfifo2",0666) < 0) && errno != 17)
	{
		perror("mkfifo");
		return -1;
	}

	printf("mkfifo success\n");
	int fd2 = open("./myfifo2",O_RDWR);
	if(fd2 < 0)
	{
		perror("open");
		return -1;
	}
	pthread_t tid;
	if(pthread_create(&tid,NULL,RD,(void*)&fd2)<0)
	{
		perror("create");
		return -1;
	}
	char buf[128] = "";
	while(1)
	{
		printf("请输入\n");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = '\0';
		if(write(fd1,buf,sizeof(buf))<0)
		{
			perror("write");
			break;
		}
		int re = strcmp(buf,"quit");
		if(re == 0)
		{
			printf("B进程退出\n");
			exit(1);
		}
		printf("写入成功\n");
	}

	pthread_join(tid,NULL);
	printf("open success fd2 = %d\n",fd2);
	return 0;
}

思维导图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值