The design of nameless pipeline pipe under Linux

Source: Internet
Author: User
Tags define function error code printf

1. Function description

Pipe (Establish pipeline):

1) header file #include

2) define function: int pipe (int filedes[2]);

3 Function Description: Pipe () creates the pipe and returns the file descriptor from the parameter Filedes array.

Filedes[0] is the read end of the pipe

FILEDES[1] is the write end of the pipe.

4) return value: If successful, return zero, otherwise return-1, the reason for the error in errno.

Error code:

Emfile process has run out of file descriptive words

The Enfile system has no file descriptors available.

Efault parameter Filedes array address is not valid.

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 Entrance

11 * * * * *

int main ()

13 {

int pipe_fd[2];

pid_t pid;

Char buf_r[100];

char* P_wbuf;

int r_num;

19

memset (buf_r,0,sizeof (buf_r));

21st

22/* Create Pipeline * *

if (pipe (PIPE_FD) <0)

24 {

-printf ("Pipe Create Errorn");

return-1;

27}

28

29/* Create CHILD process * *

The If ((Pid=fork ()) ==0)//child Process Execution sequence

31 {

printf ("n");

Close (pipe_fd[1]);//subprocess closes the write end of the pipe first

Sleep (2); /* Let the parent process run first so that the parent process writes the child process before the content reads */

if ((R_num=read (pipe_fd[0],buf_r,100)) >0)

36 {

Notoginseng printf ("%d numbers read from the pipe is%sn", r_num,buf_r);

38}

Close (pipe_fd[0]);

+ exit (0);

41}

Pid>0///parent Process Execution sequence

43 {

(Pipe_fd[0]); The parent process closes the read end of the pipe first

(Pipe_fd[1], "Hello", 5)!=-1)

-printf ("Parent write1 hello!n");

if (write (pipe_fd[1], "pipe", 5)!=-1)

-printf ("Parent write2 pipe!n");

Close (pipe_fd[1]);

Wait (NULL); /* Wait for child process to end * *

Wuyi exit (0);

52}

0;

54}

55

56

root@wl-ms-7673:/home/wl/Desktop/c++# g++ pipe_test.cpp-o pipe_test

root@wl-ms-7673:/home/wl/Desktop/c++#./pipe_test

Parent Write1 hello!

Parent Write2 pipe!

Numbers read from the pipe is Hello pipe

root@wl-ms-7673:/home/wl/Desktop/c++#

The nameless pipe was created before fork was created, by creating a pipe through pipe () and then creating a subprocess through fork, the child process copies the code snippet/data segment and stack segment of the parent process, so the created pipe is copied, one copy of the child process, one copy of the parent process, in order for the pipe to communicate normally, You must handle an existing pipeline.

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.