利用有名管道通信实现AB进程能够随时收发数据。提示:用多进程或者多线程实现

代码:

A进程:

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

pthread_t tid1,tid2;

void *read_from_two(void *arg)
{

	//打开管道文件2,开启读端
	int fd2 = open("./myfifo2",O_RDONLY);
	if(fd2 < 0 )
	{
		perror("open");
		return NULL;
	}
	printf("open fifo2 sucess!\n");

	char buf[128] = "";
	char str[] = "quit";
	ssize_t res;
	while(1)
	{
			bzero(buf,sizeof(buf));//清除数组中读取或写入的数据
			res = read(fd2,buf,sizeof(buf));//向管道文件2读取
			if(res < 0)
			{
				perror("read");
				return NULL;
			}
			else if(res == 0)
			{
				printf("文件2写端关闭\n");
				break;
			}

			if(strcmp(str,buf) == 0)//判断是否是退出条件
			{
				printf("退出\n");
				break;
			}
			printf("读端2读取>>%s\n",buf);
	}
	close(fd2);
	pthread_cancel(tid2);
	pthread_exit(NULL);
}
void *write_in_one(void *arg)
{
	sleep(1);
	//打开管道文件1,开启写端
	int fd1 = open("./myfifo1",O_WRONLY);
	if(fd1 < 0 )
	{
		perror("open");
		return NULL;
	}
	printf("open fifo1 sucess!\n");

	char buf[128] = "";
	char str[] = "quit";
	while(1)
		{

			bzero(buf,sizeof(buf));//清除数组中读取或写入的数据
			fgets(buf,sizeof(buf),stdin);//从终端读取
            
			buf[strlen(buf)-1] = '\0';//由于fgets会读到\n结束,为了消除\n所以将其置0	
			if(write(fd1,buf,sizeof(buf)) < 0)//将数组内容写入管道文件中
			{
				perror("write");
				return NULL;
	    	}
			if(strcmp(str,buf) == 0)//判断是否是退出条件
			{
				printf("退出\n");
				break;
			}
		}
	close(fd1);
	pthread_cancel(tid1);
    pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	umask(0);
	//创建有名管道文件1
	if(mkfifo("./myfifo1",0664) < 0)
	{
		if(errno != EEXIST)
		{
           perror("open");
		   return -1;
		}
	}
    //创建管道文件2
	if(mkfifo("./myfifo2",0664) < 0)
	{
		if(errno != EEXIST)
		{
           perror("open");
		   return -1;
		}
	}
	printf("create fifo2 sucess!\n");
	printf("create fifo1 sucess!\n");

	if(pthread_create(&tid1,NULL,read_from_two,NULL) != 0)//创建读取线程
	{
		fprintf(stderr,"pthread_create failed\n");
		return -1;
	}

	if(pthread_create(&tid2,NULL,write_in_one,NULL) != 0)//创建写入线程
	{
		fprintf(stderr,"pthread_create failed\n");
		return -1;
	}

	//回收子线程
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	return 0;
}

B进程:

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


pthread_t tid1,tid2;
void *read_from_one(void *arg)//从管道文件1读取
{
	//打开管道文件1,开启读端
	int fd1 = open("./myfifo1",O_RDONLY);
    if(fd1 < 0)
	{
	     perror("open");
		 return NULL;
    }
	printf("open1 fifo1 sucess!\n");

	char buf[128] = "";
	char str[] = "quit";
	ssize_t res;
	while(1)
	{
			bzero(buf,sizeof(buf));//清除数组中读取或写入的数据
			res = read(fd1,buf,sizeof(buf));//待子进程写入后向管道文件1读取
			if(res < 0)
			{
				perror("read");
				return NULL;
			}
			else if(res == 0)
			{
				fprintf(stderr,"写端退出\n");
				break;
			}
			if(strcmp(str,buf) == 0)//判断是否是退出条件
			{
				printf("退出\n");
				break;
			}
			printf("读端1读取>>%s\n",buf);
	}
	close(fd1);
	pthread_cancel(tid2);
    pthread_exit(NULL);
}
void *write_in_two(void *arg)//在管道文件2写入
{
	sleep(1);
	//打开管道文件2,开启写端
	int fd2 = open("./myfifo2",O_WRONLY);
     if(fd2 < 0)
	 {
		 perror("open");
		 return NULL;
	 }
	printf("open fifo2 sucess!\n");

	char buf[128] = "";
	char str[] = "quit";
	while(1)
		{	
			bzero(buf,sizeof(buf));//清除数组中读取或写入的数据
			fgets(buf,sizeof(buf),stdin);//从终端读取
            
			buf[strlen(buf)-1] = '\0';//由于fgets会读到\n结束,为了消除\n所以将其置0	
       
			if(write(fd2,buf,sizeof(buf)) < 0)//将数组内容写入管道文件2中
			{
				perror("write");
				return NULL;
	    	}
			if(strcmp(str,buf) == 0)//判断是否是退出条件
			{
				printf("退出\n");
				break;
			}
         }
	close(fd2);
	pthread_cancel(tid1);
    pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	umask(0);
    //创建管道文件2
	if(mkfifo("./myfifo2",0664) < 0)
	{
		if(errno != EEXIST)
		{
           perror("open");
		   return -1;
		}
	}
	printf("create fifo2 sucess!\n");
    //创建管道文件1
	if(mkfifo("./myfifo1",0664) < 0)
	{
		if(errno != EEXIST)
		{
           perror("open");
		   return -1;
		}
	}
	printf("create fifo1 sucess!\n");

	if(pthread_create(&tid1,NULL,read_from_one,NULL) != 0)//创建读取线程
	{
		fprintf(stderr,"pthread_create failed\n");
		return -1;
	}

	if(pthread_create(&tid2,NULL,write_in_two,NULL) != 0)//创建写入线程
	{
		fprintf(stderr,"pthread_create failed\n");
		return -1;
	}

   //回收子线程
   pthread_join(tid1,NULL);
   pthread_join(tid2,NULL);
	return 0;
}

现象:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值