zoukankan      html  css  js  c++  java
  • pipe的操作

             const char *fifo_name = "/tmp/my_fifo";  
        int pipe_fd = -1;  
        int data_fd = -1;  
        int res = 0;  
        const int open_mode = O_WRONLY;  
        int bytes_sent = 0;  
        char buffer[PIPE_BUF + 1];  
      
        if(access(fifo_name, F_OK) == -1)  
        {  
            //管道文件不存在  
            //创建命名管道  
            res = mkfifo(fifo_name, 0777);  
            if(res != 0)  
            {  
                fprintf(stderr, "Could not create fifo %s
    ", fifo_name);  
                exit(EXIT_FAILURE);  
            }  
        }  
      
        printf("Process %d opening FIFO O_WRONLY
    ", getpid());  
        //以只写阻塞方式打开FIFO文件,以只读方式打开数据文件  
        pipe_fd = open(fifo_name, open_mode);  
        data_fd = open("Data.txt", O_RDONLY);  
        printf("Process %d result %d
    ", getpid(), pipe_fd);  
      
        if(pipe_fd != -1)  
        {  
            int bytes_read = 0;  
            //向数据文件读取数据  
            bytes_read = read(data_fd, buffer, PIPE_BUF);  
            buffer[bytes_read] = '';  
           // while(bytes_read > 0)  
           // {  
                //向FIFO文件写数据  
                //res = write(pipe_fd, buffer, bytes_read);  
                res = write(pipe_fd, argv[1],strlen(argv[1])); 
                
                if(res == -1)  
                {  
                    fprintf(stderr, "Write error on pipe
    ");  
                    exit(EXIT_FAILURE);  
                }  
                //累加写的字节数,并继续读取数据  
                bytes_sent += res;  
                bytes_read = read(data_fd, buffer, PIPE_BUF);  
                buffer[bytes_read] = '';  
           // }  
            close(pipe_fd);  
            close(data_fd);  
        }  
        else  
            exit(EXIT_FAILURE);  
      
        printf("Process %d finished
    ", getpid());  
        exit(EXIT_SUCCESS); 
    #include <unistd.h>  
    #include <stdlib.h>  
    #include <stdio.h>  
    #include <fcntl.h>  
    #include <sys/types.h>  
    #include <sys/stat.h>  
    #include <limits.h>  
    #include <string.h>  
      
    int main()  
    {  
        const char *fifo_name = "/tmp/my_fifo";  
        int pipe_fd = -1;  
        int data_fd = -1;  
        int res = 0;  
        int open_mode = O_RDONLY;  
        char buffer[PIPE_BUF + 1];  
        int bytes_read = 0;  
        int bytes_write = 0;  
        //清空缓冲数组  
        memset(buffer, '', sizeof(buffer));  
      
        printf("Process %d opening FIFO O_RDONLY
    ", getpid());  
        //以只读阻塞方式打开管道文件,注意与fifowrite.c文件中的FIFO同名  
        //以只写方式创建保存数据的文件  
        data_fd = open("DataFormFIFO.txt", O_WRONLY|O_CREAT, 0644);  
      
        //if(pipe_fd != -1)  
        //{  
            //int  count =10;
            while(1){
            //do  
           // {  
                pipe_fd = open(fifo_name, open_mode);
                if(pipe_fd){
                printf("Process %d result %d
    ",getpid(), pipe_fd);  
                //读取FIFO中的数据,并把它保存在文件DataFormFIFO.txt文件中  
                res = read(pipe_fd, buffer, PIPE_BUF); 
                if(res>0)    {        
                    printf("%s
    ",buffer);
                    memset(buffer, '', sizeof(buffer));  
                    close(pipe_fd);  
                }
    
                }
                else{
                    printf("read %d ",res);
                    usleep(500);
                }
                ///bytes_write = write(data_fd, buffer, res);  
               // bytes_read += res;  
           // }while(res>0); 
            //count--;        
            }
            close(pipe_fd);  
            close(data_fd);  
        //}  
       // else  
           // exit(EXIT_FAILURE);  
      
        printf("Process %d finished, %d bytes read
    ", getpid(), bytes_read);  
        exit(EXIT_SUCCESS);  
    } 
  • 相关阅读:
    继承项目第13周项目1基类中成员的访问限定符和派生类的继承方式
    架构业务wait和sleep区别狭义jiavaBean规范,三层架构模式
    文件文本编辑器ASP.net使用CKEditor(html文本编辑器)
    彩信对象android(5)_发彩信操作
    方法接口UML统一建模语言,java中七种设计原则,
    jquery实现jQuery实现图片轮播效果,jQuery实现焦点新闻
    设备文件BSP及嵌入式驱动开发笔记
    Invalidate
    C#集合类使用范例
    判断一段程序是由C 编译程序还是由C++编译程序编译的
  • 原文地址:https://www.cnblogs.com/baldermurphy/p/8963170.html
Copyright © 2011-2022 走看看