• 命名管道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);
    }

  • 相关阅读:
    Linux系统_Linux平台“盖茨木马”初步了解
    查杀病毒的NB命令
    rabbitmq 常用的一些命令
    date 修改系统时间
    mkpasswd
    关于haproxy负载均衡的算法整理
    MySQL数据表中内容大小写区分的设置
    查看某个端口的连接数
    rabbitmq
    mysqldump 报导常
  • 原文地址:https://www.cnblogs.com/lvdh1314/p/6511709.html
Copyright © 2020-2023  润新知