work 1/8

创建

#include
int main(int argc, const char *argv[])
{
	if(mkfifo("./myfifo1",0664)!=0)
	{
		perror("mkfifo1 error");
		return -1;
	}





	if(mkfifo("./myfifo2",0664)!=0)
	{
		perror("mkfifo2 error");
		return -1;
	}

	printf("myfifo1 myfifo2 create sueecss\n");

	getchar();

	system("rm myfifo1");
	system("rm myfifo2");
	return 0;

}

a

#include
int main(int argc, const char *argv[])
{
	pid_t pid1;
	pid1=fork();
	if(pid1>0)
	{
	//管道1
	int wfd=-1;
	wfd=open("./myfifo1",O_WRONLY);
	if(wfd==-1)
	{
		perror("open error");
		return -1;
	}

	printf("写端打开成功\n");

	char wbuf[128]="";
	while(1)
	{
		bzero(wbuf,sizeof(wbuf));

		printf("请输入>>>>");
		fflush(stdout);
		read(0,wbuf,sizeof(wbuf));
		wbuf[strlen(wbuf)-1]='\0';
		
		write(wfd,wbuf,sizeof(wbuf));

		if(strcmp(wbuf,"quit")==0)
		{
			break;
		}
	}
	printf("myfifo1 create sueecss\n");
	close(wfd);

	}
	else if(pid1==0)
	{
//管道2
	int rfd1=-1;
	rfd1=open("./myfifo2",O_RDONLY);
	if(rfd1==-1)
	{
		perror("open error");
		return -1;
	}

	//printf("读端打开成功\n");

	char rbuf1[128]="";
	while(1)
	{
		bzero(rbuf1,sizeof(rbuf1));

		read(rfd1,rbuf1,sizeof(rbuf1));

		printf("收到消息:%s\n",rbuf1);
		if(strcmp(rbuf1,"quit")==0||rbuf1==0)
		{
			break;
		}
	}	
	close(rfd1);

	}
	wait(NULL);

	return 0;
}

b

#include
int main(int argc, const char *argv[])
{
	pid_t pid;
	pid=fork();
	if(pid>0)
	{
	//管道1
	int rfd=-1;
	rfd=open("./myfifo1",O_RDONLY);
	if(rfd==-1)
	{
		perror("open error");
		return -1;
	}

	//printf("读端打开成功\n");

	char rbuf[128]="";
	while(1)
	{
		bzero(rbuf,sizeof(rbuf));

		read(rfd,rbuf,sizeof(rbuf));

		printf("收到消息:%s\n",rbuf);
		if(strcmp(rbuf,"quit")==0||rbuf==0)
		{
			break;
		}
	}
	close(rfd);

	}
	else if(pid==0)
	{
	//管道2
	int wfd1=-1;
	wfd1=open("./myfifo2",O_WRONLY);
	if(wfd1==-1)
	{
		perror("open error");
		return -1;
	}

	printf("写端打开成功\n");

	char wbuf1[128]="";
	while(1)
	{
		bzero(wbuf1,sizeof(wbuf1));

		printf("请输入>>>>");
		fflush(stdout);
		read(0,wbuf1,sizeof(wbuf1));
		wbuf1[strlen(wbuf1)-1]='\0';
		
		write(wfd1,wbuf1,sizeof(wbuf1));

		if(strcmp(wbuf1,"quit")==0)
		{
			break;
		}
	}
	close(wfd1);

	}
	wait(NULL);

	return 0;
}

你可能感兴趣的:(java,前端,服务器)