zoukankan      html  css  js  c++  java
  • 有名管道mkfifo

    int mkfifo(const char *pathname, mode_t mode);

    int mknod(const char *pathname, mode_t mode, dev_t dev);

    管道的一个不足之处是没有名字,因此,只能用于具有亲缘关系的进程间通信。

    在有名管道(named pipe或FIFO)提出来之后,该限制得到了克服。FIFO不同于管道之处在于它提供一个路径与之关联,

    以FIFO的文件形式存储于文件系统中。有名管道是一个设备文件。因此,即使进程与创建FIFO的进程不存在亲缘关系,

    只要可以访问该路径,就能够通过FIFO相互通信。值得注意的是,FIFO(first in first out)总是按照先进先出的原则工作,

    第一个被写入的数据将首先从管道中读出。

    有名管道的创建与读写

    LINUX下有两种方式创建有名管道。一是在Shell下交互地建立一个有名管道,二是在程序中使用系统函数建立有名管道。

    shell方式下可使用mknod或mkfifo命令,下面命令使用mknod创建一个有名管道:

    mknod namedpipe

    ----------------------------------------------------------------

    创建有名管道的函数有两个:mknod和mkfifo

    int mkfifo(const char *pathname, mode_t mode);

    int mknod(const char *pathname, mode_t mode, dev_t dev);

    函数mknod参数中path为创建的有名管道的全路径名;mod为创建的有名管道的模式,指明其存取权限;dev为设备值,该值取决于文件创建的种类,

    它只在创建设备文件时才会用到。

    umask(0);
    if (mknod("/tmp/fifo", S_IFIFO|0666, 0) == -1)
    {
    perror("mknod error!");
    exit(1);
    }

    umask(0);
    if (mkfifo("/tmp/fifo", S_IFIFO|0666) == -1)
    {
    perror("mkfifo error!");
    exit(1);
    }

    S_IFIFO|0666 指明创建一个有名管道且存取权限为0666,即创建者、与创建者同组的用户、其他用户对该有名管道的  访问权限 都是 可读可写。

    有名管道创建后就可以使用了,有名管道和管道的使用方法基本是相同的。只是使用有名管道时,必须先调用open将其打开。因为有名管道

    是一个存在于硬盘上的文件,而管道是存在于内存中的特殊文件。

    需要注意的是,调用open打开有名管道的进程可能会被阻塞。但如果同时用读写方式(O_RDWR)打开,则一定不会导致阻塞;如果以只读方式

    (O_RDONLY)打开,则调用open函数的进程将会被阻塞直到有写方式打开管道;同样以写方式(O_WRONLY)打开也会阻塞直到有读方式打开管道。

    实例:

    写端:

    #include <stdio.h>
    #include <unistd.h>
    #include <strings.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    
    
    #define FIFO_NAME "testfifo"
    
    int main(int argc, char* argv[])
    {
        int fd;
        char buf[]  = "yyyyyyy";
    
        mkfifo(FIFO_NAME, S_IFIFO|0666);
    
        fd = open(FIFO_NAME, O_WRONLY);
        
        write(fd, buf, strlen(buf)+1);
    
        close(fd);

      unlink(FIFO_NAME);//删除管道文件

    
    

      sleep(1);

    return 0;    
    }

    读端:

    #include <stdio.h>
    #include <unistd.h>
    #include <strings.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    
    
    #define FIFO_NAME "testfifo"
    #define BUF_SIZE 1024
    
    int main(int argc, char* argv[])
    {
        int fd;
        char buf[BUF_SIZE];
    
    
        fd = open(FIFO_NAME, O_RDONLY);
        
        read(fd, buf, BUF_SIZE);
    
        printf("%s
    ", buf);
    
        close(fd);
    
        return 0;    
    }
  • 相关阅读:
    arrayPointer
    shellAPP
    docker
    程序运行时内存管理
    C++ deepin
    coreOS+Docker新一代企业轻量级Linux
    玩转docker
    古典小说丛书系列软件
    读《追随智慧》(一)
    高速基于echarts的大数据可视化
  • 原文地址:https://www.cnblogs.com/zhangxuan/p/6709844.html
Copyright © 2011-2022 走看看