Pipe design for unknown pipelines in Linux

Source: Internet
Author: User
1 function description pipe (pipeline creation): 1) header file include & lt; unistdh & gt; 2) definition function: intpipe (int

1. function description

Pipe (pipeline creation ):
1) header file # include
2) define the function: int pipe (int filedes [2]);
3) function description: pipe () creates an MPs queue and returns the file description words from the filedes array.
Filedes [0] is the read end in the pipeline
Filedes [1] is the write end of the MPs queue.
4) return value: if it succeeds, zero is returned. otherwise,-1 is returned. the error cause is stored in errno.

Error code:
The EMFILE process has used up the maximum number of file descriptions
The ENFILE system does not have a file description word available.
The filedes array address of the EFAULT parameter is invalid.

Example:

Root @ wl-MS-7673:/home/wl/desktop/c ++ # cat-n pipe_test.cpp 1 2 # include
 
  
3 # include
  
   
4 # include
   
    
5 # include
    
     
6 # include
     
      
7 # include
      
        8 # include
       
         9/* 10 * program entry 11 **/12int main () 13 {14int pipe_fd [2]; 15pid_t pid; 16 char buf_r [100]; 17char * p_wbuf; 18int r_num; 19 20 memset (buf_r, 0, sizeof (buf_r); 21 22/* create pipeline */23if (pipe (pipe_fd) <0) 24 {25 printf ("pipe create error \ n"); 26 return-1; 27} 28 29/* create sub-process */30if (pid = fork ()) = 0) // sub-process execution sequence 31 {32 printf ("\ n"); 33 close (pipe_fd [1]); // the sub-process first closes the write end 34 sleep (2) of the pipeline;/* let the parent process run first, in this way, the parent process writes the child process to read the content */35if (r_num = rea D (pipe_fd [0], buf_r, 100)> 0) 36 {37 printf ("% d numbers read from the pipe is % s \ n", r_num, buf_r ); 38} 39 close (pipe_fd [0]); 40 exit (0); 41} 42 else if (pid> 0) // execution sequence of the parent process 43 {44 close (pipe_fd [0]); // the parent process first closes the read end 45if (write (pipe_fd [1], "Hello", 5 )! =-1) 46 printf ("parent write1 Hello! \ N "); 47if (write (pipe_fd [1]," Pipe ", 5 )! =-1) 48 printf ("parent write2 Pipe! \ N "); 49 close (pipe_fd [1]); 50 wait (NULL);/* wait until the sub-process ends */51 exit (0); 52} 53 return 0; 54} 55 56root @ wl-MS-7673:/home/wl/desktop/c ++ # g ++ pipe_test.cpp-o pipe_testroot @ wl-MS-7673:/home/wl/desktop/c ++ #. /pipe_test parent write1 Hello! Parent write2 Pipe! 10 numbers read from the pipe is Hello Piperoot @ wl-MS-7673:/home/wl/desktop/c ++ #
       
      
     
    
   
  
 

Before creating a fork, create an MPs queue by using pipe (), and then create a child process by using fork. then, the child process copies the code segment/data segment and stack segment of the parent process, therefore, the created pipeline will be copied, the child process, and the parent process. to make the pipeline communicate normally, the existing pipeline must be processed.

Related Article

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.