Linux Learning: interprocess communication-pipelines

Source: Internet
Author: User

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

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.