Using pipelines in Linux for sibling process communication

Source: Internet
Author: User

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

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.