Linux-interprocess communication-pipelines

Source: Internet
Author: User
I. The principle of communication between processes
Inter-process Exchange data must be through the kernel, a buffer in the kernel, process 1 to copy the data space to the kernel buffer, process 2 and then read the data from the kernel buffer. This enables interprocess communication.
Second, process communication of the system-pipeline (pipe)

Call pipe (int filedes[2]) function in memory open space is called the pipeline, it reads data one end of the data, through the filedes out to the user program two descriptor, Filedes[0] point to the read end, Filedes[1] point to the write end. Read or write a file from the pipe, either through Read (filedes[0) or write (filedes).

Third, the method of realizing process communication with pipeline

(1) The parent process creates a pipeline, which gets two file descriptors pointing at both ends of the pipe,
(2) The parent process calls fork () to create a child process, and the child process also has two file descriptors pointing at both ends of the pipeline.
(3) The parent process shuts down the write end, the child process closes the read end, the child process writes to the pipeline, the parent process reads, the pipeline is implemented by the ring queue, the data is written from the write end, and the read is read, thus realizing the process communication
--Code-----

#include <stdio.h> #include <unistd.h> int main () {int _pipe[2];     int Ret=pipe (_pipe);       if (Ret < 0) {printf ("pipe ()");     return 1;     } pid_t id=fork ();       if (ID < 0) {printf ("fork ()");     return 2;       } else if (id==0) {close (_pipe[0]);       Char buf[]= "Hello World";       int count=10;        while (count) {sleep (1);        ssize_t _size=write (_pipe[1],buf,sizeof (BUF));          if (_size < 0) {printf ("write ()");        return 3;        } count--;      }}else {Close (_pipe[1]);      Char buf[1024];      int i=0;        while (i++<10) {ssize_t _size=read (_pipe[0],buf,sizeof (BUF));          if (_size >0) {buf[_size]= ' \ n ';        printf ("%s\n", buf);          } else {printf ("Child is empty or exit");        Break    }}} return 0; }

Operation Result:

Iv. Limitations of pipelines

Two processes with one pipeline can only achieve single-process communication, one to write a read, can not read and write at the same time, if you want the parent process to write, the child process to read, you must re-open another pipeline. The read-write side of the pipeline is passed through an open file descriptor, so two processes must inherit the file descriptor from the parent process. Only two of the processes can access the same pipeline before they can communicate.

V. 4 Special cases of pipelines

(1) If the file descriptor pointing to the end of the pipe is closed, but there is still a process reading the data from the read end of the pipeline, the remaining data in the pipeline is read again, and read again returns 0.

(2) If there is a file descriptor pointing to the write end of the pipe is not closed, and the process that holds the pipe write end does not write data to the pipeline, then there is a process from the pipe read the data, then the remaining data in the pipeline is read, again read will block until there is data readable in the pipeline to read the data and return.
(3) If all the file descriptors pointing to the pipe read end are closed, then there is a process to write to the pipeline, then the process receives a signal sigpipe, which usually causes the process to terminate unexpectedly.
(4) If all the file descriptors pointing to the pipe read end are not closed, but do not read the data from the pipe, this is if the process writes the data to the pipe, then when the pipeline is full, then the write will be blocked until there is an empty position in the pipeline to write the data and return.

The disadvantage of the pipeline is that there is no name, therefore, can only be used to communicate between related processes, in order to solve this problem, and introduced a named pipe (FIFO), named pipe provides a path name associated with it, stored in the file system as a FIFO file, so as long as access to the road, Communication between processes can be achieved. FIFO always works according to the first-in-first-out principle, and it is firstly written.
First, the creation of named pipes
To create a FIFO with the MKFIFO function

int Mkfifo (const char *path,mode_t mode);

Ii. How to use Named Pipes
Call Open () and open it in read-only mode, opening write as write (o_wronly)

Third, the name of the pipeline program code:
Client Side

#include <stdio.h>   #include <error.h>   #include <stdlib.h>   #include <string.h>   #include <fcntl.h>   #include <sys/types.h>   #include <sys/stat.h>   #include < unistd.h>   #define _PATH_ "./.tem"   int main ()   {     int fd=open (_path_,o_rdonly);     if (fd<0)     {       printf ("Open error");     }     Char buf[1024];     memset (buf, ' n ', sizeof (BUF));     while (1)     {      int ret =read (fd,buf,sizeof (BUF));       if (ret>0)       {         buf[ret]= ' \ n ';         printf ("Serve said:%s\n", buf);       }     }     Close (FD);      return 0;   }

Serve end

#include <stdio.h>  #include <stdlib.h>  #include <string.h>  #include <fcntl.h>  #include <sys/types.h>  #include <sys/stat.h>  #include <unistd.h>  #define _path_ "./.tem"  int main ()   {     umask (0);     int ret = MKFIFO (_path_,s_ififo |0666);     if (ret<0)     {       printf ("Mkfifo error\n");       return 1;      }     int Fd=open (_path_,o_wronly);     if (fd<0)     {       printf ("Open error");     }     Char buf[1024];     memset (buf, ' n ', sizeof (BUF));     while (1)     {        printf ("iput\n");        scanf ("%s", buf);        int ret =write (Fd,buf,strlen (BUF) +1);        if (ret<0)        {          printf ("Write error\n");          break;        }      }      Close (FD);       return 0;    }

Operation Result:

The above is the Linux-interprocess communication-the content of the pipeline, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.