IO进程线程 day8 work

1.创建AB进程,要求如下:

a. A进程先发送一句话给B进程,B进程接收到后打印到终端上;

b.在a要求之后,B进程发送一句话给A进程,A进程接收后打印;

c.重复a, b步骤,直到发送或者接收到quit后,结束AB进程。

ubuntu@ubuntu:~/32/exercise$ cat a.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc, const char *argv[])
{	
	char buf[128]="";
	ssize_t res = 0;

	while(1)
	{	
		int fd = open("./myfifo",O_WRONLY);
		if(fd<0)
		{
			perror("open");
			return -1;
		}

		printf("please input>>>");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;

		if(0==strcasecmp(buf,"quit"))
			break;

		if(write(fd,buf,sizeof(buf))<0)
		{
			perror("write");
			return -1;
		}
		close(fd);

		int fd1 = open("./myfifo",O_RDONLY);
		if(fd1<0)
		{
			perror("open");
			return -1;
		}

		bzero(buf,sizeof(buf));
		res = read(fd,buf,sizeof(buf));


		if(res<0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res)
		{
			printf("对端关闭\n");
			break;
		}
		printf("%s\n",buf);	

		if(0==strcasecmp(buf,"quit"))
			break;

		close(fd1);
	}
	return 0;
}


ubuntu@ubuntu:~/32/exercise$ cat b.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc, const char *argv[])
{
	char buf[128]="";
	ssize_t res = 0;

	while(1)
	{	
		int fd = open("./myfifo",O_RDONLY);
		if(fd<0)
		{
			perror("open");
			return -1;
		}

		bzero(buf,sizeof(buf));
		res = read(fd,buf,sizeof(buf));

		if(res<0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res)
		{
			printf("对端关闭\n");
			break;
		}
		printf("%s\n",buf);

		if(0==strcasecmp(buf,"quit"))
			break;

		close(fd);

		int fd1 = open("./myfifo",O_WRONLY);	
		if(fd1<0)
		{
			perror("open");
			return -1;
		}

		printf("please input>>>");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;	

		if(0==strcasecmp(buf,"quit"))
			break;

		if(write(fd,buf,sizeof(buf))<0)
		{
			perror("write");
			return -1;
		}

		close(fd1);

	}
	return 0;
}

 2.在第1题的基础上实现,A能随时发信息给B,B能随时接收A发送的数据,反之亦然。

ubuntu@ubuntu:~/32/work/io/day8$ cat AcommunicationB.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<pthread.h>
#include<errno.h>
pthread_t tid1,tid2;
char buf[128]="";

void* callBack_w(void* arg)
{
	int fd = open("./A",O_WRONLY);
	if(fd<0)
	{
		perror("open");
		return NULL;
	}

	while(1)
	{
	//	printf("please input:");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;

        if(0==strcasecmp(buf,"quit"))
			break;

		if(write(fd,buf,sizeof(buf))<0)
		{
			perror("write");
			return NULL;
		}
	}
	close(fd);
	pthread_exit(NULL);
}
void* callBack_r(void* arg)
{
	int fd = open("./B",O_RDONLY);

	ssize_t res=0;
	while(1)
	{
		bzero(buf,sizeof(buf));
		res = read(fd,buf,sizeof(buf));
		if(res<0)
		{
			perror("read");
			return NULL;
		}
		else if(0 == res)
        {
			printf("对端关闭\n");
            break;
        }
	//	printf("receive:%s\n",buf);
        printf("%s\n",buf);

        if(0==strcasecmp(buf,"quit"))
			break;

	}
	close(fd);
	pthread_exit(NULL);

}

int main(int argc, const char *argv[])
{
	umask(0);
	if(mkfifo("./A",0775)<0)
	{
		if(errno!=17)
		{
		perror("mkfifo");
		return -1;
		}
	}
	if(mkfifo("./B",0775)<0)
	{
		if(errno!=17)
		{
		perror("mkfifo");
		return -1;
		}
	}

	pthread_create(&tid1,NULL,callBack_w,NULL);
	pthread_create(&tid2,NULL,callBack_r,NULL);

	pthread_join(tid2,NULL);
	return 0;
}


ubuntu@ubuntu:~/32/work/io/day8$ cat BcommunicationA.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<pthread.h>
#include<errno.h>
pthread_t tid1,tid2;
char buf[128]="";

void* callBack_r(void* arg)
{	
	int fd = open("./A",O_RDONLY);
	while(1)
	{
		bzero(buf,sizeof(buf));

		ssize_t res=0;
		res = read(fd,buf,sizeof(buf));
		if(res<0)
		{
			perror("read");
			return NULL;
		}
		else if(0 == res)
        {
			printf("对端关闭\n");
            break;
        }
	//	printf("receive:%s\n",buf);
		printf("%s\n",buf);

        if(0==strcasecmp(buf,"quit"))
			break;

	}
	close(fd);
	pthread_exit(NULL);

}
void* callBack_w(void* arg)
{

	int fd = open("./B",O_WRONLY);
	while(1)
	{
	//	printf("please input:");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;

        if(0==strcasecmp(buf,"quit"))
			break;

		if(write(fd,buf,sizeof(buf))<0)
		{
			perror("write");
			return NULL;
		}

	}
	close(fd);
	pthread_exit(NULL);

}

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

	pthread_create(&tid1,NULL,callBack_w,NULL);
	pthread_create(&tid2,NULL,callBack_r,NULL);

	pthread_join(tid2,NULL);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值