Linux Pipeline Learning (i)

Source: Internet
Author: User
Tags function prototype

Recently learned pipeline pipe, here to summarize.

Put a piece of your own real code here

structnode{intA;Longb;};intMain () {intfield[2];    pid_t pid; Charbuf[ the]; intReturned_count;    Pipe (field); //fcntl (field[0], F_SETFL, o_nonblock);    intstatus; PID=Fork (); if(PID <0) {printf ("Error in fork\n"); Exit (1); }    if(PID = =0) {printf ("In child process\n"); Close (field[0]);        Node Testnode; Testnode.a=1; testnode.b=2; //sleep (10); //write (field[1], "This was a pipe test\n", strlen ("This is a pipe test"); //write (field[1], "This was another pipe test\n", strlen ("This is another pipe test");Write (field[1],&testnode,sizeof(Testnode)); Testnode.a=Ten; testnode.b= -; Write (field[1],&testnode,sizeof(Testnode)); Exit (0); }    Else{printf ("In Parent process\n"); Close (field[1]); //Read (field[0],buf,sizeof (BUF));node Ptestnode; Read (field[0],&ptestnode,sizeof(node)); //printf ("Msg%s from child\n", buf);printf"MSG from child node a=%d, b=%ld\n", ptestnode.a,ptestnode.b); Read (field[0],&ptestnode,sizeof(node)); printf ("MSG from child node a=%d, b=%ld\n", ptestnode.a,ptestnode.b); //waitpid (pid,&status,0);} close (field[1]);}

Pipe as a common means of communication in Linux process is widely used, function prototype is int pipe (int filedes[2]); Where Filedes in the filedes[0] represents read Filedes[1] representative write.

If you do not use the FCNTL function to qualify, the pipeline will be blocked by default.

For example, when the parent process uses the Read function to read the contents of the pipeline, if the pipeline is empty, the read function blocks the wait. If the pipe is set to read non-blocking, the parent process will not read the pipeline content directly to the next step, no longer waiting.

However, if the wait or Waitpid function is used in the parent process, I find that the implementation is still the same as when the pipeline is blocked, waiting for the child process to write and read the contents before proceeding to the next step, which needs to be studied.

The return value of read determines whether the contents of the pipeline are read.

For example

1 node Ptestnode;2         intBufcount =0;3Bufcount = Read (field[0],&ptestnode,sizeof(node));4         //printf ("Msg%s from child\n", buf);5printf"MSG from child node a=%d, b=%ld\n", ptestnode.a,ptestnode.b);6 7          while(Bufcount)8         {9Bufcount = Read (field[0],&ptestnode,sizeof(node));Ten             if(!bufcount) One             { A                  Break; -             } -printf"MSG from child node a=%d, b=%ld\n", ptestnode.a,ptestnode.b); the         } -  -printf"Pipe Read over\n");

Linux Pipeline Learning (i)

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.