zoukankan      html  css  js  c++  java
  • Linux进程间通信-命名管道

    继上篇文章分析了进程间通信管道的机制和特性,本文将从命名管道(FIFO)介绍进程间通信。

    1、命名管道(FIFO)

    管道应用的一个重大限制是它没有名字,只适合具有亲缘性质的进程之间通信。命名管道克服了这种限制,FIFO不同于管道之处在于它提供一个路径名与之关联,以FIFO的文件形式存在于文件系统中。这样,即使与FIFO的创建进程不存在亲缘关系的进程,只要可以访问该路径,就能够彼此通过FIFO相互通信(能够访问该路径的进程以及FIFO的创建进程之间),因此,通过FIFO不相关的进程也能交换数据。

    2、创建一个命名管道

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

    3、操作命名管道

    FIFO在文件系统中表现为一个文件,大部分的系统文件调用都可以用在FIFO上面,比如:read,open,write,close,unlink,stat等函数。但是seek等函数不能对FIFO调用。

    可以调用open函数打开命名管道,但是有两点要注意
    1)不能以O_RDWR模式打开命名管道FIFO文件,否则其行为是未定义的,管道是单向的,不能同时读写;
    2)就是传递给open调用的是FIFO的路径名,而不是正常的文件

    打开FIFO文件通常有四种方式:
    open(pathname, O_RDONLY);           //1只读、阻塞模式
    open(pathname, O_RDONLY | O_NONBLOCK);    //2只读、非阻塞模式
    open(pathname, O_WRONLY);          //3只写、阻塞模式
    open(pathname, O_WRONLY | O_NONBLOCK);   //只写、非阻塞模式

    注意阻塞模式open打开FIFO:
    1)当以阻塞、只读模式打开FIFO文件时,将会阻塞,直到其他进程以写方式打开访问文件;
    2)当以阻塞、只写模式打开FIFO文件时,将会阻塞,直到其他进程以读方式打开文件;
    3)当以非阻塞方式(指定O_NONBLOCK)方式只读打开FIFO的时候,则立即返回。当只写open时,如果没有进程为读打开FIFO,则返回-1,其errno是ENXIO。

     4、阻塞式命名管道

    Read进程代码如下:

    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    
    int main(int argc, char* argv[])
    {
        int ret = mkfifo("my_fifo",0777);
        if (ret == -1)
        {
            printf("make fifo failed!
    ");
            return 1;
        }
    
        char buf[256] = {0};
        int fd = open("my_fifo",O_RDONLY);
        read(fd,buf,256);
        printf("%s
    ",buf);
        close(fd);
        unlink("my_fifo");
        return 0;
    }

    Write进程代码如下:

    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char* argv[])
    {
        char *buf = "i am write process
    ";
        int fd = open("my_fifo",O_WRONLY);
        write(fd,buf,strlen(buf));
        close(fd);
        return 0;
    }

    首先启动read进程,创建fifo,当前目录下会生出一个名字为“my_fifo”的文件,然后启动write进程,运行结果如下:

    结论:当write进程没有以O_WRONLY模式open命名管道时,read进程在以O_RDONLY模式open命名管道的时候会阻塞。反过来也是这样。

    5、非阻塞式命名管道

    Read进程代码入下:

    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    
    int main(int argc, char* argv[])
    {
        int ret = mkfifo("my_fifo",0777);
        if (ret == -1)
        {
            printf("make fifo failed!
    ");
            return 1;
        }
    
        char buf[256] = {0};
        int fd = open("my_fifo",O_RDONLY | O_NONBLOCK);
       // sleep(5);   read(fd,buf,
    256); printf("%s ",buf); close(fd); unlink("my_fifo"); return 0; }

    Write进程代码如下:

    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char* argv[])
    {
        char *buf = "i am write process
    ";
        int fd = open("my_fifo",O_WRONLY | O_NONBLOCK);
        write(fd,buf,strlen(buf));
        close(fd);
        return 0;
    }

    直接启动Read进程,open函数将不会阻塞,read函数读取到0个字节数据,程序退出。

    将sleep(5)的注释去掉,再次启动Read进程,马上启动Write进程,Read进程会读取到Write进程写入fifo的数据并打印,然后退出。

    结论:flags=O_RDONLY|O_NONBLOCK:如果此时没有其他进程以写的方式打开FIFO,open也会成功返回,此时FIFO被读打开,而不会返回错误。

    经测试:flags=O_WRONLY|O_NONBLOCK:立即返回,如果此时没有其他进程以读的方式打开,open会失败打开,此时FIFO没有被打开,返回-1。

  • 相关阅读:
    vue-resource请求
    vue的生命周期
    Swift-多类型封装
    Swift
    Swift-structures 和 classes 初始化
    iOS-延时加载,延时初始化
    Swift-Closures
    WKWebView-填坑总结
    存档&&解档游戏状态
    循环引用 && weak strong
  • 原文地址:https://www.cnblogs.com/alvin2010/p/8607859.html
Copyright © 2011-2022 走看看