Fork Generation Sub-process communication using pipe pipe

Source: Internet
Author: User

http://siqun.blog.163.com/blog/static/213496001201341231121720/

Reprint Link: HTTP://HI.BAIDU.COM/HJ11YC/ITEM/9A2EA30CCA773077BFE97EFC
Note: Add a little bit of content


interprocess communication Fork Pipe pie_t usage (Pipeline mechanism communication)

Each process has a different user address space, the global variables of any one process can not be seen in another process, so the process to exchange data between the kernel, the kernel to open a buffer, process 1 data from the user space to the kernel buffer, process 2 and then read the data from the kernel buffer, This mechanism provided by the kernel is called interprocess communication (ipc,interprocess communication). As shown in.

Figure 30.6. Inter-process communication

4.1. Pipeline Please review
Piping is the most basic IPC mechanism, created by the pipe function:

Call the pipe function in the kernel to open a buffer (called a pipe) for communication, it has a read end of a write end, and then passed through the Filedes parameter to the user program two file descriptor, Filedes[0] point to the read end of the pipe, filedes[1] point to the write end of the pipeline (very good remember, Just like 0 is standard input 1 is the same as standard output). So the pipeline in the user program looks like an open file, through Read (Filedes[0]), or write (filedes[1]); Reading and writing data to this file is actually reading and writing the kernel buffer. The pipe function call successfully returned 0, and the call failed to return-1.

The two file descriptors in int pipe (int filedes[2]) are mandatory filedes[0] can only point to the read end of the pipe, if the write operation will cause an error, filedes[1] can only point to the write end of the pipeline, if the read operation will cause an error
How can communication between two processes be realized after the pipeline is opened? For example, you can follow the steps below to communicate.

Figure 30.7. Pipeline

1. The parent process calls pipe to open a pipeline and gets two file descriptors pointing at both ends of the pipeline.
2. The parent process calls fork to create a child process, and the child process also has two file descriptors pointing to the same pipe.
3. The parent process closes the pipe read end, and the child process closes the pipe write end. The parent process can write to the pipeline, the child process can be read from the pipeline, the pipeline is implemented by the ring queue, the data flows from the writing end flow from the reading end, so that the process of communication between.

Note: Never understand why the parent process here to shut down the pipe read end, and the child process to close the pipe write end, think for a long time finally figured out ..., for the following reasons: Because this program is to simulate the parent process and the child process of the pipeline read and write operations, where the parent process to write data to the pipeline, The child process is used to read data to the pipeline, so start to close the parent process's read file descriptor Filedes[0], and close the child process's write file descriptor filedes[1], which is to simulate this process.

Then as to why the parent process closes the read file descriptor of the pipe filedes[0] the child process can also read the data of the pipeline because the system maintains a count of the file descriptor of a file, the parent-child process each has a file descriptor pointing to the same file, and when a file descriptor is closed, the corresponding count is reduced by one, When this count is reduced to 0 o'clock, the file is closed, so although the parent process closes its file descriptor Filedes[0], the file descriptor count is not equal to 0, so the child process can also be read. It can also be understood that both the parent and child processes have their own file descriptors, so although Filedes[0] is closed in the parent process, filedes[0 in the child process does not affect


Each item in the File table maintains a reference count that identifies how many file descriptor (FD) references the table entry will be deleted when the reference count is 0. So calling Close (FD) closes the file descriptor of the child process, only reduces the reference count, but does not cause the file table entry to be purged, so the parent process can still access

Finally, it is important to note that, under the pipe pipeline of Linux, when writing data on the writing end, the buffer file of the read end is not required (i.e., the file descriptor count of 0 is not required), but the buffer file of the writing end must be closed before reading the data. The write-side file descriptor count is 0) before the data can be read.

Example 30.7. Pipeline

There are some restrictions on using pipelines:
? Two processes can only achieve one-way communication through a pipeline, such as the above example, the parent process write sub-process read, if it is sometimes necessary for the child process to write the parent process read, you must open another pipeline. Ask the reader to think, if only one pipeline, but the parent process does not shut down the read end, the child process does not shut down the write end, both sides have read and write end, why can not achieve two-way communication?
? The read-write side of the pipeline is passed through an open file descriptor, so the two processes to communicate must inherit the pipe file descriptor from their common ancestor. The above example is the parent process to pass the file descriptor to the child process communication between the parent and child processes, can also fork two times the parent process, the file descriptor to two sub-processes, and then two sub-processes to communicate, in short, you need to pass the file descriptor by fork so that two processes can access the same pipeline, they can communicate.
There are 4 special cases that you need to be aware of in the pipeline (assuming all blocking I/O operations and not setting the O_NONBLOCK flag):
1. If all the file descriptors that point to the pipe write end are closed (the reference count for the pipe write is equal to 0), and there is still a process reading the data from the read end of the pipeline, the remaining data in the pipeline is read, and read again returns 0, just like the end of the file.
2. If there is a file descriptor pointing to the end of the pipe is not closed (the reference count of the pipe write end is greater than 0), and the process that holds the pipe write end does not write data to the pipeline, then there is a process reading the data from the pipe, then the remaining data in the pipeline is read, again read will block, The data is read and returned until the data in the pipeline is readable.
3. If all the file descriptors pointing to the pipe read end are closed (the reference count for the pipe read is equal to 0), then there is a process to write to the pipeline, then the process receives the signal sigpipe, which usually causes the process to terminate unexpectedly. In the 33rd chapter the signal will talk about how to make the sigpipe signal do not terminate the process.
4. If there is a file descriptor pointing to the read end of the pipe is not closed (the reference count of the pipe read is greater than 0), and the process that holds the pipe read does not read the data from the pipe, then there is a process to write the data to the pipeline, then write again when the pipe is full and then block until there is empty position in
These four special cases of pipelines have universal significance. In the 37th chapter of the socket programming the TCP socket to speak also has these features of the pipeline.

Fork Generation Sub-process communication using pipe pipe

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.