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