1. One of the simpler methods of interprocess communication is pipeline operation
/* ============================================================================ Name : Test.c Author : Wangchuan Version : Copyright : Your copyright notice Description:hello World in C, Ansi-style ================= =========================================================== * * #include <stdio.h> #include <stdlib.h># Include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h>int main ( int argc,char* argv[]) {int Fd[2],cld_pid,len;char buf[200];if (pipe (FD) = =-1) {perror ("Error creating Pipeline"); exit (1);} if ((Cld_pid=fork ()) = = 0) {close (fd[1]); len = Read (fd[0],buf,sizeof (BUF)); Buf[len] = 0;printf ("The data that the child process reads from the pipeline is:%s", buf) Exit (0);//End Child process}else{close (fd[0]); sprintf (BUF, "parent process creates this data for child processes (pid=%d)", cld_pid); write (Fd[1],buf,strlen (BUF)); Exit (0);//end parent process}return 0;}
Compile the execution output such as the following:
The data that the child process reads from the pipeline is: the parent process creates the data for the child process (pid=7309)
Linux Learning: interprocess communication-pipelines