Linux有名管道
创始人
2024-11-11 02:39:30
0

有名管道FIFO

有名管道是文件系统中可见的文件,但是不占用磁盘空间,仍然在内存中。可以通过 mkfifo命令创建有

名管道(在共享目录不能使用mkfifo)

有名管道与无名管道一样,在应用层是基于文件接口进行操作

有名管道用于任意进程之间的通讯,当管道为空时,读进程会阻塞。

创建有名管道

mkfifo()

  1. 在shell中使用mkfifo 命令

    mkfifo filename

  2. mkfifo 函数 (在代码中使用其创建管道文件)

例子

一个进程写,一个进程读

首先通过命令创建管道文件:fifo

mkfifo fifo 

read.c

  #include    #include    #include    #include    #include    #include    #include       #define PATH_FIFO "/home/yujiu/fifo"   void if_error(int,char*);   int main(){       int res=0;       res=access(PATH_FIFO,F_OK);       if_error(res,"access");       int fd=open(PATH_FIFO,O_RDONLY);       if_error(fd,"read");       char buf[128];       ssize_t byte =0;       while((byte=read(fd,buf,sizeof(buf)))>0){           printf("read:%s\n",buf);           memset(buf,0,sizeof(buf));       }       close(fd);       return 0;   }   void if_error(int res,char* str){       if(res==-1){           perror(str);           exit(1);       }   }  

write.c

  #include    #include    #include    #include    #include    #include    #include       #define PATH_FIFO "/home/yujiu/fifo"   void if_error(int,char*,int);   int main(){       int res=0;       res=access(PATH_FIFO,F_OK);       if_error(res,"access",-1);       int fd=open(PATH_FIFO,O_WRONLY);       if_error(fd,"read",fd);       char buf[128];       ssize_t byte=0;       while(1){           memset(buf,0,sizeof(buf));           printf("input:\n");           fgets(buf,sizeof(buf),stdin);           buf[strlen(buf)-1]='\0';           byte=write(fd,buf,strlen(buf));           if_error(byte,"write",fd);           if(strcmp(buf,"exit")==0){               break;           }       }       close(fd);       return 0;   }   void if_error(int res,char* str,int fd){       if(res==-1){           perror(str);           if(fd!=-1){               close(fd);           }           exit(1);       }   }  

相关内容

热门资讯

透视指南书!hh poker软... 透视指南书!hh poker软件(透视)开挂脚本工具(哔哩哔哩)1、全新机制【hh poker软件a...
透视积累!we poker辅助... 透视积累!we poker辅助器下载(透视)开挂脚本平台(哔哩哔哩)1、起透看视 we poker辅...
透视模块!sohoo辅助(透视... 透视模块!sohoo辅助(透视)开挂透视神器(哔哩哔哩)小薇(辅助器软件下载)致您一封信;亲爱soh...
透视模块!wpk俱乐部是真的吗... 透视模块!wpk俱乐部是真的吗(透视)开挂透视神器(哔哩哔哩)1、许多玩家不知道wpk俱乐部是真的吗...
透视练习!约局吧如何查看是否有... 透视练习!约局吧如何查看是否有挂(透视)开挂透视神器(哔哩哔哩)1、不需要AI权限,帮助你快速的进行...
透视步骤!哈糖大菠萝万能挂(透... 透视步骤!哈糖大菠萝万能挂(透视)开挂透视下载(哔哩哔哩)1、完成哈糖大菠萝万能挂有辅助插件,帮助玩...
透视办法!佛手在线有挂吗(透视... 透视办法!佛手在线有挂吗(透视)开挂脚本辅助器(哔哩哔哩)1、透视办法!佛手在线有挂吗(透视)开挂脚...
透视要领!wepoker辅助器... 透视要领!wepoker辅助器官方(透视)开挂透视软件(哔哩哔哩)1、wepoker辅助器官方透视辅...
透视手段!wepoker轻量版... 透视手段!wepoker轻量版辅助(透视)开挂透视平台(哔哩哔哩)1、点击下载安装,wepoker轻...
透视技法!有没有人wepoke... 透视技法!有没有人wepoker(透视)开挂脚本下载(哔哩哔哩)1、不需要AI权限,帮助你快速的进行...