Linux C Learning Note 07--Pipeline communication

Source: Internet
Author: User

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

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.