2023.05.11 IO进程线程 DAY8

1.创建AB进程,要求如下:

(1)A进程先发送一句话给B进程,B进程接收到后打印到终端上;

(2)在a要求之后,B进程发送一句话给A进程,A进程接收后打印。

(3)重复a, b步骤,直到发送或者接收到quit后,结束AB进程。

附加题

(4)在第1题的基础上实现,A能随时发信息给B,B能随时接收A发送的数据,反之亦然。

进程A代码如下

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

int fd1, fd2, fd3, fd4;

//接受终端输入的数据,并发送给另一个进程,当前进程A
void *callback1(void *arg)
{
	char buf[128] = "";
	while(1)
	{
		bzero(buf, sizeof(buf));
		printf("请输入>>>");
		fgets(buf, sizeof(buf), stdin);
		buf[strlen(buf)-1] = 0;
		write(fd1, buf, sizeof(buf));

		if(strcasecmp(buf, "quit") == 0)
			exit(0);
		
		bzero(buf, sizeof(buf));
		read(fd2, buf, sizeof(buf));
		printf("%s\n", buf);
	}
}

//接收另一个进程发送的数据
void *callback2(void *arg)
{
	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		bzero(buf, sizeof(buf));
		res = read(fd3, buf, sizeof(buf));
		if(res > 0)
		{
			printf("A receive:%s\n", buf);

			if(strcasecmp(buf, "quit") == 0)
				exit(0);

			bzero(buf, sizeof(buf));
			strcpy(buf, "A receive success");
			write(fd4, buf, strlen(buf));
		}
		else if(res == 0)
		{
			printf("对端退出\n");
			exit(0);
		}
	}
}

int main(int argc, const char *argv[])
{
	if(mkfifo("./fifo1", 0755) < 0)  //A->B  A给B发送数据
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./fifo2", 0755) < 0)  //B->A  A接受B成功接收信号
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}	
	if(mkfifo("./fifo3", 0755) < 0)  //B->A  A接受B发送的数据
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./fifo4", 0755) < 0)  //A->B  A向B返回成功接收信号
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	
	fd1 = open("./fifo1", O_WRONLY);
	if(fd1 < 0)
	{
		perror("open");
		return -1;
	}	
	fd2 = open("./fifo2", O_RDONLY);
	if(fd2 < 0)
	{
		perror("open");
		return -1;
	}
	fd3 = open("./fifo3", O_RDONLY);
	if(fd3 < 0)
	{
		perror("open");
		return -1;
	}

	fd4 = open("./fifo4", O_WRONLY);
	if(fd4 < 0)
	{
		perror("open");
		return -1;
	}

	printf("open fifo success A\n");
	
	pthread_t tid1, tid2;
	if(pthread_create(&tid1, NULL, callback1, NULL) != 0)
	{
		printf("pthread_create failed\n");
		return -1;
	}
	if(pthread_create(&tid2, NULL, callback2, NULL) != 0)
	{
		printf("pthread_create failed\n");
		return -1;
	}

	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);
	close(fd1);
	close(fd2);
	close(fd3);
	close(fd4);
	printf("进程A退出\n");
	return 0;
}

进程B代码如下

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

int fd1, fd2, fd3, fd4;

//接受终端输入的数据,并发送给另一个进程,当前进程B
void *callback1(void *arg)
{
	char buf[128] = "";
	while(1)
	{
		bzero(buf, sizeof(buf));
		printf("请输入>>>");
		fgets(buf, sizeof(buf), stdin);
		buf[strlen(buf)-1] = 0;
		write(fd3, buf, sizeof(buf));

		if(strcasecmp(buf, "quit") == 0)
			exit(0);
		
		bzero(buf, sizeof(buf));
		read(fd4, buf, sizeof(buf));
		printf("%s\n", buf);
	}
}

//接收另一个进程发送的数据
void *callback2(void *arg)
{
	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		bzero(buf, sizeof(buf));
		res = read(fd1, buf, sizeof(buf));
		if(res > 0)
		{
			printf("B receive:%s\n", buf);

			if(strcasecmp(buf, "quit") == 0)
				exit(0);

			bzero(buf, sizeof(buf));
			strcpy(buf, "B receive success");
			write(fd2, buf, strlen(buf));
		}
		else if(res == 0)
		{
			printf("对端退出\n");
			exit(0);
		}
	}
}

int main(int argc, const char *argv[])
{
	if(mkfifo("./fifo1", 0755) < 0)  //A->B  B接收A发送的数据
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./fifo2", 0755) < 0)  //B->A  B向A返回成功接收信号
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}	
	if(mkfifo("./fifo3", 0755) < 0)  //B->A  B向A发送数据
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	if(mkfifo("./fifo4", 0755) < 0)  //A->B  B接收A成功接收信号
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	
	fd1 = open("./fifo1", O_RDONLY);
	if(fd1 < 0)
	{
		perror("open");
		return -1;
	}	
	fd2 = open("./fifo2", O_WRONLY);
	if(fd2 < 0)
	{
		perror("open");
		return -1;
	}
	fd3 = open("./fifo3", O_WRONLY);
	if(fd3 < 0)
	{
		perror("open");
		return -1;
	}
	fd4 = open("./fifo4", O_RDONLY);
	if(fd4 < 0)
	{
		perror("open");
		return -1;
	}

	printf("open fifo success A\n");
	
	pthread_t tid1, tid2;
	if(pthread_create(&tid1, NULL, callback1, NULL) != 0)
	{
		printf("pthread_create failed\n");
		return -1;
	}
	if(pthread_create(&tid2, NULL, callback2, NULL) != 0)
	{
		printf("pthread_create failed\n");
		return -1;
	}

	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);
	close(fd1);
	close(fd2);
	close(fd3);
	close(fd4);
	printf("进程B退出\n");
	return 0;
}

运行结果

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值