A B 进程通过有名管道通信

创建 AB 进程,实现 AB 进程的通话。
1 A 进程先发送一句话给 B 进程, B 进程接收打印。
2 B 进程发送与句话给 A 进程, A 进程接收后打印。
3 )重复 1 2 )步骤即可。
4 )当 A 进程或者 B 进程发送 quit 后, AB 进程均要结束。

  实现随时收发

A进程

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
//A写入
pthread_t R,W;
void * callback_W(void* arg)
{ 
		 int fd1=open("./fifo1",O_WRONLY);
		 if(fd1<0)
		 {
            perror("open");
				 return NULL;
		 }
		 char buf[128]="";
		 char arr[10]="quit";
         while(1)
		 {
		 bzero(buf,sizeof(buf));
		 printf("请输入A进程向B进程接发送的话:\n");
		 fgets(buf,sizeof(buf),stdin);
		 buf[strlen(buf)-1]='\0';
		 if(write(fd1,buf,sizeof(buf))<0)
		 {
			 perror("write");
		 }
		 if(strcmp(buf,arr)==0)
		 {
			 close(fd1);
			 break;
		 }	
		 }
		 pthread_cancel(R);
		 pthread_exit(NULL);
}

//A读取
void * callback_R(void* arg)
{
		 int fd2=open("./fifo2",O_RDONLY);
		 if(fd2<0)
		 {
            perror("open");
			return NULL;
		 }
		 char buf[128]="";
		 char arr[10]="quit";
		 while(1)
		 {
		 bzero(buf,sizeof(buf));
		 ssize_t res;
		 res=read(fd2,buf,sizeof(buf));
		 if(res<0)
		 {
			 perror("read"); 
		 }
		 else if(res==0)
		 {
			 break;
		 }
		 printf("A进程接收到的话:%s\n",buf); 
		 if(strcmp(buf,arr)==0)
		 {
			 close(fd2);
			 break;
		 }
		 }
		 pthread_cancel(W);
		 pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	  
	     umask(0);
		 if(mkfifo("./fifo1",0777)<0)
		 {
			 if(17!=errno)
			 {
				 perror("mkfifo");
				 return -1;
			 }

		 }
		 printf("mkfio success");
		 
		 if(mkfifo("./fifo2",0777)<0)
		 {
			 if(17!=errno)
			 {
				 perror("mkfifo");
				 return -1;
			 }

		 }
		 printf("mkfio success");

		 //线程创建
		 if(pthread_create(&W,NULL,callback_W,NULL)!=0)
		 {
			 perror("pthread_create");
			  return -1;
		 }
		 if(pthread_create(&R,NULL,callback_R,NULL)!=0)
		 {
			 perror("pthread_create");
			  return -1;
		 }	
		 pthread_join(W,NULL);
		 pthread_join(R,NULL);
	  
	return 0;
}

B进程

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pthread.h>
//B接受
pthread_t W,R;
void * callback_R(void *arg)
{
	int fd1=open("./fifo1",O_RDONLY);
	if(fd1<0)
	{
		perror("open");
	}
	    while(1)
		{
	    char buf[128]="";
	    char arr[10]="quit";
		bzero(buf,sizeof(buf));
		ssize_t res;
		res=read(fd1,buf,sizeof(buf));
		if(res<0)
		{
			perror("read"); 
			return NULL;
		}
		else if(res==0)
		{
			break;
		}
		printf("B进程接收到的话:%s\n",buf); 
		if(strcmp(buf,arr)==0)
		{
		   close(fd1);
			break;
		}
		}
		pthread_cancel(W);
		pthread_exit(NULL);

}
B写入
void * callback_W(void *arg)
{
	int fd2=open("./fifo2",O_WRONLY);
	if(fd2<0)
	{
		perror("open");
			return NULL;
	}
	    while(1)
		{
	    char buf[128]="";
	    char arr[10]="quit";
		bzero(buf,sizeof(buf));
		printf("请输入B进程向A进程接发送的话:\n");	
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]='\0';
		if(write(fd2,buf,sizeof(buf))<0)
		{
			perror("write");
			return NULL;
		}
		if(strcmp(buf,arr)==0)
		{ 
			close(fd2);
			break;
		}
		}
		pthread_cancel(R);
		pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{

	umask(0);
	if(mkfifo("./fifo1",0777)<0)
	{
		if(17!=errno)
		{
			perror("mkfifo");
			return -1;
		}

	}
	printf("mkfio success");

	if(mkfifo("./fifo2",0777)<0)
	{
		if(17!=errno)
		{
			perror("mkfifo");
			return -1;
		}

	}
	printf("mkfio success");
	if(pthread_create(&W,NULL,callback_W,NULL)!=0)
	{
		perror("pthread_create");
			return -1;
	}
	if(pthread_create(&R,NULL,callback_R,NULL)!=0)
	{
		perror("pthread_create");
			return -1;
	}
	pthread_join(W,NULL);
	pthread_join(R,NULL);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值