IO day8(实时交互FIFO)

A能随时发信息给B,B能随时接收A发送的数据,反之亦然。

 A端

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

void* bad1(void * arg)
{
	char arr[128]="";
	ssize_t res=0;

	umask(0);
	if(mkfifo("./FIFO1",0777));
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return NULL;
		}
	}
	printf("mkfifo success\n");

	int fd=open("./FIFO1",O_WRONLY);
	while(1)
	{
		if(fd<0)
		{
			perror("open");
			return NULL;
		}
		printf("A >>>");
		fgets(arr,sizeof(arr),stdin);
		arr[strlen(arr)-1]=0;
		res=write(fd,arr,sizeof(arr));
		if(res==0||strcasecmp(arr,"quit")==0)
		{
			printf("对端退出\n");
			close(fd);
			break;
		}
		if(res<0)
		{
			perror("write");
			return NULL;
		}

	}	

	close(fd);
	return NULL;
}


void* bad2(void * arg)
{
	char arr[128]="";
	ssize_t res=0;

	umask(0);
	if(mkfifo("./FIFO2",0777));
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return NULL;
		}
	}
	printf("mkfifo success\n");

	int	fd=open("./FIFO2",O_RDONLY);
	while(1)
	{
		if(fd<0)
		{
			perror("open");
			return NULL;
		}
		bzero(arr,sizeof(arr));
		res=read(fd,arr,sizeof(arr));
		if(res<0)
		{
			perror("read");
			return NULL;
		}
		if(res==0||strcasecmp(arr,"quit")==0)
		{
			printf("对端退出\n");
			close(fd);
			break;
		}
		printf("\n");
		printf("B: %s\n",arr);
		printf("A >>>");
		fflush(stdout);
	}	

	close(fd);
	return NULL;
}


int main(int argc, const char *argv[])
{
	pthread_t tid1;
	pthread_t tid2;
	int ttid1=pthread_create(&tid1,NULL,bad1,NULL);
	int ttid2=pthread_create(&tid2,NULL,bad2,NULL);
	if(ttid1)
	{
		fprintf(stderr,"pthread_creat is flase.%d\n",__LINE__);
		return -1;
	}
	pthread_detach(tid1);

	if(ttid2)
	{
		fprintf(stderr,"pthread_creat is flase.%d\n",__LINE__);
		return -1;
	}
	pthread_join(tid2,NULL);
	wait(NULL);
	return 0;
}

B端 

 

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

void* bad1(void * arg)
{
	char arr[128]="";
	ssize_t res=0;

	umask(0);
	if(mkfifo("./FIFO1",0777));
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return NULL;
		}
	}
	printf("mkfifo success\n");
	int fd=open("./FIFO1",O_RDONLY);
	while(1)
	{


		if(fd<0)
		{
			perror("open");
			return NULL;
		}
		bzero(arr,sizeof(arr));
		res=read(fd,arr,sizeof(arr));

		if(res<0)
		{
			perror("read");
			return NULL;
		}
		if(res==0||strcasecmp(arr,"quit")==0)
		{
			printf("对端退出\n");
			close(fd);
			break;
		}
		printf("\n");
		printf("A: %s\n",arr);
		printf("B >>>");
		fflush(stdout);


	}	

	close(fd);
	return NULL;
}


void* bad2(void * arg)
{
	char arr[128]="";
	ssize_t res=0;
	umask(0);
	if(mkfifo("./FIFO2",0777));
	{
		if(errno!=17)
		{
			perror("mkfifo");
			return NULL;
		}
	}
	printf("mkfifo success\n");

	int fd=open("./FIFO2",O_WRONLY);
	while(1)
	{

		if(fd<0)
		{
			perror("open");
			return NULL;
		}
		printf("B >>>");
		fgets(arr,sizeof(arr),stdin);
		arr[strlen(arr)-1]=0;
		res=write(fd,arr,sizeof(arr));
		if(res==0||strcasecmp(arr,"quit")==0)
		{
			printf("对端退出\n");
			close(fd);
			break;
		}
		if(res<0)
		{
			perror("write");
			return NULL;
		}
	}	

	close(fd);
	return NULL;
}


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

	pthread_t tid1;
	pthread_t tid2;
	int ttid1=pthread_create(&tid1,NULL,bad1,NULL);
	int ttid2=pthread_create(&tid2,NULL,bad2,NULL);
	if(ttid1)
	{
		fprintf(stderr,"pthread_creat is flase.%d\n",__LINE__);
		return -1;
	}
	pthread_detach(tid1);

	if(ttid2)
	{
		fprintf(stderr,"pthread_creat is flase.%d\n",__LINE__);
		return -1;
	}
	pthread_join(tid2,NULL);
	wait(NULL);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值