利用有名管道实现进程间的通信

 1 /*****************************************************************
  2 *   Copyright (C) 2018 FBI WARNING. All rights reserved.
  3 *   
  4 *   文件名称:fifo_write.c
  5 *   创 建 者:constantine
  6 *   创建日期:2018年02月26日
  7 *   描    述:
  8 *
  9 *****************************************************************/
 10 
 11 #include <stdio.h>
 12 #include <unistd.h>
 13 #include <sys/types.h>
 14 #include <sys/wait.h>
 15 #include <string.h>
 16 #include <stdlib.h>
 17 #include <sys/stat.h>
 18 #include <fcntl.h>
 19 
 20 int main()
 21 {
 22     int ret,fd;
 23     char buf[100]={0};
 24     ret = mkfifo("fifo.tmp",O_CREAT | O_EXCL);
 25     if(-1 == ret)
 26     {
 27         perror("mkfifo");
 28         exit(1);
 29 
 30     }

上面是写的一端下面是读的一端


/*****************************************************************
*   Copyright (C) 2018 FBI WARNING. All rights reserved.
*   
*   文件名称:fifo_read.c
*   创 建 者:constantine
*   创建日期:2018年02月26日
*   描    述:
*
*****************************************************************/

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



int main()
{
	int ret;
	char buf[100] = {0};
	int fd = open("fifo.tmp",O_RDONLY);
	if(-1 == fd)
	{
		perror("read");
		exit(1);

	}
	while(1)
	{
		ret = read(fd,buf,sizeof(buf));
		if(-1 == ret)
		{
			 perror("read");
			 exit(1);
		}
		if(!strcmp(buf,"bye"))
		{
		  break;
		}
		printf("read from buf: %s\n",buf);
		memset(buf,0,sizeof(buf));
	}

	return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值