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

      首先我得检讨一下自己,这几天有些颓呀,打不起精神,板子出了点问题,果真自学还是很困难呀,硬件方面难解决呀,理想与现实还是很有差距的,伤透了,凌乱了。

      一直在理解进程间通信的问题。发现上次忽略了一个问题,就是命名管道,命名管道和一般的管道有一些显著的不同:

    1.FIFO是在文件系统中作为一个特殊的设备文件而存在的;

    2.不同祖先的进程之间可以通过管道共享数据;

    3.当共享管道的进程执行完所有的I/O操作后,FIFO将继续保存在文件系统中以便以后使用。

      这里需要注意一下:管道只能由相关的进程使用,它们共同的祖先进程创建了管道,但是通过FIFO,不相关的进程也能交换数据。

      FIFO的创建:

    #include <sys/types.h>

    #include <sys/stat.h>

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

    返回:若成功则为0,出错为-1;一旦已经mkfifo创建了一个FIFO,就可以open打开它。

     当打开一个FIFO时,非阻塞标志会产生如下影响:

    (1)一般情况,只读打开要阻塞到某个其它进程为写打开此FIFO。类似,为写打开一个FIFO要阻塞到某个其它进程为读而打开它。

    (2)如果指定非阻塞状态,则只读打开立即返回,但是如果没有进程已经为读而打开一个FIFO,那么只写打开将出错返回,其errno是ENXIO。

       类似于管道,若写一个尚无进程为读而打开的FIFO,则产生信号SIGPIPE。若某个FIFO的最后一个写进程关闭了该FIFO,则将为该FIFO的读进程产生一个文件结束标志。

      具体点代码如下:

     1 #include <sys/types.h>
     2 #include <sys/stat.h>
     3 #include <errno.h>
     4 #include <fcntl.h>
     5 #include <stdio.h>
     6 #include <stdlib.h>
     7 #include <string.h>
     8 #define FIFO "/tmp/myfifo"
     9 
    10 main(int argc,char** argv)
    11 {
    12     char buf_r[100];
    13     int  fd;
    14     int  nread;
    15     
    16     if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
    17         printf("cannot create fifoserver
    ");
    18     printf("Preparing for reading bytes...
    ");
    19     
    20     memset(buf_r,0,sizeof(buf_r));
    21     fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
    22     if(fd==-1)
    23     {
    24         perror("open");
    25         exit(1);    
    26     }
    27     while(1)
    28     {
    29         memset(buf_r,0,sizeof(buf_r));
    30         
    31         if((nread=read(fd,buf_r,100))==-1){
    32             if(errno==EAGAIN)
    33                 printf("no data yet
    ");
    34         }
    35         printf("read %s from FIFO
    ",buf_r);
    36         sleep(1);
    37     }    
    38     pause();
    39     unlink(FIFO);
    40 }
    fifo_read
     1 #include <sys/types.h>
     2 #include <sys/stat.h>
     3 #include <errno.h>
     4 #include <fcntl.h>
     5 #include <stdio.h>
     6 #include <stdlib.h>
     7 #include <string.h>
     8 #define FIFO_SERVER "/tmp/myfifo"
     9 
    10 main(int argc,char** argv)
    11 {
    12     int fd;
    13     char w_buf[100];
    14     int nwrite;
    15     
    16     if(fd==-1)
    17         if(errno==ENXIO)
    18             printf("open error; no reading process
    ");
    19     fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
    20     if(argc==1)
    21         printf("Please send something
    ");
    22     strcpy(w_buf,argv[1]);
    23     if((nwrite=write(fd,w_buf,100))==-1)
    24     {
    25         if(errno==EAGAIN)
    26             printf("The FIFO has not been read yet.Please try later
    ");
    27     }
    28     else 
    29         printf("write %s to the FIFO
    ",w_buf);
    30 }
    fifo_write
  • 相关阅读:
    (一)maven基本配置,概念,常用命令
    redis 小结
    git 小结
    spring.xml
    servlet web.xml学习笔记
    springmvc小试牛刀
    maven
    springmvc学习笔记1
    springmvcpojo
    springmvc学习笔记
  • 原文地址:https://www.cnblogs.com/lihuidashen/p/3470254.html
Copyright © 2011-2022 走看看