实现AB进程同时收发数据

需求:

A进程发送一句话给B线程,B进程打印

B进程发送一句话给A线程,A进程打印,做到同时收发

代码实现过程:

进程A(fifoW.c):

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

#define MSG_ERR(msg){\
	fprintf(stderr,"line : %d\n",__LINE__);\
	perror("msg");\
}

char str[32]="";
ssize_t res = 0;
char cmp[32]="quit";
int fd;
int fd1;

void* funcW(void *arg){

	while(1){

		bzero(str,sizeof(str));
		//printf("请输入pipe1:");
		fgets(str,sizeof(str),stdin);
		str[strlen(str)-1]='\0';
		if(strcmp(str,cmp)==0){
			break;
		}
		if(write(fd,str,sizeof(str))<0){
			perror("write");
			return NULL;
		}
	}

}

void* funcR(void *arg){
	
	while(1){
	
		bzero(str,sizeof(str));
		res = read(fd1,str,sizeof(str));
		if(strcmp(str,cmp)==0){
			break;
		}
		if(res<0){
			perror("read");
			return NULL;
		}
		else if(res == 0){
			printf("pipe2写端关闭,管道无数据\n");
			break;
		}
		printf("pipe2 : %s\n",str);
	}

}

int main(int argc, const char *argv[])
{

	umask(0);
	if(mkfifo("./myfifo1",0664)< 0){
		//printf("%d\n",errno);
		if(errno != 17){
			perror("mkfifo1");
			return -1;
		}
	}
	printf("pipe1 create success\n");

	if(mkfifo("./myfifo2",0664)< 0){
		//printf("%d\n",errno);
		if(errno != 17){
			perror("mkfifo2");
			return -1;
		}
	}
	printf("pipe2 create success\n");

	fd = open("./myfifo1",O_WRONLY); //只读或只写阻塞,以读写模式打开不阻塞;添加O_NONBLOCK以何种形式打开都则不阻塞
	fd1 = open("./myfifo2",O_RDONLY);
	
	pthread_t tid_w;
	if(pthread_create(&tid_w,NULL,funcW,NULL)<0){
		MSG_ERR(pthread_creat);
		return -1;
	}

	pthread_t tid_r;
	if(pthread_create(&tid_r,NULL,funcR,NULL)<0){
		MSG_ERR(phthread_create);
		return -1;
	}
	pthread_detach(tid_r);
	
	pthread_join(tid_w,NULL);

	close(fd);
	close(fd1);
	return 0;
}

进程B(fifoR.c):

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

#define MSG_ERR(msg){\
	fprintf(stderr,"line : %d\n",__LINE__);\
	perror("msg");\
}

char str[32]="";
ssize_t res = 0;
char cmp[32]="quit";
int fd;
int fd1;

void* funcR(void *arg){

	while(1){

		bzero(str,sizeof(str));
		res = read(fd,str,sizeof(str));
		if(strcmp(str,cmp)==0){
			break;
		}
		if(res <0){
			perror("read");
			return NULL;
		}
		else if(res == 0){
			printf("pipe1写端关闭,管道无数据\n");
			break;
		}
		printf("pipe1 :  %s\n",str);
	}

}

void* funcW(void *arg){

	while(1){

		bzero(str,sizeof(str));
		//printf("请输入pipe2:");
		fgets(str,sizeof(str),stdin);
		str[strlen(str)-1]='\0';
		if(strcmp(str,cmp)==0){
			break;
		}
		if(write(fd1,str,sizeof(str))<0){
			perror("write");
			return NULL;
		}
	}

}

int main(int argc, const char *argv[])
{

	umask(0);
	if(mkfifo("./myfifo2",0664)< 0){
		//printf("%d\n",errno);
		if(errno != 17){
			perror("mkfifo2");
			return -1;
		}
	}
	printf("pipe2 create success\n");

	if(mkfifo("./myfifo1",0664)< 0){
		//printf("%d\n",errno);
		if(errno != 17){
			perror("mkfifo1");
			return -1;
		}
	}
	printf("pipe1 create success\n");

	fd = open("./myfifo1",O_RDONLY); //只读或只写阻塞,以读写模式打开不阻塞;添加O_NONBLOCK以何种形式打开都则不阻塞
	fd1 = open("./myfifo2",O_WRONLY);

	pthread_t tid_r;
	if(pthread_create(&tid_r,NULL,funcR,NULL)<0){
		MSG_ERR(pthread_create);
		return -1;
	}
	pthread_detach(tid_r);

	pthread_t tid_w;
	if(pthread_create(&tid_w,NULL,funcW,NULL)<0){
		MSG_ERR(phthread_create);
		return -1;
	}

	pthread_join(tid_w,NULL);

	close(fd);	
	close(fd1);
	return 0;
}

代码实现结果:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值