Pipeline communication is a way of interprocess communication and is easy to use, but the limitation is the communication between the parent and child processes, and the following is the debug code.
The program parent process creates 2 pipelines for the parent process to write the data--the child process read data and the child process write data--the parent process reads the data:
#include <errno.h>#include<fcntl.h>#include<unistd.h>#include<stdio.h>intMainintargcChar*argv[]) { intfprcw[2];//For parent Read,child write intfpwcr[2]; Charstr[ the]; pid_t pid; if(pipe (FPRCW) = =-1) {perror ("Pipe1"); return-1; } if(pipe (FPWCR) = =-1) {perror ("pipe2"); Close (fprcw[0]); Close (fpwcr[0]); return-1; } PID=Fork (); if(PID = =0) {Close (fprcw[0]); Close (fpwcr[1]); Sleep (1); Write (fprcw[1],"Hello world!", A); Read (fpwcr[0],STR, the); printf ("Child str is:%s\n", str); Close (fpwcr[0]); Close (fprcw[1]); } Else if(PID >0) {Close (fpwcr[0]); Close (fprcw[1]); Write (fpwcr[1],"How is you!", A); if(Waitpid (Pid,null,0) == -1) {perror ("Waitpid"); } Read (fprcw[0],STR, the); printf ("Parent STR is:%s\n", str); Close (fpwcr[1]); Close (fprcw[0]); } Else{perror ("Fork"); } return 0;}
Linux C Learning Note 07--Pipeline communication