Linux Study Notes (7)

Source: Internet
Author: User
Linux Study Notes (7)-Linux general technology-Linux programming and kernel information. The following is a detailed description. 11. elementary learning of Pipelines
MPs queue-special files, which are independent and only exist in memory
-- Process generated by the same process (Same Origin)
-- Half-duplex communication is adopted. Only one party can transmit data at a time.
Low-level MPs queue
Use the pipe function to create a low-level pipeline. After the pipeline is created, two file descriptors, pipe_fd [0] And pipe_fd [1], are generated. Pipe_fd [0] is used to read data from the pipeline, and pipe_fd [1] is used to write data to the pipeline (read and write functions are used to read and write data ).
Pipeline Data Flow Diagram
[Table = 98%] [tr] [td] MPs queue

[/Td] [/tr] [/table] [table = 98%] [tr] [td] fd [1]

[/Td] [/tr] [/table] [table = 98%] [tr] [td] fd [0]

[/Td] [/tr] [/table] [table = 98%] [tr] [td] Pipeline

[/Td] [/tr] [/table] [table = 98%] [tr] [td] fd [1]

[/Td] [/tr] [/table] [table = 98%] [tr] [td] fd [0]

[/Td] [/tr] [/table]
Common functions in pipeline operations

Pipe
Create an unknown MPs queue

Popen
Create a standard Stream pipeline

Mkfifo
Determine whether to detect or change the signal shielding word

Pipe Function Description
[Table] [tr] [td = 115,] function header file
[/Td] [td = 453,] # include
[/Td] [/tr] [tr] [td = 115,] function prototype
[/Td] [td = 1,1, 453] int
Pipe (int files [2])
[/Td] [/tr] [tr] [td = 115,] function return value
[/Td] [td = 453,] 0 is returned for successful execution, and-1 is returned for unsuccessful execution
[/Td] [/tr] [/table] programming: To design a program, you must create a pipeline, copy the process, write data to the parent process, and read data from the child process.

Flowchart


[Table = 98%] [tr] [td] creating an MPS queue

[/Td] [/tr] [/table] [table = 98%] [tr] [td]

[/Td] [/tr] [/table] [table = 98%] [tr] [td] fork () create a sub-process

[/Td] [/tr] [/table] [table= 98%] [tr] [td] if

[/Td] [/tr] [/table] [table = 98%] [tr] [td] disable fd [0]

[/Td] [/tr] [/table] [table = 98%] [tr] [td] write data to the pipeline

[/Td] [/tr] [/table] [table = 98%] [tr] [td] disable fd [1]

[/Td] [/tr] [/table] [table = 98%] [tr] [td] wait until the sub-process ends

[/Td] [/tr] [/table] [table = 98%] [tr] [td] Ends

[/Td] [/tr] [/table] [table = 98%] [tr] [td] disable fd [1]

[/Td] [/tr] [/table] [table = 98%] [tr] [td] read data from the pipeline

[/Td] [/tr] [/table] [table = 98%] [tr] [td] disable fd [0]

[/Td] [/tr] [/table] [table = 98%] [tr] [td] exit

[/Td] [/tr] [/table] 0 parent process "fitpath =" t "trim =" t ">


Result = fork ();
/* Creat subprocess */



If (result <0) {printf ("ERROR"); exit (0);}/* failture */

Else if (result = 0)

{Close (pipe_fd [1]);

If (r_num = read (pipe_fd [0], buf_r, 100)> 0)

Printf ("subprocess readed % d character from pipe, context is % s \ n", r_num, buf_r );

Close (pipe_fd [0]);

Exit (0 );

}


/* Father process */

Else

{

Close (pipe_fd [0]);

If (write (pipe_fd [1], "hello, world! ", 20 )! =-1)

Printf ("input string is 'hello, world' \ n ");



Close (pipe_fd [1]);

Waitpid (result, NULL, 0 );


Exit (0 );

}

}

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.