• 命名管道FIFO及其读写规则


      一、匿名管道的一个限制就是只能在具有共同祖先的进程间通信
    命名管道(FIFO):如果我们想在不相关的进程之间切换数据,可以使用FIFO文件来做这项工作
    注意:命名管道是一种特殊类型文件。
    利用命令:$ mkfifo filename
    或者相关函数:int mkfifo(const char*filename,mode_t mode);
      二、区别与联系
    匿名管道由pipe函数创建或打开
    命名管道由mkfifo函数创建,打开用open.
    FIFO与PIPE的区别在它们创建与打开的方式不同,一旦这些工作完后,它们语义相同。

    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<fcntl.h>
    #include<stdlib.h>
    #include<stdio.h>
    #include<errno.h>
    #include<string.h>
    
    #include<signal.h>
    #define ERR_EXIT(m)
        do
        {
            perror(m);
            exit(EXIT_FAILURE);
        }while(0)  //宏要求一条语句
    int main(int argc,char*argv[])
    {
        umask(0);//掩码先要清零。 mode & (~umask)
        mkfifo("p2",0644);//当前路径下创建一个p2
        return 0;
    }

      有名管道读写规则

    命名管道打开规则:如果当前打开操作是为读而打开FIFO
    O_NONBLOCK disable:阻塞直到有相应的进程为写而打开FIFO
    O_NONBLOCK enable:非阻塞模式立刻返回成功。并不是-1。不要等到有一个进程写而打开。
    如果当前打开操作是为写而打开FIFO
    O_NONBLOCK disable:阻塞直到有相应的进程为读而打开FIFO
    O_NONBLOCK enable:立刻返回失败,错误码ENXIO

      下面两个进程,一个是读打开,一个是为写而打开,阻塞非阻塞的区别见程序:

    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<fcntl.h>
    #include<stdlib.h>
    #include<stdio.h>
    #include<errno.h>
    #include<string.h>
    
    #include<signal.h>
    #define ERR_EXIT(m)
        do
        {
            perror(m);
            exit(EXIT_FAILURE);
        }while(0)  //宏要求一条语句
    
    int main(int argc,char*argv[])
    {
        int fd;
        fd=open("p1",O_RDONLY);//打开p1,默认阻塞模式.用mkfifo p1已经创建好了p1管道文件。
        //fd=open("p1",O_RDONLY|O_NONBLOCK);//非阻塞模式打开,直接返回成功,不用等另一个进程写成功
        if(fd==-1)
            ERR_EXIT("open error");
        printf("open success
    ");
        return 0;
    }

      下面这一进程以写的方式打开FIFO ,同时运行上一个以读的方式打开FIFO进程。则两个都成功了。

    1、如果是O_NONBLOCK disable:阻塞直到有相应的进程为读而打开该FIFO;

    2、如果是O_NONBLOCK enable:立刻返回失败,错误码为ENXIO。

    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<fcntl.h>
    #include<stdlib.h>
    #include<stdio.h>
    #include<errno.h>
    #include<string.h>
    #include<signal.h>
    #define ERR_EXIT(m)
        do
        {
            perror(m);
            exit(EXIT_FAILURE);
        }while(0)  //宏要求一条语句
    int main(int argc,char*argv[])
    {
        int fd;
        //fd=open("p1",O_WRONLY);//写打开p1,阻塞.用mkfifo p1
        fd=open("p1",O_WRONLY|O_NONBLOCK);//非阻塞模式打开(没有任何为读而打开的)立刻返回失败
        if(fd==-1)
            ERR_EXIT("open error");
        printf("open success
    ");
        return 0;
    }
  • 相关阅读:
    objective-C nil,Nil,NULL 和NSNull的小结
    Calendar控件点击下个月按钮后,本月标记的各个具体天的样式都取消
    如何让Button的Text垂直居中显示
    html基础总结2
    html基础总结1
    html基础总结
    微信空白页获取用户openid
    网址
    网站式更新后台代码
    JavaScriptSerializer引用
  • 原文地址:https://www.cnblogs.com/wsw-seu/p/8399491.html
Copyright © 2020-2023  润新知