IPC--------fifo的进阶使用

练习:

写两个程序fifo1 、fifo2,各自建立父子进程,子进程用来读管道,父进程用来写管道(即fifo1 的父进程写管道A,fifo2的子进程读A,结果输出到终端;fifo2的父进程写B,fifo1的子进程读B,结果输出到终端),实现fifo1 、fifo2之间的通信

调试:./A   fifo1  fifo2

./B   fifo1  fifo2   


运行结果:



源代码:

fifo1.c

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

int read_fifo(int fifo_fd)
{
	int n;
	char buf[1024];

	while(1)
	{
		n = read(fifo_fd,buf,sizeof(buf) -1);
		buf[n] = '\0';
	
		printf("Read %d bytes : %s.\n",n,buf);
		
		if(strncmp(buf,"quit",4) == 0)
			break;
	}

	return 0;
}

int write_fifo(int fifo_fd)
{
	char buf[1024];

	while(1)
	{
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf) - 1] = '\0';
		write(fifo_fd,buf,strlen(buf));

		if(strncmp(buf,"quit",4) == 0)
			break;
	}

	return 0;
}

//./a.out fifo1 fifo2
int main(int argc,char *argv[])
{
	pid_t pid;
	int n;
	char buf[1024];
	int fifo_fd1,fifo_fd2;

	if(argc < 3)
	{
		fprintf(stderr,"Usage : %s argv[1] argv[2].\n",argv[0]);
		exit(EXIT_FAILURE);
	}
	
	//鍒涘缓鏈夊悕绠¢亾 
	if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
	{
		printf("errno = %d.\n",errno);
		fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
		exit(EXIT_FAILURE);
	}
	
	//鍒涘缓鏈夊悕绠¢亾 
	if(mkfifo(argv[2],0666) < 0 && errno != EEXIST)
	{
		printf("errno = %d.\n",errno);
		fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
		exit(EXIT_FAILURE);
	}

	if((fifo_fd1 = open(argv[1],O_RDONLY)) < 0)
	{
		fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
		exit(EXIT_FAILURE);
	}

	printf("open  fifo1 success for readonly.\n ");

	if((fifo_fd2 = open(argv[2],O_WRONLY)) < 0)
	{
		fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
		exit(EXIT_FAILURE);
	}

	printf("open  fifo2 success for writeonly.\n ");

	if((pid = fork()) < 0)
	{
		perror("Fail to fork");
		exit(EXIT_FAILURE);
	}

	if(pid == 0)
	{
		read_fifo(fifo_fd1);
	}

	if(pid > 0)
	{
		write_fifo(fifo_fd2);
	}

	exit(EXIT_SUCCESS);
}


fifo2.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>

int read_fifo(int fifo_fd)
{
	int n;
	char buf[1024];

	while(1)
	{
		n = read(fifo_fd,buf,sizeof(buf) -1);
		buf[n] = '\0';
	
		printf("Read %d bytes : %s.\n",n,buf);
		
		if(strncmp(buf,"quit",4) == 0)
			break;
	}

	return 0;
}

int write_fifo(int fifo_fd)
{
	char buf[1024];

	while(1)
	{
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf) - 1] = '\0';
		write(fifo_fd,buf,strlen(buf));

		if(strncmp(buf,"quit",4) == 0)
			break;
	}

	return 0;
}

//./a.out fifo1 fifo2
int main(int argc,char *argv[])
{
	pid_t pid;
	int n;
	char buf[1024];
	int fifo_fd1,fifo_fd2;

	if(argc < 3)
	{
		fprintf(stderr,"Usage : %s argv[1] argv[2].\n",argv[0]);
		exit(EXIT_FAILURE);
	}
	
	//鍒涘缓鏈夊悕绠¢亾 
	if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
	{
		printf("errno = %d.\n",errno);
		fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
		exit(EXIT_FAILURE);
	}
	
	//鍒涘缓鏈夊悕绠¢亾 
	if(mkfifo(argv[2],0666) < 0 && errno != EEXIST)
	{
		printf("errno = %d.\n",errno);
		fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
		exit(EXIT_FAILURE);
	}

	if((fifo_fd2 = open(argv[1],O_WRONLY)) < 0)
	{
		fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
		exit(EXIT_FAILURE);
	}

	printf("open  fifo2 success for writeonly.\n ");
	
	if((fifo_fd1 = open(argv[2],O_RDONLY)) < 0)
	{
		fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
		exit(EXIT_FAILURE);
	}

	printf("open  fifo1 success for readonly.\n ");

	if((pid = fork()) < 0)
	{
		perror("Fail to fork");
		exit(EXIT_FAILURE);
	}

	if(pid == 0)
	{
		read_fifo(fifo_fd1);
	}

	if(pid > 0)
	{
		write_fifo(fifo_fd2);
	}

	exit(EXIT_SUCCESS);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值