What Is a Duction?

Pipes are devices that are connected by pipes, pipe couplings, valves, etc. and are used to transport gas, liquid or fluid with solid particles. Generally, after the fluid is pressurized by blowers, compressors, pumps and boilers, the fluid flows from the high pressure to the low pressure of the pipeline, and it can also be transported by the pressure or gravity of the fluid itself. Pipes have a wide range of uses, mainly in water supply, drainage, heating, gas supply, long-distance transportation of oil and gas, agricultural irrigation, hydraulic engineering and various industrial installations.

Pipeline is used
1. Classification by materials: metal pipes and non-metal pipes.
2. Classified by design pressure: vacuum pipeline, low pressure pipeline, high pressure pipeline, ultra high pressure pipeline.
3. Classified by conveying temperature: low temperature pipeline, normal temperature pipeline, medium temperature and high temperature pipeline.
4. Classified by conveying medium: water supply and drainage pipes, compressed air pipes, hydrogen pipes, oxygen pipes, acetylene pipes, heat pipes, gas pipes, fuel pipes, highly toxic fluid pipes, toxic fluid pipes, acid and alkali pipes, boiler pipes, refrigeration Pipelines, purified pure gas pipes, pure water pipes.
Pipe-to-pipe diameter determination When the flow rate of the fluid is known, the size of the pipe diameter depends on the allowable flow rate or allowable frictional resistance (pressure drop). When the flow rate is large, the pipe diameter is small, but
Key pipeline concepts
Pipes are one of the original UnixIPC forms supported by Linux, and have the following characteristics: pipes are half-duplex, and data can only flow in one direction; when two parties need to communicate, two pipes need to be established; they can only be used by parent-child processes or brothers Between processes (processes with kinship); separate constitutes an independent file system: a pipe is a file for processes at both ends of the pipe, but it is not an ordinary file, it does not belong to a file system, but A self-contained portal forms a separate file system and exists only in memory. Data read and write: What a process writes to the pipe is read by the process at the other end of the pipe. Writes are added at the end of the pipe buffer each time, and data is read from the head of the buffer each time. [2]
1 pipeline implementation mechanism
A pipe is a shared file that is used to connect a read process and a write process to achieve communication between them, also known as a pipe file. Two types of pipes are implemented in Linux, one is an unnamed pipe, and the other is a named pipe. An unnamed pipe has no disk nodes. It exists only as a memory object and is destroyed when it is used up. Because there is no file name and path, and no disk node, the unnamed pipe has no explicit opening process.In fact, it is automatically opened when it is created, and a memory inode node, a d entry directory entry object, and two file structure objects are generated. (One read operation, one write operation), its memory object is consistent with ordinary files, so read and write operations use the same file interface, of course, read and write functions are dedicated. Because an unnamed pipe cannot be opened explicitly, it can only be used by two processes for communication between the parent and child processes, between sibling processes, or other pipe files that have ancestor processes. Named pipes have file names and disk inodes, so they can be used for communication between any two or more processes.Its usage is similar to ordinary files.They all follow the process of opening, reading, writing, and closing. The internal implementation is different from ordinary files, and is the same as an unnamed pipe. [2]
2 How unnamed pipes work
The pipeline stores a certain amount of data in a first-in-first-out manner. When using a pipe, one process writes from one end of the pipe and the other reads from the other end of the pipe. In the main process, use the fork () function to create a child process, so that the parent and child processes have both read and write handles to the same pipe.Because the pipe does not provide a lock protection mechanism, the direction of data flow must be determined, and then closed in the corresponding process. Unwanted handle. In this way, you can use read () and write () functions to read and write it. The steps for using an unnamed pipe for inter-process communication are outlined below:
create the required pipeline;
spawn (multiple) child processes;
close / copy the file descriptor to associate it with the corresponding pipe end;
close the ends of the unwanted pipes;
Carry out communication activities;
Close all remaining open file descriptors
Wait for the child process to end.
Because the read () and write () functions have a blocking effect on the pipeline operation itself, it can ensure that one process must perform a write operation before another process can perform a read operation, thereby achieving synchronization between the parent and child processes.
2.2 The establishment and use of anonymous pipes
2.2.1 Pipeline creation
#include <unistd.h> intpipe (intfd [2])
There are two file descriptors in the function's parameters: fd [0] is used for the read end of the pipeline, and fd [1] is used for the write end of the pipeline. If the creation is successful, it returns the value 0, otherwise it returns the value -1.
2.2.2 Write function ret = write (fd [1], buf, n)
If the pipe is full, it is blocked until the other end of the pipe reads the data that has entered the pipe.
2.2.3 read pipeline read function ret = read (fd [0], buf, n)
If the pipe is empty and the write file descriptor is not closed, it is blocked. If the write end of the pipe is closed, 0 is returned. If the pipe is not empty, it is divided into two cases: (Let there are actually m bytes in the pipe), if n> = m, then read m; if n <m, then read n. The actual number of reads is used as the return value of read.
2.2.4 Close the pipeline close function
Closing the write end causes the read end read call to return 0; closing the read end causes the write end write call to return -1, and errno is set to EPIPE. Before the write function of the write end exits, the process will also receive the S IGP IPE signal ( The default processing is to terminate the process, this signal can be caught).
2.2.5 Duplication of file descriptors
intdup2 (intfdl, intfd2);
Copy the file descriptor fdl to fd2. fd2 can be a free file descriptor. If fd2 is an open file, fd2 is closed; if fd1 is not a valid descriptor, fd2 is not closed, and the call fails.
2.3 A few issues to note about unnamed pipes
The pipeline is half-duplex, and data can only be transmitted in one direction. If you want to transfer data between two processes, you need to establish two pipelines.
The pipe () call must be made before fork () is called, otherwise the child process cannot inherit the file descriptor.
Any processes connected to each other using an unnamed pipe must be in a related process family. Because the pipeline must be restricted by the kernel, if the process is not in the family of the pipeline creator, the process will not be able to access the pipeline. [2]
3 named pipes
On Linux systems, named pipe files can be identified. For example: $ ls-lfilenameprw-r--r--lrootroot0sep2719: 40filename | filename followed by a "|" symbol indicates that the file is a pipe file.
3.1 how named pipes work
A major limitation of the application of unnamed pipes is that they can only be used for inter-process communication with kinship. After the name pipes were proposed, this limitation was overcome. Named pipes provide a path name associated with them and exist in the file system as FIFO files. In this way, even if there is no process related to the FIFO creation process, as long as the path can be accessed, they can communicate with each other through the FIFO. Therefore, through FIFO, unrelated processes can also exchange data. The opening method of the FIFO pipe is different from the ordinary pipe.The ordinary pipe includes two file data structures: the corresponding VFS index node and the shared data page, which is created once each time the process runs, and the FIFO always exists and requires users Open and close. Linux must handle both cases where the read process opens the pipe before the write process and the read process reads in before the write process writes data. In addition, FIFO pipes are used in exactly the same way as ordinary pipes, all using the same data structures and operations.
3.2 FIFO file creation
There are two common methods for creating named pipes: using the mknod command at a shell prompt or using the mk nod () system call in a program.
3.2.1 Shell command line mode
$ mknodfilenamep
$ mkfifoa = rwfilename
Both command lines can create the FIFO file filename. mkfifo provides the function of directly changing the read and write permissions of files. The files created by mknod can be changed with chmod. The parameter p indicates the established node, that is, the type of the special file is named pipe. [2]
4 Insufficient pipes
Pipelines provide an efficient way to transfer data from one process to another.However, there are some disadvantages to pipes:
Because reading data also removes data from the pipeline, the pipeline cannot be used to broadcast data to multiple recipients.
If a pipe has multiple read processes, then the write process cannot send data to the specified read process. Similarly, if there are multiple write processes, there is no way to tell which one of them is sending the data. [2]

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?