【10/17】

将上述练习修改成:AB进程能够随时收发形式

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


#define N 128

void* callBack_Write(void*arg)
{
	int fd_w = open("./myfifo",O_WRONLY);
	if(fd_w < 0)
	{
		perror("open");
		pthread_exit(NULL);
	}

	char str[N] = "";

	while(1)
	{
		bzero(str,sizeof(str));

		fgets(str,sizeof(str),stdin);
		str[strlen(str)-1] = 0;

		if(write(fd_w, str, sizeof(str)) < 0)
		{
			perror("write");
			 break;
		}
		if(strncasecmp(str, "quit", 4) == 0)
		{
			break;
		}


	}

	close(fd_w);
	pthread_exit(NULL);

}

void* callBack_Read(void* arg)
{
	int fd_r = open("./myfifo1", O_RDONLY);
	if(fd_r < 0)
	{
		perror("open");
		pthread_exit(NULL);
	}

	ssize_t res = 0;
	char str[N] = "";


	while(1)
	{
		bzero(str, sizeof(str));
		res = read(fd_r, str, sizeof(str));
		if(res < 0)
		{
			perror("read");
			break;
		}
		else if(0 == res)
		{
			printf("对方退出\n");
			break;
		}
		printf("对方说: %s\n", str);
		if(strncasecmp(str, "quit", 4) == 0)
		{
			exit(0);
		}


	}

	close(fd_r);
	pthread_exit(NULL);

}
int main(int argc, const char *argv[])
{
	umask(0);

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

	}
	printf("创建成功\n");

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

	}
	printf("创建成功\n");


	pthread_t tid_r,tid_w;

	if(pthread_create(&tid_w, NULL, callBack_Write, NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}

	if(pthread_create(&tid_r, NULL, callBack_Read, NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}

	pthread_join(tid_w, NULL);
	pthread_join(tid_r, NULL);



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


#define N 128

void* callBack_Write(void* arg)
{
	int fd_w = open("./myfifo1", O_WRONLY);
	if(fd_w < 0)
	{
		perror("open");
		pthread_exit(NULL);
	}

	char str[N] = "";
	while(1)
	{
		bzero(str,sizeof(str));

		fgets(str,sizeof(str),stdin);
		str[strlen(str)-1] = 0;

		if(write(fd_w, str, sizeof(str)) < 0)
		{
			perror("write");
			break;
		}
		if(strncasecmp(str, "quit", 4) == 0)
		{
			break;
		}

	
	}

	close(fd_w);
	pthread_exit(NULL);
}

void* callBack_Read(void* arg)
{
	int fd_r = open("./myfifo",O_RDONLY);
	if(fd_r < 0)
	{
		perror("open");
		pthread_exit(NULL);
	}


	ssize_t res = 0;
	char str[N] = "";

	while(1)
	{
		bzero(str, sizeof(str));
		res = read(fd_r, str, sizeof(str));
		if(res < 0)
		{
			perror("read");
			break;
		}
		else if(0 == res)
		{
			printf("对方退出\n");
			break;
		}
		printf("对方说: %s\n", str);
		if(strncasecmp(str, "quit", 4) == 0)
		{
			exit(0);
		}

	
	
	}

	close(fd_r);
	pthread_exit(NULL);

}

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

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

	}
	printf("创建成功\n");

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

	}
	printf("创建成功\n");

    pthread_t tid_w, tid_r;

	if(pthread_create(&tid_w, NULL, callBack_Write, NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}

	if(pthread_create(&tid_r, NULL, callBack_Read, NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}

	pthread_join(tid_w, NULL);
	pthread_join(tid_r, NULL);


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值