王杰国庆作业day3

父子进程对话

#include 
#include 
#include 	
#include 
int main(int argc, const char *argv[])
{
	mkfifo("./fifo1",0664);
	mkfifo("./fifo2",0664);
	pid_t cpid = fork();
	if(0 < cpid)
	{
		int wfd = open("./fifo1",O_WRONLY);
		int rfd = open("./fifo2",O_RDONLY);
		char buf[128] = "";
		while(1)
		{
			bzero(buf,sizeof(buf));
			printf("父进程请输入>>>>>\n");
			scanf("%s",buf);
			write(wfd,buf,sizeof(buf));
			bzero(buf,sizeof(buf));
			read(rfd,buf,sizeof(buf));
			printf("父进程接收打印:%s\n",buf);

		}
		close(wfd);
		close(rfd);
	}
	if(0 == cpid)
	{
		int rfd = open("./fifo1",O_RDONLY);
		int wfd = open("./fifo2",O_WRONLY);
		char buf[128] = "";
		while(1)
		{	
			bzero(buf,sizeof(buf));
			read(rfd,buf,sizeof(buf));
			printf("子进程接收打印:%s\n",buf);
			bzero(buf,sizeof(buf));
			printf("子进程请输入>>>>>\n");
			scanf("%s",buf);
			write(wfd,buf,sizeof(buf));

		}
		close(wfd);
		close(rfd);
	}

	return 0;
}

你可能感兴趣的:(c语言)