zoukankan      html  css  js  c++  java
  • 命名管道FIFO

    1.可以在任意进程(不需要有关系)中进行通信;

    2.管道本质是内核中的一块缓存;

    3.FIFO在文件系统中存在一个管道文件,管道文件也是和内核中的缓存同步的,是指向文件的路径;

    4.命令管道默认是阻塞型;

    5.对FIFO的操作跟普通文件一样,可用open打开它,一般的文件I/O函数(close,read,write,ulink)都可以使用。

    mkfifo s.pipe

    ------------------------------------Fifo_read.c------------------------------------------

    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <memory.h>

    int main(int argc, char *argv[])
    {
        if(argc <2)
        {
            printf("usage:%s fifo ",argv[0]);
            exit(1);
        }    
        printf("open fifo read... ");
        // 打开命名管道
        int fd=open(argv[1],O_RDONLY);
        if(fd < 0)
        {
            perror("open error");
            exit(1);
        }
        else
        {
            printf("open file success: %d ",fd);
        }
        //从命名管道中读取数据
        char buff[512];
        memset(buf,0,sizeof(buf));
        while(read(fd,buf,sizeof(buf)) <0 )
        {
            perror("read error");
        }
        printf("%s ",buf);
        close(fd);
        exit(0);
    }

    ------------------------------------Fifo_write.c------------------------------------------

    #include <unistd.h>
    #include <memory.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <fcntl.h>

    int main(int argc,char *argv[])
    {
        if(argc <2)
        {
            printf("usage: %s fifo ",argv[0]);
            exit(1);
        }
        printf("open fifo write... ");
        
        //打开命名管道
        int fd=open(argv[1],O_WRONLY);
        if(fd < 0)
        {
            perror("open error");
            exit(1);
        }
        else
        {    
            printf("open fifo success: %d ",fd);
        }
        char *s="1234567890";
        size_t size = strenlen(s);
        if(write(fd,s,size) != size)
        {
            perror("write error");
        }
        close(fd);
        
        exit(0);
    }

  • 相关阅读:
    二维树状数组的区间加减及查询 tyvj 1716 上帝造题的七分钟
    hdu5399
    WebLogicSSL解决苹果IOS itms下载问题
    怎样封装RESTful Web Service
    ie8下面版本号(包含ie8)的浏览器不支持html5标签属性解决方式(Modernizr 2.6.2插件的使用)
    在Linux平台使用VNC连接树莓派
    合作开发——设计阶段
    HDU-1165-Eddy&#39;s research II
    Iocomp控件教程之LinearGauge--线性刻度尺控件
    VB中的排序问题 15个
  • 原文地址:https://www.cnblogs.com/lvdh1314/p/6511709.html
Copyright © 2011-2022 走看看