Operation of 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)  
    {  
        // pipe file does not exist  
         // create named pipe   
        res = mkfifo(fifo_name, 0777 );  
         if (res != 0 )  
        {  
            fprintf(stderr, "Could not create fifo %s\n", fifo_name);  
            exit(EXIT_FAILURE);  
        }  
    }  
  
    printf( " Process %d opening FIFO O_WRONLY\n " , getpid());  
     // Open the FIFO file in write-only blocking mode and open the data file in read-only mode   
    pipe_fd = open(fifo_name, open_mode);  
    data_fd = open("Data.txt", O_RDONLY);  
    printf("Process %d result %d\n", getpid(), pipe_fd);  
  
    if(pipe_fd != -1)  
    {  
        int bytes_read = 0 ;  
         // Read data to the data file   
        bytes_read = read(data_fd, buffer, PIPE_BUF);  
        buffer[bytes_read] = '\0';  
       // 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\n");  
                exit(EXIT_FAILURE);  
            }  
            // Accumulate the number of bytes written, and continue to read the data   
            bytes_sent += res;  
            bytes_read = read(data_fd, buffer, PIPE_BUF);  
            buffer[bytes_read] = '\0';  
       // }  
        close(pipe_fd);  
        close(data_fd);  
    }  
    else  
        exit(EXIT_FAILURE);  
  
    printf("Process %d finished\n", 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>  
  
intmain ()  
{  
    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, '\0', sizeof(buffer));  
  
    printf( " Process %d opening FIFO O_RDONLY\n " , getpid());  
     // Open the pipe file in read-only blocking mode, pay attention to the same name as the FIFO in the fifowrite.c file  
     // Create a file to save data in write-only mode   
    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\n " ,getpid(), pipe_fd);  
             // Read the data in the FIFO and save it in the file DataFormFIFO.txt   
            res = read(pipe_fd, buffer, PIPE_BUF) ; 
             if (res > 0 ) {        
                printf("%s\n",buffer);
                memset(buffer, '\0', 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\n", getpid(), bytes_read);  
    exit(EXIT_SUCCESS);  
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324966374&siteId=291194637