实现AB进程的自由对话

1、作业要求

 

2、实现过程

1)A进程

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

void* pthread_c1(void *arg);
void* pthread_c2(void *arg);

char str[100] = "";
//char str2[100] = "";

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

	//创建fifo_1文件
	if(mkfifo("./myfifo_1",0664)<0){
		if(errno!=EEXIST){ 		//EEXIST <==> 17
			perror("mkfifo");
			return -1;
		}
	}

	//创建fifo_2文件
	if(mkfifo("./myfifo_2",0664)<0){
		if(errno!=EEXIST){ 		//EEXIST <==> 17
			perror("mkfifo");
			return -1;
		}
	}

	//printf("成功创建fifo文件\n");

	//以只读的方式打开文件
	int fd1 = open("./myfifo_1",O_RDONLY);
	if(fd1<0){
		perror("open");
		return -1;
	}

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

	//创建A线程,接受数据
	pthread_t tid1,tid2;
	int res1 = pthread_create(&tid1,NULL,pthread_c1,&fd1);
	if(res1!=0){
		fprintf(stderr,"A线程创建失败\n");
		return -1;
	}

	//创建B线程,发送数据
	int res2 = pthread_create(&tid2,NULL,pthread_c2,&fd2);
	if(res2!=0){
		fprintf(stderr,"B线程创建失败\n");
		return -1;
	}

	//等待分支线程退出
	pthread_join(tid2,NULL);

	//当终端输入quit后,B线程自动退出,然后请求A线程退出
	pthread_cancel(tid1); 
	pthread_join(tid1,NULL);

	//关闭文件
	close(fd1);
	close(fd2);

	return 0;
}


//A线程,接收数据
void* pthread_c1(void *arg){
	int fd1 = *(int*)arg;
	ssize_t res1 = 0;

	while(1){
		bzero(str,sizeof(str));
		res1 = read(fd1,str,sizeof(str));
		if(res1<0){
			perror("read");
			return NULL;
		}
		if(strcmp(str,"quit")==0){
			printf("对方已退出\n");
			break;
		}
		printf("对方说>>> %s\n",str);
		//printf("请输入");
	}

	pthread_exit(NULL);
}


//B线程,发送数据
void* pthread_c2(void *arg){
	int fd2 = *(int*)arg;
	ssize_t res2 = 0;

	while(1){
		bzero(str,sizeof(str));
		//printf("请输入>>>");

		fgets(str,sizeof(str),stdin);
		str[strlen(str)-1] = '\0';
	
		res2 = write(fd2,str,sizeof(str));
	
		if(res2<0){
			perror("write");
			return NULL;
		}

		if(strcmp(str,"quit")==0){
			printf("自己退出\n");
			break;
		}
	}

	pthread_exit(NULL);
}

2)B进程

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

void* pthread_c1(void *arg);
void* pthread_c2(void *arg);

char str[100] = "";
//char str2[100] = "";

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

	//创建fifo_1文件
	if(mkfifo("./myfifo_1",0664)<0){
		if(errno!=EEXIST){ 		//EEXIST <==> 17
			perror("mkfifo");
			return -1;
		}
	}
	//创建fifo_2文件
	if(mkfifo("./myfifo_2",0664)<0){
		if(errno!=EEXIST){ 		//EEXIST <==> 17
			perror("mkfifo");
			return -1;
		}
	}

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

	//以只读的方式打开文件
	int fd2 = open("./myfifo_2",O_RDONLY);
	if(fd2<0){
		perror("open");
		return -1;
	}

	//创建A线程,接受数据
	pthread_t tid1,tid2;
	int res1 = pthread_create(&tid1,NULL,pthread_c1,&fd1);
	if(res1!=0){
		fprintf(stderr,"A线程创建失败\n");
		return -1;
	}

	//创建B线程,发送数据
	int res2 = pthread_create(&tid2,NULL,pthread_c2,&fd2);
	if(res2!=0){
		fprintf(stderr,"B线程创建失败\n");
		return -1;
	}

	//等待分支线程退出
	pthread_join(tid1,NULL);

	//当终端输入quit后,A线程自动退出,然后请求B线程退出
	pthread_cancel(tid2); 
	pthread_join(tid2,NULL);

	//关闭文件
	close(fd1);
	close(fd2);

	return 0;
}


//A线程,发送数据
void* pthread_c1(void *arg){
	int fd1 = *(int*)arg;
	ssize_t res1 = 0;

	while(1){
		bzero(str,sizeof(str));
		//printf("请输入>>>");

		fgets(str,sizeof(str),stdin);
		str[strlen(str)-1] = '\0';

		res1 = write(fd1,str,sizeof(str));
		if(res1<0){
			perror("write");
			return NULL;
		}

		if(strcmp(str,"quit")==0){
			printf("自己退出\n");
			break;
		}
	}
}


//B线程,接受数据
void* pthread_c2(void *arg){
	int fd2 = *(int*)arg;
	ssize_t res2 = 0;

	while(1){
		bzero(str,sizeof(str));
		res2 = read(fd2,str,sizeof(str));
		if(res2<0){
			perror("read");
			return NULL;
		}
		if(strcmp(str,"quit")==0){
			printf("对方已退出\n");
			break;
		}
		printf("对方说>>> %s\n",str);
		//printf("请输入");
	}
}

3、实现效果

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值