IO 作业 day7 6/29

使用多进程完成管道全双工通信

#include <myhead.h>


//寫入線程

void *task1(void *arg)
{
	
		//定義文件描述符
		int fd1;
	
		//打開管道文件
	
		if((fd1=open("./myfifo1",O_WRONLY))==-1)
		{
			perror("open error"); 
			return NULL;
		}

		while(1)
		{
			
			//定義容器
			char str[128]="";
			//獲取數據
  			fgets(str,sizeof(str),stdin);
			//把\n置爲\0
			str[strlen(str)-1]='\0';
			//將數據寫入管道
			write(fd1,str,strlen(str));
			printf("發送的數據爲:%s\n",str);
			if(strcmp(str,"quit")==0)
			{
				break;
			}
		

		}
		
		//關閉管道
  		close(fd1);
     	pthread_exit(NULL);
	
}

void *task2(void *arg)
{
 		//定義文件描述符
	
		int fd2;

		//打開管道文件
	
		if((fd2=open("./myfifo2",O_RDONLY))==-1)
		{
			perror("open error");
			return NULL;
		}
		printf("asdds");

		while(1) 
		{
			
				//定義容器
			char str[128]="";
			//讀取數據
			read(fd2,str,sizeof(str));
			printf("接收的數據爲: %s\n",str);
			if(strcmp(str,"quit")==0)
			{
				break;
			}
	
		}

		//關閉管道
		close(fd2);
		pthread_exit(NULL);
 
}


int main(int argc, const char *argv[])
{
	//定義線程號
	pthread_t tid1,tid2;
	//創建線程
	if(pthread_create(&tid1,NULL,task1,NULL))
	{
		perror("create tid1 error");
		return -1;;
	}
	//創建線程
	if(pthread_create(&tid2,NULL,task2,NULL))
	{
		perror("create tid2 error");
		return -1;
	}

	//初始化信號量
	//sem_init(&sem,0,0);
	
  ///回收線程資源
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	


	return 0;
}
#include <myhead.h>

//寫入線程

void *task1(void *arg)
{
	
		//定義文件描述符
		int fd;
	
		//打開管道文件
	
		if((fd=open("./myfifo2",O_WRONLY))==-1)
		{
			perror("open error");
			return NULL;
		}

		while(1)
		{
			
	
			//定義容器
			char str[128]="";
			//獲取數據
  			fgets(str,sizeof(str),stdin);
			//把\n置爲\0
			str[strlen(str)-1]='\0';
			//將數據寫入管道
			write(fd,str,strlen(str));
			printf("發送的數據爲:%s\n",str);
			if(strcmp(str,"quit")==0)
			{
				break;
			}

		}

			//關閉管道
  		close(fd);
	pthread_exit(NULL);

}

void *task2(void *arg)
{
 		//定義文件描述符
	
		int fd;

		//打開管道文件
	
		if((fd=open("./myfifo1",O_RDONLY))==-1)
		{
			perror("open error");
			return NULL;
		}

		while(1)
		{
				//定義容器
			char str[128]="";
			//讀取數據
			read(fd,str,sizeof(str));
			printf("接收的數據爲: %s\n",str);
			if(strcmp(str,"quit")==0)
			{
				break;
			}
		}
	

		//關閉管道
		close(fd);
	pthread_exit(NULL);

}


int main(int argc, const char *argv[])
{
	//定義線程號
	pthread_t tid1,tid2;
	//創建線程
	if(pthread_create(&tid1,NULL,task1,NULL))
	{
		perror("create tid1 error");
		return -1;;
	}
	//創建線程
	if(pthread_create(&tid2,NULL,task2,NULL))
	{
		perror("create tid2 error");
		return -1;;
	}

	
	//回收線程資源
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);

	
	return 0;
}
#include <myhead.h>

int main(int argc, const char *argv[])
{
	//創建管道文件
	if(mkfifo("./myfifo1",0664)==-1)
	{
		perror("mkfifo1 error");
		return -1;
	}

	//創建管道文件
	if(mkfifo("./myfifo2",0664)==-1)
	{
		perror("mkfifo2 error");
		return -1;
	}
	//使用函數阻塞主函數
	getchar();

	//刪除管道文件
	
	system("rm myfifo1");
	system("rm myfifo2");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值