linux进程通信管理之:命名管道

    作者:课课家教育更新于: 2016-02-18 18:21:21

      管道应用的一个重大限制就是它没有名字,因此,只能用于具有亲缘关系的进程间的通信,在有名管道(Named Pipe或 FIFO)提出后,该限制得到了克服,FIFO不同于管道之处在于它提供了一个路径名与之关联,以FIFO的文件形式存在与文件系统中。这样,即使与FIFO的创建进程不存在亲缘关系的进程,只要可以访问该路径,彼此之间就能进行通信。

    linux进程通信管理之:命名管道_Linux管道_Linux系统_Linux_课课家

      一个比较好的命名管道例子:

      下面这个例子使用FIFO进行进程间的通信,程序lucy.c创建了FIFO write_fifo用于向程序peter.c发送消息;peter.c程序创建了FIFO read_fifo用于向lucy.c发送消息。

      同时,lucy.c能够通过打开peter.c创建的FIFO来得到的peter.c发来的消息,peter.c能够通过打开lucy.c创建的FIFO来得到lucy.c发来的消息。因此两者就能互相通信了,两者必须在线才能进行通信聊天,这个有点类似qq的聊天功能。

      下面是源程序lucy.c

      #include

      #include// 文件状态的头文件

      #include //字符串函数头文件

      #include //标准的输入输出头文件

      #include //错误提示头文件

      #include //控制头文件

      #include //创建进程的头文件

      int main(void)

      {

      char write_fifo_name[]="write-fifo"; //定义一个写管道名字

      char read_fifo_name[]="read-fifo";//定义一个读管道的名字

      int write_fd,read_fd;

      char buf[256]; //定义存储通信的会话空间长度

      int len;

      struct stat stat_buf;

      int ret=mkfifo(write_fifo_name,S_IRUSR | S_IWUSR);

      if(ret==-1) //创建一个命名管道

      {

      printf("Fail to create FIFO %s",write_fifo_name,strerror(errno));

      exit(-1);

      }

      write_fd=open(write_fifo_name,O_WRONLY); //以读写方式打开命名的管道

      if(write_fd==-1) {

      printf("Fail to open FIFO %s: %s",write_fifo_name,strerror(errno));

      exit(-1);

      }

      while((read_fd=open(read_fifo_name,O_RDONLY))==-1) { //以只读方式打开管道文件

      sleep(1);

      }

      while(1) {

      printf("熊尧: ");

      fgets(buf,256,stdin); //fgets()函数,从stdin流中读取256-1个字符存储在以buf为地址的空间里,至到读取成功,成功则返回buf的指针

      buf[strlen(buf)-1]='\\0';

      if(strncmp(buf,"quit",4)==0) { //用strncmp()函数来比较buf中的前四个字符是否为quit

      close(write_fd);//若是,则关闭写入函数

      unlink(write_fifo_name); //删除文件的目录并减少它的链接数

      close(read_fd); //最后要关闭打开的对话文件

      exit(0);

      }

      write(write_fd,buf,strlen(buf));

      len=read(read_fd,buf,256);

      if(len>0) {

      buf[len]='\\0';

      printf("红刚: %s\\n",buf);

      }

      }

      }

      下面是peter.c

      #include

      #include

      #include

      #include

      #include

      #include

      int main(void)

      {

      char write_fifo_name[]= "read-fifo";

      char read_fifo_name[]= "write-fifo";

      int write_fd,read_fd;

      char buf[256];

      int len;

      int ret=mkfifo(write_fifo_name,S_IRUSR | S_IWUSR);

      if(ret==-1) {

      printf("Fail to create FIFO %s",write_fifo_name,strerror(errno));

      exit(-1);

      }

      while((read_fd=open(read_fifo_name,O_RDONLY))==-1) {

      sleep(1);

      }

      write_fd=open(write_fifo_name,O_WRONLY);

      if(write_fd==-1) {

      printf("Fail to open FIFO %s: %s",write_fifo_name,strerror(errno));

      exit(-1);

      }

      while(1) {

      len=read(read_fd,buf,256);

      if(len>0) {

      buf[len]='\\0';

      printf("熊尧: %s\\n",buf);

      }

      printf("红刚:");

      fgets(buf,256,stdin);

      buf[strlen(buf)-1]='\\0';

      if(strncmp(buf,"quit",4)==0) {

      close(write_fd);

      unlink(write_fifo_name);

      close(read_fd);

      exit(0);

      }

      write(write_fd,buf,strlen(buf));

      }

      }

      在两个端口分别开始运行lucy.c和peter.c,即可以开始聊天了

课课家教育

未登录

1