Inter-process Pipeline communication

Source: Internet
Author: User

Communication between parent and child processes through pipelines.

Pipelines can only be used between two processes with a common ancestor, typically one pipeline is created by another, and after the process calls fork, the pipeline can be used between the parent and child processes.


The general process, fd[0] means reading, fd[1] means writing.

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/9C/30/wKiom1ltchCiG0YMAAAgFhIvE8s371.png-wh_500x0-wm_ 3-wmp_4-s_3288712991.png "title=" 1.png "width=" "height=" 231 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:250px; height:231px; "alt=" Wkiom1ltchcig0ymaaagfhive8s371.png-wh_50 "/>

If it is useful to the pipe, it is written into the pipe and read from the pipe.

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/9C/30/wKioL1ltclexXUrvAAAYiLho8kc484.png-wh_500x0-wm_ 3-wmp_4-s_3170042953.png "title=" 2.png "width=" "height=" 397 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:250px; height:397px; "alt=" Wkiol1ltclexxurvaaayilho8kc484.png-wh_50 "/>

Between the parent and child processes.

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/9C/2F/wKioL1ltcGnC7OlnAABE-8UP3FA247.png-wh_500x0-wm_ 3-wmp_4-s_2178977751.png "title=" Untitled picture.png "alt=" Wkiol1ltcgnc7olnaabe-8up3fa247.png-wh_50 "/>

Therefore, through the simple read, write function, the target file is set to Fd[0] fd[1] that can complete the communication between the parent-child process.

write usage, read similar.
int write (int handle, void *buf, int nbyte);
Handle isFile descriptor;
BUF is the specified buffer, which is thePointer, pointing to a section of the internal deposit cells;
Nbyte is to writeFileThe specified number of bytes; return value: Number of bytes written to the document (success); 1 (Error)
The Write function writes BUF nbyte to the document referred to in the file descriptor handle, returns the number of bytes written when successful, and returns -1 on error.

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

#define BUFSZ 256


int main (void)

{

int fd[2];

Char Buf[bufsz];

pid_t pid;

int Len;

if (pipe (FD) <0)

{

Perror ("Failed to pipe");

Exit (1);

}

if ((PID = fork ()) <0)

{

Perror ("Failed to fork");

Exit (1);

}

else if (PID > 0)


{

printf ("father wrote password to pipe\n");

Write (fd[1], "1234\n", 25); The parent process writes the password into sleep, waiting for the child process to read.

Sleep (1);

Read (Fd[0], buf, BUFSZ);

printf ("Father read result is%s\n", buf);

Exit (0);

}

Else

{

printf ("Child would read from pipe\n");

Read (Fd[0], buf, BUFSZ); The child process reads the password from the pipeline that the parent process writes into

printf ("Child read result is%s\n", buf);

printf ("Child wrote password to pipe\n");

Write (fd[1], "4321\n", 25); The child process writes its own password, waiting for the parent process to read

}

return 0;

}


The results of the operation are as follows:

[Email protected] xcui]#./pipe

Father wrote password to pipe

Child would read from pipe

Child read result is 1234


Child wrote password to pipe

Father Read result is 4321


Inter-process Pipeline communication

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.