Linux FIFO 进程间通信 (服务进程-客户进程模型)

/*! @file
 **********************************************************************************
 * <PRE>
 * 模块名       : FIFO
 * 文件名       : fifo_serv.c
 * 相关文件     : fifo_client.c
 * 功能描述     : 测试进程间FIFO通信方式
 * 作者         : <Barry>
 * 版本         : 1.0
 * --------------------------------------------------------------------------------
 * 多线程安全性 : 
 * 异常时安全性 : 
 * --------------------------------------------------------------------------------
 * 备注         : 
 * --------------------------------------------------------------------------------
 * 修改记录     : 
 * 日 期        版本   修改人         修改内容 
 * 2012/04/16   1.0    <Barry>           创建
 * </PRE>
 * ********************************************************************************
 *
 * 版权所有(c) 2012, <Barry>, 保留所有权利
 *
 *********************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

#define SERV_READ_FIFO "/tmp/barry_read_fifo"

static void rm_fifo(void);

int main()
{
	int return_value;
	pid_t pid;
	int fd_read;
	int fd_write;
	char write_fifo_path[255];
	rm_fifo(); // 清理残留fifo
	mkfifo(SERV_READ_FIFO, 0777);
	fd_read = open(SERV_READ_FIFO, O_RDONLY);

	while(1)
	{
		return_value = read(fd_read, &pid, 4); // 第一次read的时候是阻塞的,但是第二次即时没有数据,也不会阻塞。所以在这里判断并sleep

		if(return_value <= 0)
		{
			sleep(1);
			continue;
		}

		printf("%u come in\n", pid);
		sprintf(write_fifo_path, "/tmp/barry_write_fifo.%u", pid); // 与客户进程通信的fifo名称
		
		if(access(write_fifo_path, F_OK) != 0) //如果对应fifo不存在则新建
		{
			mkfifo(write_fifo_path, 0777);
		}

		fd_write = open(write_fifo_path, O_WRONLY);
		time_t t = time(NULL);
		write(fd_write, &t, 4); //发送当前时间
		close(fd_write);
	}

	return 0;
}

void rm_fifo(void)
{
	system("rm -f /tmp/barry_read_fifo*");

	return;
}



/*! @file
 **********************************************************************************
 * <PRE>
 * 模块名       : FIFO
 * 文件名       : fifo_client.c
 * 相关文件     : fifo_serv.c
 * 功能描述     : 测试进程间FIFO通信方式
 * 作者         : <Barry>
 * 版本         : 1.0
 * --------------------------------------------------------------------------------
 * 多线程安全性 : 
 * 异常时安全性 : 
 * --------------------------------------------------------------------------------
 * 备注         : 
 * --------------------------------------------------------------------------------
 * 修改记录     : 
 * 日 期        版本   修改人         修改内容 
 * 2012/04/16   1.0    <Barry>           创建
 * </PRE>
 * ********************************************************************************
 *
 * 版权所有(c) 2012, <Barry>, 保留所有权利
 *
 *********************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

#define SERV_READ_FIFO "/tmp/barry_read_fifo"

int main()
{
	pid_t pid;
	int fd_read;
	int fd_write;
	char read_fifo_path[255];
    fd_write = open(SERV_READ_FIFO, O_WRONLY);
	pid = getpid();
	sprintf(read_fifo_path, "/tmp/barry_write_fifo.%u", pid);

	while(1)
	{
		write(fd_write, &pid, 4); // 向服务进程写本进程的pid
	
		while(access(read_fifo_path, F_OK) != 0) //等待服务进程创建通信的fifo
		{
			sleep(1);
		}	

		fd_read = open(read_fifo_path, O_RDONLY);
		time_t t;
		read(fd_read, &t, 4);
		close(fd_read);
		printf("%u 从服务进程得到时间数值是:%lu\n", pid, t);
		sleep(2);
	}

	return 0;
}

 服务进程运行效果


 客户进程1运行效果

 客户进程2运行效果

  客户进程3运行效果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值