1 Create two sub-processes using the fork function. Sends a message to the second child process in the first child process, and the second child process comes out and processes it.
2 in the parent process, pipe communication is not applicable, so what does not need to be done directly close the two ends of the pipe
3 Code implementation
1#include <unistd.h>2#include <stdio.h>3#include <stdlib.h>4#include <limits.h>5#include <fcntl.h>6#include <sys/types.h>7 #defineBUFSZ Pipe_buf8 voidErr_quit (Char*msg) {9 perror (msg);TenExit1); One } A intMain (void ) - { - intfd[2]; the CharBUF[BUFSZ];/*buffers*/ - pid_t pid; - intLen =0; - if(Pipe (FD)) <0)/*Creating Pipelines*/ +Err_quit ("Pipe" ); - if(PID = fork ()) <0)/*Create first child process*/ +Err_quit ("Fork"); A Else if(PID = =0){/*in a child process*/ atClose (fd[0] );/*to close a file descriptor that is not used*/ -Write (fd[1],"Hello son!\n", the);/*Send Message*/ -Exit0); - } - if(PID = fork ()) <0)/*Create a second child process*/ -Err_quit ("Fork"); in Else if(PID >0){/*in the parent process*/ -Close (fd[0] ); toClose (fd[1] ); +Exit (0 ); - } the Else{/*in a child process*/ *Close (fd[1] );/*to close a file descriptor that is not used*/ $Len = Read (fd[0], buf, BUFSZ);/*reading Messages*/Panax Notoginseng Write (Stdout_fileno, buf, Len); -Exit0); the } +}
4
Using pipelines in Linux for sibling process communication