Process Communication (Pipeline one-way communication)

Source: Internet
Author: User

 1  #include <stdio.h> 2#include<sys/wait.h> 3#include<unistd.h> 4# Include<string.h> 5#include<stdlib.h> 6int main ()  7 { 8     int pipefd[2]; 9    if (Pipe (PIPEFD) ==-1)  10     { 11     perror ("pipe"); 12     return  -1; 13    } 14   pid_t pid=fork (); 15    if (pid<0)  16    { 17      perror ("pid");  18      return -1; 19    } 20    else if (pid==0)  21    { 22        Close (pipefd[0]);  23       char buf[20]= "We are young";  24  &Nbsp;     int i=0; 25       while (i<  26     { 27       write (Pipefd[1], Buf,strlen (BUF));  28       sleep (1); 29        i++; 30     } 31      exit ( 1);  32    } 33   else 34    { 35      close (pipefd[1]); 36     char buf[200];  37     int j=0; 38     while (j<10)  39      { 40     memset (buf, ' n ', sizeof (BUF));  41      ssize_t size= read (pipefd[0],buf,sizeof (BUF)); 42        iF (size>0)  43       printf ("%s\n", buf); 44       j++; 45     } 46    if (Waitpid (PID, null,0) ==pid)  47    { 48       printf ("Wait  success\n "); 49     } 50    } 51    return 0; 52 }   Makefile: 1 Pipe:pipe.c 2          gcc -o [email protected] $^ 3.phony:clean  4clean: 5         rm -f pipe

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7F/04/wKiom1cPrEKCM4usAALe2p7ArIo504.png "title=" Screenshot (2). png "alt=" Wkiom1cprekcm4usaale2p7ario504.png "/>

This article from "Liveyoung" blog, reproduced please contact the author!

Process Communication (Pipeline one-way communication)

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.