Linux 进程间通信 1.1 — 管道 :有名管道 FIFO

有名管道 FIFO

在匿名管道 pipe 中,只有通过 fork() 函数,才能使父子进程之间,共享 pipe() 开辟的管道两端的 文件描述符,而其他没有血缘关系(没有共享文件数据,就是指两个文件描述符)的管道则无法通信,因此出现了 FIFO()函数。
FIFO()不同于管道之处在于它提供一个路径名与之关联,以 FIFO 的文件形式存储与文件系统中。命名管道是一个设备文件,因此,即使进程与创建的 FIFO的 进程间不存在 血缘关系,访问该路径即可通过 FIFO 实现相互通信。
FIFO(first input first output)按照先进先出的原则工作,第一个被写入的数据将会首先从管道中读出。

命名管道的创建

#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *path,mode_t mode);
(man 3)

实际使用:
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
	int ret = kfifo("./file",0600);
	if (ret == -1 && errno != EEXIST){    //  EEXIST 是在 erron 中的一个 宏定义(百度百科),指的是,文件已存在
	// errno 是记录系统的最后一次错误代码。代码是一个int型的值,在errno.h中定义。查看错误代码errno是调试程序的一个重要方法。
	//当linux C api函数发生异常时,一般会将errno变量(需include errno.h)赋一个整数值,不同的值表示不同的含义,
	//可以通过查看该值推测出错的原因。在实际编程中用这一招解决了不少原本看来莫名其妙的问题。
		printf("mkfifo failuer\n");
		perror("why");			
		//  C 库函数 void perror(const char *str) 把一个描述性错误消息输出到标准错误 stderr。首先输出字符串 str,后跟一个冒号,然后是一个空格。
		}

path 为创建的命名管道的 路径名(该文件必须不存在): mode 为创建的命名管道的权限,mkfifo() 所创建的文件,皆可用 write 和 read 进行读写,当使用 open 打开 FIFO 文件时,O_NONBLOCK 旗标会有影响。

  1. 当使用O_NONBLOCK 旗标时,打开FIFO 文件来读取的操作会立刻返回,但是若还没有其他进程打开FIFO 文件来读取,则写入的操作会返回ENXIO 错误代码。
  2. 没有使用O_NONBLOCK 旗标时,打开FIFO 来读取的操作会等到其他进程打开FIFO文件来写入才正常返回。同样地,打开FIFO文件来写入的操作会等到其他进程打开FIFO 文件来读取后才正常返回。

返回值:
若成功则返回0,否则返回-1,错误原因存于errno中。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int mknod(const char *path,mode_t mod,dev_t dev);
(man 2)

path 为创建的命名管道的 路径名: mod 为创建的命名管道的权限,dev 为设备值,该值取决于文件创建的种类,它只在创建设备文件时才会用到,空着。
返回值:
这两个函数都是 若成功则返回0,否则返回-1,错误原因存于errno中。

阻塞

当 open 一个 FIFO 时,会默认设置阻塞(O_NONBLOCK)

  1. 若没有指定 O_NONBLOCK(默认情况),则 open FIFO(管道),read(只读)会阻塞,直至另一个进程 open 此管道,然后 写入数据。
  2. 若指定了 O_NONBLOCK,则只读的 open 会立刻出错并返回 -1,如果没有 进程 已经 为读 而打开该 FIFO,其 errno 设置为 ENXIO。

如何 指定 O_NONBLOCK,就一句话:
open("./file", 0600, O_RDONLY | O_NONBLOCK ) ;

1.eg: 首先设置一个 read 的程序,创立一个 FIFO 名叫 file 而后,另一个程序 write 去打开 file 文件,写入数据。
先运行 read,由于没有写入,会阻塞,而后运行 write ,写入成功后,read 运行成功。

read

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(){
//      char * buf = (char * )malloc(128);
        char buf[24] = {0};
        int ret = mkfifo("./file",0600);
        if (ret == -1 && errno != EEXIST){    
                printf("mkfifo failuer\n");
                perror("why");
                //  C 库函数 void perror(const char *str) 把一个描述性错误消息
        }
        int fd = open("./file",O_RDONLY);
        int nread = read(fd,buf,20);
        printf("read %d byte from fifo; context : %s \n",nread,buf);
        close(fd);
        return 0;
}

write

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(){
        char * str = "message from fifo";
        int fd = open("./file",O_WRONLY);
        if(fd > 0){
                printf("write open success\n");
        }
        int nwrite = write(fd,str,strlen(str));
        if(nwrite > 0){
                printf("write is success\n");
        }
        close(fd);
        return 0;
}

运行结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值