interprocess communication----pipe, FIFO

Source: Internet
Author: User


Process is a separate unit, each process has a different address space, the information of any one process is not visible in another process, so to exchange data between two processes must pass through the kernel.


Pipeline (pipe) is a kind of intermediate medium that realizes inter-process communication. It refers to a file that is used to connect a read process and a write process to achieve communication between them. So pipelines are used for one-way communication between processes.

Pipelines are divided into anonymous pipes and named pipes.

Anonymous Pipelines : Mainly used for communication between parent-child processes, or sibling processes, and other inter-related processes. Because only two processes with relative relationships can share a single pipeline. exists in memory.

Function:

#include <unistd.h>

int pipe (int filedes[2]);

Characteristics:

1. Is unidirectional, that is, two processes have only one read and write end, can not be both read and write end.

2. Pipeline communication is a flow-oriented service. That is, it is written as a stream of characters.

3. After the data has been read out of the pipeline by the process, the data does not exist in the pipeline.

4. When the process goes to read the empty pipeline, the process blocks.

5. The process is blocked when the process is writing data to a full pipeline.

6. Life cycle with process.

7. Can only be used locally, not for the network.

8. Asynchronous read and write operations are not supported.

Named Pipes (FIFO): used primarily for communication between two two processes that are not related to each other. It provides a path name for a file. As long as two processes can access this path, communication can be achieved. exists on the hard disk

The main functions used are:

#include <sys/types.h>

#include <sys/stat.h>

int Mknod (const char *path,mode_t mod,dev_t dev);

int Mkfifo (const char *path,mode_t mode);//Create a named pipe.

Characteristics:

1. Both one-way communication and two-way communication can be realized.

2. It can be referenced by its path.

3. Support multi-Client connection.

4. Can be used either locally or on a network.

5. Supports asynchronous overlapped I/O operations.

 6. You can transfer data as a stream, and you can also gather data into a block of data called a message.

   1  #include <stdio.h>  2  #include <string.h>  3  #include <stdlib.h>  4  #include <unistd.h>  5 int main ()    6 {  7     int filedes[2]={0};  8      if (Pipe (filedes) ==-1)   9     { 10          perror ("pipe"); 11          exit (1);  12     } 13     pid_t pid  = fork ();  14     if (pid==-1)  15     {  16         perror ("fork"); 17          exit (1); 18     } 19      else if (pid == 0)  20     { 21         / /child 22         close (Filedes[0]); 23          int count = 10; 24          while (count--)  25         { 26              char buf[]= "hello  World ";  27             ssize_t _ Size = write (filedes[1],buf,sizeof (BUF)); 28              if (_size<0)  29              { 30                  perror ("write"); 31                  exit (1);  32             }  33             sleep (1); 34          } 35     } 36      else 37     { 38          //father; 39         close (Filedes[1]);  40         char buf[1024]; 41          int count = 5; 42          while (1)  43         { 44       &nbsP;      memset (buf, ' + ', sizeof (BUF)); 45              ssize_t _size = read (filedes[0],buf,sizeof (BUF) );  46             if (_size<=0)   47             { 48                  perror ("read");  49                  exit (1);  50             } 51              else 52              { 53              &nBsp;   buf[_size]= '; 54                  printf ("%s\n", buf); 55                  sleep (1); 56              } 57  58          } 59  60     } 61      return 0; 62 }
  client.c  1  #include <stdio.h>  2  #include <stdlib.h>   3  #include <sys/types.h>  4  #include <sys/stat.h>  5  #include <fcntl.h>  6  #include <string.h>  7  #include <unistd.h>   8 int main ()   9 { 10      umask (0);  11      if (Mkfifo ("./.tmp", s_ififo | 0666) ==-1)  12       { 13         perror ("Mkfifo");  14          exit (1); 15       } 16     int op = open ("./.tmp", O_WRONLY);  17      if (op<0)  18     { 19         &Nbsp;perror ("open");  20         exit (1); 21      } 22     char buf[1024]; 23      while (1)  24     { 25          memset (buf, ' n ', sizeof (BUF));  26         fflush (stdout );  27         printf ("please say#       ")  28         gets (BUF); 29     31         ssize_t _size = write (OP, Buf,strlen (BUF) +1);  32         if (_size<0)  33          { 34             &nbSp;perror ("write");  35             exit ( 1);  36         } 37     }  38         return 0; 39 }   server.c  1  #include <stdio.h>  2  #include <stdlib.h>  3   #include <sys/types.h>  4  #include <sys/stat.h>  5  #include < fcntl.h>  6  #include <string.h>  7 int main ()   8 {   9     int op = open ("./.tmp", O_RDWR); 10      if (op<0)  11     { 12          perror ("open");  13         exit (1);  14    &nbSP;}  15     char buf[1024]; 16     while (1)   17     { 18         memset (buf, ' + ') , sizeof (BUF));  19         fflush (stdout); 20          ssize_t _size = read (op,buf,sizeof (BUF));  21         if (_size<=0)  22          { 23              printf ("read end or error\n"); 24              break; 25         } 26          else 27          { 28&nbsP;            buf[_size]= '; 29              printf ("client say# %s\n", buf);  30         } 31     } 32      return 0; 33 }   operation Result:  [[email protected]  fifo]$ ./client please say#     nihao please say#      hello please say#     abhuuh    [[email protected] fifo]$ ./server client say# nihao client say#  hello client say# abhuuh

interprocess communication----pipe, FIFO

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.