Pipeline of interprocess communication

Source: Internet
Author: User

Piping (pipe) is a way of interprocess communication, with the following demo:

#include <sys/types.h>#include<sys/wait.h>#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<string.h>#defineMAXLINE 4096intMainintargcChar**argv) {    intN; intfd[2];    pid_t pid; CharLine[maxline]; if(Pipe (FD) <0) {perror ("Pipe Error"); Exit (1); }    if(PID = fork ()) <0) {perror ("Fork Error"); Exit (1); } Else if(PID >0) {Close (fd[0]); Write (fd[1],"Hello world\n", A); } Else{Close (fd[1]); N= Read (fd[0], line, MAXLINE);    Write (Stdout_fileno, line, N); } exit (0);}

Features of the piping:

1. The pipe is half-duplex (data can only flow in one direction)

2. Pipelines can only be used between two processes with a common ancestor

For pipelines from the parent process to the child process, the parent process closes the read end of the pipe fd[0], and the child process closes the write end fd[1].

For pipes from child process to parent process, parent process closes fd[1], child process closes fd[0].

Pipeline of interprocess 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.