In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shares with you the content of a sample analysis of the Linux pipeline. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Pipe is the earliest means of inter-process communication. When executing a command in shell, the output of the previous command is often used as the input of the next command, and multiple commands work together to accomplish one thing. The function of the pipeline is to send messages between related processes, because the common master process has called the pipe function, and the open pipeline file will be shared by all descendant processes after fork, and the open pipeline can be written by one process and read by another related process.
Strictly speaking, multiple processes in the family can throw messages into the same secret place, or they can all get messages from the same secret place, but it is risky to do so. The pipeline is essentially a byte stream, not the message mentioned earlier, and there is no message boundary. If the byte streams sent by multiple processes are mixed together, the respective content cannot be recognized. So it is usually two related processes that use pipes to communicate.
Generally speaking, the data flow in the process is unidirectional and burns immediately after reading. A pipe is a file that can be manipulated by calling interfaces to manipulate files such as read, write, and close. On the other hand, pipe is not an ordinary file, it belongs to a unique file system: pipefs. The essence of the pipe is that the kernel maintains a buffer associated with the pipe file, and the operation on the pipe file is converted by the kernel into the operation of this buffer memory.
Create a pipeline function under Linux:
Int pipe (int pipefd [2])
After a successful call to the pipe function, two open file descriptors are returned, one is the pipe's read-side descriptor pipefd [0], and the other is the pipe's write-side descriptor pipefd [1]. The pipe has no file name associated with it, so the program has no choice but to access the pipe through the file descriptor, and only processes that can see the two file descriptors can use the pipe. If the process performs a fork operation, the pipeline becomes as follows:
Generally speaking, pipes are used in one way, so after fork, one of the new and old processes will close write fd and the other will close read fd to avoid confusion between reading and writing. This is the case with executing pipe commands in shell, with only a slight exception in that the pipe descriptor occupies both standard input and standard output file descriptors.
One-way use of pipes, that is, processes that do not use pipes should release the pipe file fd as soon as possible, not only to make the flow of data clearer, not just to save file descriptors, but also because closing unused pipe file descriptors has a significant impact on the proper use of pipes.
Pipes have the following three characteristics:
Calling the read function on the reader-side descriptor will return 0 (that is, read the EOF flag) only if all the write-side descriptors are turned off and the data in the pipe is read.
If all read-side descriptors are turned off and the process writes data into the pipeline again, the write operation fails, errno is set to EPIPE, and the kernel sends a SIGPIPE signal to the write process.
When all read and write ends are closed, the pipe can be destroyed.
The pipe is essentially a memory area with a default size of 65536 bytes, but you can call fcntl to get and modify the size of this value. Therefore, in the process of using the pipeline, we should pay attention to the problem of whether the write data can be consumed in time. Once the pipeline is full, the write will be blocked; for the reader side, it is necessary to read in time to prevent the pipeline from being full, resulting in write blocking.
All of the above are common forms of pipes, that is, anonymous pipes, which can only be used between related processes because there are no physical files associated with them. So there is another form of pipe, named pipe, the biggest difference is that there is an entity file associated with it. Due to the existence of physical files, unrelated and unrelated processes can also communicate with each other by using FIFO.
The named pipe function is created as follows:
Int mkfifo (const char * pathname, modt_t mode)
Named pipes support the same pipe operations as anonymous pipes, except that it has an associated file with a file name, so that any process can use it through the file name.
Thank you for reading! This is the end of this article on "sample Analysis of Linux Pipeline". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.