Interprocess communication (pipes and named pipes)

Pipe (Pipe) is a mechanism for one-way communication between two processes, because it is one-way, it is also known half-duplex pipes. It is mainly used for simple inter-process communication.

  1. Data can only be (a write pipeline, a pipeline read) by the flow of a process to another process; if you want full-duplex communication, need to build two pipelines.
  2. Pipeline can only be used for communication between parent and child or sibling processes.
  3. Pipe has no name, and its limited buffer size.
  4. A process to write data to the pipe, each time data is added to the end of the pipe's buffer; another process to read data from the other end of the pipe, the head reads data from the buffer.

Create a command pipeline:

#include <unistd.h>
int pipe(int fd[2])

Ends of the pipe respectively descriptor fd [0] and fd [1] will be described. Wherein fd [0] for read only, read-called pipe end; fd [1] can be used to write, write called pipe end.
General usage pipe: to create a pipeline, and then create a child process with fork, after the parent process close the read end of the pipe (or write-side), the child process close the write end of the pipe (or read end), the parent process to write to the pipeline enter, the child will be able to read the data from the pipe.

example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
 
/*读管道*/
void read_from_pipe (int fd)
{
        char message[100];
        read (fd,message,100);
        printf("read from pipe:%s",message);
}
 
/*写管道*/
void write_to_pipe (int fd)
{
        char *message = "Hello, pipe!\n";
        write (fd, message,strlen(message)+1);
}
 
int main(void)
{
        int     fd[2];
        pid_t   pid;
        int     stat_val;
 
        if (pipe (fd))
        {
                printf ("create pipe failed!\n");
                exit (1);
        }
 
        pid = fork();
        switch (pid)
        {
                case -1:
                        printf ( " fork error \ the n-! " ); 
                        Exit ( 1 );
                 Case  0 :
                         / * child process close fd1 * / 
                        use Close (fd [ 1 ]); 
                        read_from_pipe (fd [ 0 ]); 
                        Exit ( 0 );
                 default :
                         / * parent close fd0 * / 
                        Close (FD [ 0 ]); 
                        write_to_pipe (FD [ . 1 ]);
                        wait (&stat_val);
                        exit (0);
        }
 
        return 0;
}

 

Named Pipes

One disadvantage of the duct is no name, it can only have a communication between processes in the genetic relationship. The "named pipe" On the contrary, it provides a path name associated with it, as there is a device file, even between unrelated processes, as long as access to the path, you can also communicate through FIFO. FIFO always work according to the FIFO principle, the first data is written is read out from the first pipe.

Prototype:

#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *path,mode_t mode);

path is the path name of the named pipe is created; mode to create a model named pipe, indicating its access. Function call returns 0 on success, failure to return -1.
      Before using a named pipe present, we need to use open () to open. Because the named pipe is present in a file on the hard disk, and the pipe is a special file exists in memory.

      The following program demonstrates how the named pipe communication between processes unrelated.

Write side:

#include <stdio.h> 
#include <stdlib.h> 
#include < String .h> 
#include <fcntl.h> 
#include <SYS / types.h> 
#include <SYS / defined in stat.h> #define FIFO_NAME " myfifo "
 #define buf_size 1024 int main ( void ) 
{ int      FD;
     char     buf [buf_size] = " the Hello procwrite, the I Come from the named Process procread! " ; 
    the umask ( 0 );
     // specified to create a named pipe and access 0666, namely the creator, the creator and user of the same group, the other users access to named pipes are readable and writable if
 

 

    
 
    (the mkfifo (FIFO_NAME, S_IFIFO | 0666 ) == - . 1 )         
    { 
        perror ( " the mkfifo error! " ); 
        Exit ( . 1 ); 
    } 
 
    IF ((FD = Open (FIFO_NAME, O_WRONLY)) == - . 1 ) / * In opened for writing the FIFO * / 
    { 
        perror ( " the fopen error! " ); 
        Exit ( . 1 ); 
    } 
    write (FD, buf, strlen (buf) + . 1 ); / * write data to the FIFO * / 
 
    Close (FD); 
    Exit ( 0);

}

Reading side:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
 
#define FIFO_NAME      "myfifo"
#define BUF_SIZE        1024
 
int main(void)
{
        int     fd;
        char    buf[BUF_SIZE];
 
        umask (0);
        fd = open(FIFO_NAME, O_RDONLY);
        read (fd, buf, BUF_SIZE);
        printf ("Read content: %s\n", buf);
        close (fd);
        exit (0);
}

 

These are the basic usage of the pipes and named pipes.

Guess you like

Origin www.cnblogs.com/lonelamb/p/11261122.html