美文网首页
linux 中有名管道mkfifo

linux 中有名管道mkfifo

作者: 嵌入式工作 | 来源:发表于2018-07-14 11:44 被阅读0次

    1创建管道并且发送 fifo

    #include <stdio.h>
    //文件操作函数头文件
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main(int argv,char *argc[])
    {
        FILE *write_file;
        char write_txt[100];
        int i;
        char buf[512];
        int rlen;
        
       char *file_fifo="testfifo";
       char *file_src="fifosrc.txt";
        
        printf("start write file \n");
        write_file = fopen(file_src,"w+");
        if(NULL == write_file)
        {
          fprintf(stderr,"open file %s err \n",file_src);
          exit(EXIT_FAILURE);
        }
        
       for(i=0;i<10000;i++)
        {
            fprintf(write_file,"write file test %10d\n",i);
        }
        
        fclose(write_file);
        
        
        printf("write file done \n");
        
        
        
        
        printf("start write fifo \n");
        
        if(0!=access(file_fifo,F_OK))
        {
             if(-1 == mkfifo(file_fifo,0777))
             {
                 fprintf(stderr,"creat fifo err \n");
                 exit(EXIT_FAILURE);
                 
             }
            
        }
       
       int fd_w,fd_r;
       
       fd_r=open(file_src,O_RDONLY);
       fd_w=open(file_fifo,O_WRONLY);
       if(fd_r==-1)
       {
               fprintf(stderr,"open %s  err \n",file_src);
                 exit(EXIT_FAILURE);
       }
         if(fd_w==-1)
       {
               fprintf(stderr,"open %s  err \n",file_fifo);
                 exit(EXIT_FAILURE);
       } 
    
       
       while((rlen=read(fd_r,buf,512))>0)
       {
           write(fd_w,buf,rlen);
           rlen=0;
      
       }
       
       close(fd_w);
       close(fd_r);
        
    printf("write fifo done!");
        
        
        
        
        
    }
    

    2读取管道fifo

    #include <stdio.h>
    
    
    
    //文件操作函数头文件
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main(int argv,char *argc[])
    {
    
         char buf[512];
       int rlen;
       char *file_fifo="testfifo";
       char *file_des="readfromfifo.txt";
       int fd_w,fd_r;
         printf("RRRstart read fifo  \n");
       fd_w=open(file_des,O_CREAT|O_WRONLY,0777);
       fd_r=open(file_fifo,O_RDONLY);
       if(fd_r==-1)
       {
               fprintf(stderr,"RRRopen %s, err \n",file_fifo);
                 exit(EXIT_FAILURE);
       }
         if(fd_w==-1)
       {
               fprintf(stderr,"RRRopen %s err \n",file_des);
                 exit(EXIT_FAILURE);
       } 
       
       
     
       
       
       while((rlen=read(fd_r,buf,512))>0)
       {
           write(fd_w,buf,rlen);
           rlen=0;
      
       }
       
       
       
       close(fd_w);
       close(fd_r);
        
        
         printf("RRR read fifo ok \n");
        
        
        
        
        
    }
    

    相关文章

      网友评论

          本文标题:linux 中有名管道mkfifo

          本文链接:https://www.haomeiwen.com/subject/moaepftx.html