Wu type of inter-process communication Linux (pipe, shared memory, message queues, semaphores)

           Look how that process between private message ~

      Because the process has the independence, there is a word

        To understand how the process communication: dissemination or exchange of information between different processes 

        So why does the process of communication? Co-operation (data transmission, data sharing, process control), project modular (low coupling). 

      Why os-process communication required to control it? In order to ensure the process of independence, so that the stable operation of each process, the user is difficult to control, it is difficult to do it ~ os

      Communication Theory: multiple processes can access to provide a buffer .

      According to usage scenarios, we can be divided into the following communication:

    1. Line (anonymous pipes, named pipes) 

      Pipeline is inherited from Unix down process communication mechanism, the idea is this:  create a buffer in the kernel for both sides to communicate to transmit information. "Pipeline" is a half-duplex communication (one-way transmission of information).

    

             Anonymous pipe: to create a buffer zone is not identified , can only be used to process communication with a genetic relationship .

                 And communication flow Code: 

                

                                                                  

 

/ * 
* Basic use anonymous pipe interface 
* / 

#include <stdio.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <errno.h> 
#include < String .h> int main () 
{ // create a pipe must be created before the child
     // int pipe (int pipefd [2]);
     // pipefd: descriptor for acquiring operator pipeline
     // pipefd [0]: is used to read from the pipe data
     // pipefd [. 1]: for writing data to the pipeline
     // return value: 0 failure success -1 int pipefd [ 2 ];
     int RET = pipe (pipefd);
     IF (RET < 0


    
    ) 
    { 
        Perror ( " pipe error " );
         return - . 1 ; 
    } 
    
    int PID = the fork ();
     IF (PID < 0 ) 
    { 
        return - . 1 ; 
    } 
    the else  IF (PID == 0 ) 
    { 
        // child process close the write end 
        Close (pipefd [ . 1 ]);
         // child process - read data pipeline 
        char BUFF [ 1024 ] = { 0 }; 
        read (pipefd [ 0 ], BUFF,1023 ); 
        the printf ( " BUFF: [% S] \ n- " , BUFF); 
    } 
    the else 
    { 
        // parent read off end 
        Close (pipefd [ 0 ]);
         // parent - to write data to the pipe 
        char PTR = * "Me do you like? " ; 
        Write (pipefd [ . 1 ], BUFF, strlen (PTR)); 
    } 
    
    return  0 ; 
}    

                W properties pipeline:

                   If the pipeline is no data , the read will be blocked until the read data

                     If the pipeline data is full , it writes will be blocked until the data is read out

                     If the pipe all the write end are closed , then the read end reading after the data pipeline, will return 0

                     If the pipe all the read end are closed , then the write-side write data when it will trigger an exception , exit process

                Pipeline Features:

                   1. The half-duplex communication , data can only flow in one direction

                   2. W properties

                   3. The kernel will pipeline synchronization and mutual exclusion operation (if the size of the write data pipe <= PIPE_BUF, read and write operations are atomic operations are not interrupted)

                   4. Provide byte stream (stream does not contain a continuous border) service ( the transmission data is flexible, it may cause blocking of data (data no border) )

                     5. lifecycle process exits with exit 

                Now, with the anonymous pipe to achieve ls | grep command:

// LS | grep analog realization 
#include <stdio.h> 
#include <unistd.h> 
#include <errno.h> int main () 
{ // create anonymous pipe int pipefd [ 2 ];
     int RET = pipe ( pipefd);
     IF (RET < 0 ) 
    { 
        perror ( " pipe error \ n- " );
         return - . 1 ; 
    } int PID1 = the fork ();
     IF (PID1 == 0 ) 
    { // LS --- written to standard Print output
        


    
    
    
    
        // standard output to a write pipe end 
        Close (pipefd [ 0 ]);            
        dup2 (pipefd [ . 1 ], . 1 ); 
        execlp ( " LS " , " LS " , NULL); 
        Exit ( 0 ); 
    } 
    int PID2 = the fork ();
     IF (PID2 == 0 ) 
    { 
        // grep the make reading data from the standard input ---
         // redirect the input to the read end of the pipe the standard 
        Close (pipefd [ . 1 ]);   
        dup2 (pipefd [ 0 ], 0 ); 
        execlp (" Grep " , " grep " , " the make " , NULL); 
        Exit ( 0 ); 
    } 
    
    // close the pipe before the wait, the pipe to prevent communication between the impact subprocess 
    Close (pipefd [ 0 ]);    
    Close (pipefd [ . 1 ]); 
    the wait (NULL); 
    the wait (NULL); 
    return  0 ; 
}

                Named pipes: a piping found in the files created by the system, all processes can get the file descriptor core pipe by opening the file.

                  Features: let the same machine can communicate in any process .

                  Open features: 

                    If the pipeline file is read-only / write only way open will block until the file is write-only / open read-only;

                    It is opened for reading and writing, without blocking.

                  And communication flow Code: 

                     Write pipe end mkfifo Create File -> Open reading pipe end -> ends may be a one-way communication

//命名管道 读端demo
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<errno.h>

int main()
{
    int fd=open("./test.fifo",O_RDONLY);
    while(1)
    {
        char buff[1024]={0};
        int ret=read(fd,buff,1023);
        if(ret>0)
        {
        printf("client say:%s\n",buff);
        }
        else if(ret == 0)
        {
        printf("write close!\n");
        return -1;
        }
        else 
        {
        perror("read error");
        return -1;
        }
    }    
    close(fd);
    return 0;
}
// named pipe using the write end substantially Demo
 //    int the mkfifo (const char * pathname, mode_t MODE)
 //    MODE: Permissions Return Value: 0 Failure Success -1 
#include <stdio.h> 
#include <unistd.h> 
# the include <errno.h> 
#include < String .h> 
#include <fcntl.h> 
#include <SYS / defined in stat.h> int main () 
{ int RET = the mkfifo ( " ./test.fifo " , 0664 );
     IF (RET < 0 ) 
    { 
        perror ( " mkfifio error " );
        return


     -1;
    }
    
    int fd = open("./test.fifo",O_WRONLY);
    if(fd<0)
    {
        perror("open error");
        return -1;
    }
    printf("open fifo success!\n");

    while(1)
    {
        char buff[1024] = {0};
        scanf("%s",buff);
        write(fd,buff,strlen(buff));
        printf("buff:[%s]\n",buff);
    }        
    close(fd);
    return 0;
}

               Run the demo: 

                

 

 

                  

               

 

 

            

  

 

 

 

 

 

 

 

 

 

             

 

 

      

     

 

 

            

 

    

 

 

Guess you like

Origin www.cnblogs.com/Duikerdd/p/11737374.html