作业IO6.0

#include 
#include 
#include 
#include 
int main(int argc, const char *argv[])
{
	//定义进程号
	pid_t pid;
	//创建一个有名管道
	pid=fork();
	if(pid>0)
	{
		//父进程
		char rbuf[128]="";
		int fid2=-1;
		if((fid2=open("./myfifo_b",O_WRONLY)) == -1)
		{
			perror("myfifo_a error");
			return -1;
		}
		while(1)
		{
			bzero(rbuf,sizeof(rbuf)); //清零
			printf("请输入:");
			fflush(stdout);
			//获取数据
			read(0,rbuf,sizeof(rbuf));
			rbuf[strlen(rbuf)-1]='\0';
			//将数据写入管道文件
			write(fid2,rbuf,sizeof(rbuf));
			//判断
			if(strcmp(rbuf,"quit")==0)
			{
				break;
			}
		}
	}
	else if(pid == 0)
	{
		//子进程
		char wbuf[128]="";
		int fid_s=-1;
		if((fid_s=open("./myfifo_a",O_RDONLY)) == -1)
		{
			perror("myfifo_b error");
			return -1;
		}
		while(1)
		{
			bzero(wbuf,sizeof(wbuf)); //清零
			//获取数据
			read(fid_s,wbuf,sizeof(wbuf));
			//判断
			if(strcmp(wbuf,"quit")==0)
			{
				break;
			}
			
			printf("接收到的数据为:%s\n",wbuf);
		}
		exit(EXIT_SUCCESS);
	}
	else
	{
		perror("fork error");
		return -1;
	}
	wait(NULL);
	
	return 0;
}

#include 
#include 
#include 
#include 
int main(int argc, const char *argv[])
{
	//定义进程号
	pid_t pid;
	//创建一个有名管道
	pid=fork();
	if(pid>0)
	{
		//父进程
		char rbuf[128]="";
		int fid1=-1;
		if((fid1=open("./myfifo_a",O_WRONLY)) == -1)
		{
			perror("myfifo_a error");
			return -1;
		}
		while(1)
		{
			bzero(rbuf,sizeof(rbuf)); //清零
			printf("请输入:");
			fflush(stdout);
			//获取数据
			read(0,rbuf,sizeof(rbuf));
			rbuf[strlen(rbuf)-1]='\0';
			//将数据写入管道文件
			write(fid1,rbuf,sizeof(rbuf));
			//判断
			if(strcmp(rbuf,"quit")==0)
			{
				break;
			}
		}
	}
	else if(pid == 0)
	{
		//子进程
		char wbuf[128]="";
		int fid_s=-1;
		if((fid_s=open("./myfifo_b",O_RDONLY)) == -1)
		{
			perror("myfifo_b error");
			return -1;
		}
		while(1)
		{
			bzero(wbuf,sizeof(wbuf)); //清零
			//获取数据
			read(fid_s,wbuf,sizeof(wbuf));
			//判断
			if(strcmp(wbuf,"quit")==0)
			{
				break;
			}
			
			printf("接收到的数据为:%s\n",wbuf);
		}
		exit(EXIT_SUCCESS);
	}
	else
	{
		perror("fork error");
		return -1;
	}
	wait(NULL);
	
	return 0;
}

作业IO6.0_第1张图片作业IO6.0_第2张图片

你可能感兴趣的:(java,linux,算法)