Linux inter-process communication---pipeline

Source: Internet
Author: User

IPC:IPC, i.e. inter-process communication, interprocess communication. is an inter-process communication object, including pipelines, message queues, semaphores, sockets, and so on. With respect to the IPC structure, the IPC structure is first maintained by the kernel and does not belong to a particular process. The IPC structure is identified by two things: an identifier (ID) and a key. Where the ID is the internal name of the IPC and is used only in interprocess communication; key is the external name of the IPC, and when multiple processes call the Get function for the same key, the same ID is obtained, that is, the same IPC is identified, and the process can communicate through this common IPC. Nameless Pipe:A nameless pipe can only be used between two processes with a common ancestor, namely:can only be used for related two process communication, cannot be used for unrelated two process communication. Common implementation functions are: int pipe (int fd[2]). The pipe function creates a half-duplex pipe. FD[0] for the process read-end, fd[1] for the process write end, one process's fd[1] output is another process fd[0] input. The implementation routines are as follows:
#include <stdio.h>#include<stdlib.h>#include<sys/types.h>#include<unistd.h>intMain () {intfd[2]; CharLine[max_line]; if(Pipe (FD) <0);//build Pipelines, fd[1] write, fd[0] read. Returns 0 for success, 1 for failureErr_sys ("Pipe Erroe"); if(PID = fork ()) <0)//Create Child processErr_sys ("Fork Error"); Else if(PID >0)//Parent Process{Close (fd[0]);//Close the read side of the parent processWrite (fd[1],"Data",4);//The parent process writes to the fd[1] descriptor     }     Else  //Child Process{Close (fd[1]);//Close the write end of the child process        intn = Read (fd[0], line, max_line);//child process read from fd[0] DescriptorWrite (Stdout_fifeno, line, N);//writes the read to standard output (cout)} exit (0);}

Pros and cons of using nameless pipes: Advantages: Relative to the establishment of a disk file, two processes through disk read and write to communicate the method, using nameless pipe does not need to create intermediate temporary disk files, increase the speed of inter-process communication. Cons: Nameless pipes can only be used between two processes with a common ancestor, that is, two processes can only be parent-child processes or two child processes with a common parent process.

known pipeline (FIFO):A nameless pipe can only be used between two related processes (two processes have a common ancestor), but through FIFO,Unrelated processes can also exchange data for communication。 The commonly used implementation function is MKFIFO (path, mode), which creates a FIFO, similar to creating a file, path name is located in the file system. The use of FIFIO is similar to CS communication, Write_fifo is equivalent to the client, to the pre-known pipeline to send messages, Read_fifo equivalent to the server side, create a well-known pipeline, monitoring the reading of the pipeline, when there is data to read and processing. The implementation routines are as follows: (1) Read_fifo:
#include <stdio.h>#include<stdlib.h>#include<errno.h>#include<fcntl.h>#include<sys/stat.h>intMain () {intFD; intLen; Charbuf[1024x768]; if(Mkfifo ("Fifo1",0666) <0&& errno!=eexist)//creating a FIFO pipelinePerror ("Create FIFO Failed"); if(FD = open ("Fifo1", o_rdonly)) <0)//open FIFO with Read{perror ("Open FIFO Failed"); Exit (1); }      while(len = Read (FD, BUF,1024x768)) >0)//Read FIFO pipelineprintf"Read message:%s", BUF);   Close (FD); //Close FIFO file     return 0;} 

(2) Write_fifo:

#include <stdio.h>#include<stdlib.h>//Exit#include <fcntl.h>//o_wronly#include <sys/stat.h>intMain () {intFD; printf ("I am%d process.\n", Getpid ());//Description Process ID         if(FD = open ("Fifo1", o_wronly)) <0)//to write open a FIFO{perror ("Open FIFO Failed"); Exit (1); }    Charbuf[Ten] ="Hello,fifo"; if(Write (FD, buf, One) <0)//write to FIFO{perror ("Write FIFO Failed");         Close (FD); Exit (1); } Sleep (2);//Sleep 2 secondsClose (FD);//Close FIFO file    return 0;}

Linux inter-process communication---pipeline

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.