要求实现AB进程对话A进程先发送一句话给子进程,B进程接收后打印B进程再回复一句话给父进程,A进程接收后打印重复1.2步骤,当收到quit后,要结束AB进程

A进程代码:

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


pthread_t tid1;
pthread_t tid2;


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


//分支线程从向管道2中读取数据
void *callback1(void *arg)
{
	while(1)
	{
		bzero(buf1,sizeof(buf1));
		res = read(*(int*)arg,buf1,sizeof(buf1));
		if(res < 0)
		{
			perror("read");
			return NULL;
		}
		else if(res == 0)
		{
			fprintf(stderr,"对方进程退出\n");
			break;
		} 
		if(strcmp(buf1,"quit") == 0)
		{
			printf("退出\n");
			break;
		}
		printf("成功接收数据 buf1=%s\n",buf1);
	}
	pthread_cancel(tid2);      //终止线程2
	pthread_exit(NULL);
}

void *callback2(void* arg)
{
	while(1)
	{
		//主线程向管道中写入数据
		bzero(buf,sizeof(buf));
		printf("请输入>>>");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;

		res = write(*(int *)arg,buf,sizeof(buf));
		if(res < 0)
		{
			perror("write"); 
			return NULL;
		}
		printf("成功发送数据 res=%ld\n",res);
		if(strcmp(buf,"quit") == 0)
		{
			printf("退出\n");
			break;
		}
	}
	pthread_cancel(tid1);     //终止线程1
	pthread_exit(NULL);
}



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

	//创建fifo1文件
	if(mkfifo("./myfifo1",0664) < 0)
	{
		if(errno != EEXIST)     //EEXIST=17
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("create FIFO success\n");
	//创建fifo2文件
	if(mkfifo("./myfifo2",0664) < 0)
	{
		if(errno != EEXIST)     //EEXIST=17
		{
			perror("mkfifo");
			return -1;
		}
	}
    printf("create myofifo1 sucess\n");
	//以写的方式打开有名管道1文件
	int fd = open("./myfifo1",O_WRONLY);      //阻塞 
	if(fd <0)
	{
		perror("open");
		return -1;
	}
    printf("open myofifo1 sucess\n");
	//以读的方式打开有名管道2文件1
	int fd1 = open("./myfifo2",O_RDONLY);      //阻塞 
	if(fd <0)
	{
		perror("open");
		return -1;
	}
	printf("open myofifo2 success\n");

	//创建2个分支线程
	if(pthread_create(&tid1,NULL,callback1,&fd1) !=0)
	{
		fprintf(stderr,"pthread_create failed\n");
		return -1;
	}
	if(pthread_create(&tid2,NULL,callback2,&fd) !=0)
	{
		fprintf(stderr,"pthread_create failed\n");
		return -1;
	}



	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	//关闭有名管道文件
	close(fd);
	close(fd1);
	return 0;
}

B进程代码

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

pthread_t tid1;
pthread_t tid2;
char buf[128] = "";
char buf1[128] = "";
ssize_t res = 0;

void *callback1(void *arg)
{
	while(1)
	{
		//向管道2中写入数据
		bzero(buf1,sizeof(buf1));
		printf("请输入>>>");
		fgets(buf1,sizeof(buf1),stdin);
		buf1[strlen(buf1)-1] = 0;

		res = write(*(int*)arg,buf1,sizeof(buf1));
		if(res < 0)
		{
			perror("write");
			return 	NULL;
		}
		if(strcmp(buf1,"quit") == 0)
		{
			printf("退出\n");
			break;
		}
		printf("成功发送数据 res=%ld\n",res);
	}
	pthread_cancel(tid2);
	pthread_exit(NULL);
}

void *callback2(void *arg)
{
	while(1)
	{
		//分支线程2从向管道中读取数据
		bzero(buf,sizeof(buf));
		res = read(*(int*)arg,buf,sizeof(buf));
		if(res < 0)
		{
			perror("read");
			return NULL;
		}
		else if(res == 0)
		{
			fprintf(stderr,"对方进程退出\n");
			break;
		}
		if(strcmp(buf,"quit") == 0)
		{
			printf("退出\n");
			break;
		}
		pthread_cancel(tid1);
		printf("成功接收数据 buf=%s\n",buf);
	}
	pthread_exit(NULL);
}

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

	//创建fifo1文件
	if(mkfifo("./myfifo1",0664) < 0)
	{
		if(errno != EEXIST)     //EEXIST=17
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("create FIFO1 success\n");
	//创建fifo2文件
	if(mkfifo("./myfifo2",0664) < 0)
	{
		if(errno != EEXIST)     //EEXIST=17
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("create FIFO2 success\n");

	//以只读的方式打开有名管道文件1            //阻塞
	int fd = open("./myfifo1",O_RDONLY);
	if(fd <0)
	{
		perror("open");
		return -1;
	}
	printf("open FIFO1 success\n");
	//以只写的方式打开有名管道文件2           //阻塞
	int fd1 = open("./myfifo2",O_WRONLY);
	if(fd1 <0)
	{
		perror("open");
		return -1;
	}
	printf("open FIFO2 success\n");

	//创建2个分支线程
	if(pthread_create(&tid1,NULL,callback1,&fd1))
	{
		fprintf(stderr,"pthread_create failed\n");       //写进程
		return -1;
	}
	pthread_t tid2;
	if(pthread_create(&tid2,NULL,callback2,&fd))
	{
		fprintf(stderr,"pthread_create failed\n");       //读进程
		return -1;
	}



	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	//关闭有名管道文件
	close(fd);
	close(fd1);
	return 0;
}

运行结果:

​​​​​​​

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值