Linux method for building pipe pipeline functions

  
                

pipe is a pipe in the Linux system. The main body of the pipe mechanism is the call of the pipe function. How do the Linux system build the pipe function? The following small series will introduce you to the method of building a pipe function in Linux. Let's take a look at it.

● unnamed pipes

mainly used between the parent and the child, or between two brothers processes. In the Linux system, a one-way communication pipeline can be established through system calls, and this relationship can only be established by the parent process. Therefore, each pipe is unidirectional, and two pipes need to be established when two-way communication is required. Processes at both ends of the pipeline treat the pipeline as a file, one process is responsible for writing to the pipeline, and the other is reading from the pipeline. This transmission follows the rules of "first in, first out" (FIFO).

● Named Pipes

Named pipes are designed to address the shortcomings of unnamed pipes that can only be used for communication between close relatives. A named pipe is a file with its own name built on the actual disk media or file system (not just in memory), and any process can contact the file at any time by file name or path name. In order to implement named pipes, a new file type ——FIFO file (following the principle of first in, first out) was introduced. Implementing a named pipe is actually implementing a FIFO file. Once a named pipe is established, its read, write, and close operations are identical to those of a normal pipe. Although the inode node of the FIFO file is on the disk, it is only a node. The file data still exists in the memory buffer page, which is the same as the normal pipe.

1. Function Description

pipe(Create Pipeline):

1) Header #include "unistd.h"

2) Define function: Int pipe(int filedes[2]);

3) Function description: pipe() will create a pipe and return 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: Returns zero if successful, otherwise returns -1, the cause of the error is stored in errno.

Error Code:

EMFILE Process has run out of file descriptors maximum

ENFILE The system has no file descriptors available.

EFAULT parameter filedes The array address is invalid.
Previous12Next Total 2 Pages

Copyright © Windows knowledge All Rights Reserved