I. Preface
Pipelines can be divided into famous and unknown pipelines.
An unknown pipeline can only be used for inter-process communication with relatives, that is, inter-process communication can only be performed between parent and child processes or sibling processes. A famous pipeline can be used for communication between any process. The pipeline has half duplex, that is, at a certain time, only one process can read or write to the pipeline.
Pipelines are the oldest form of IPC in Unix systems. Pipelines have the following two limitations:
1) They are half-duplex in history (that is, data can only flow in one direction );
2) They can only be used between processes with common ancestor. A pipeline is created by a process and fork () is called by the process. Then, the pipeline can be applied between parent and child processes.
2. Unknown Pipeline
What to do after fork is called depends on the direction of the data stream we want. For the pipeline from the parent process to the child process, the parent process closes the read-side FD of the pipeline [0], and the child process closes the write-side FD [1].
1. Pipe Function
Create an MPS queue.
# Include <unistd. h> Int pipe (INT filedes [2]); |
Filedes: The parameter returns two file descriptors: filedes [0] is opened for read, filedes [1] is opened for write, and filedes [1] is output for filedes [0;
Shmflg: ipc_creat, ipc_excl,
2. Unknown MPs queue instance
Create a FIFO and fork, disable reading in the parent process, and write in the child process; implement half-duplex pipeline communication.
# Include <stdio. h> # Include <unistd. h> # Include <sys/types. h> # Include <fcntl. h> # Include <sys/IOCTL. h> Int main (void) { Int N, FD [2]; Pid_t PID; Char line [100]; If (pipe (FD) <0) Printf ("[% s: % d]: pipe error! \ N ",__ function __,__ line __); If (pid = fork () <0) Printf ("[% s: % d]: fork error! \ N ",__ function __,__ line __); Else if (pid> 0) // parant { Close (FD [0]); Write (FD [1], "Hello word \ n", 12 ); } Else { Close (FD [1]); N = read (FD [0], line, sizeof (line )); Printf ("[% s: % d]: FIFO date = % s \ n" ,__ function __,__ line __, line ); } Exit (0 ); } |
The running result is:
# Gcc fifo. C-o LCL #./LCL [Main: 26]: FIFO date = Hello word |
3. Famous Pipelines
A major restriction of a pipeline application is that it has no name, so it can only be used for Kinship-related inter-process communication. After a famous Pipeline (named pipe or FIFO) is proposed, this restriction is overcome. In this way, even if there is no kinship between the Process and the FIFO creation process, as long as the path can be accessed, the process can communicate with each other through FIFO. Follow the rules for first-in, first-out, and lseek () positioning is not supported.
1. mkfifo Function
Create a FIFO pipeline.
# Include <sys/STAT. h> # Include <sys/types. h> Int mkfifo (const char * pathname, mode_t mode ); |
2. Open rules for famous Pipelines
A famous Pipeline has one more open operation than an unknown pipeline: open. FIFO open rules.
If a read-only FIFO is enabled for the current open operation, if a corresponding process has enabled this FIFO for writing, the current open operation will be successful; otherwise, it may be blocked until a corresponding process opens the FIFO for writing (the blocking flag is currently enabled); or a success is returned.
3. Read and Write rules for famous Pipelines
Read data from FIFO: sets a blocking flag. When reading data, if there is data in the current FIFO, and other processes are reading the data, or there is no data, the current process will be blocked.
The read blocking mark only applies to the first read operation of the process. In fact, the read operation to be executed will not be blocked.
4. Famous pipeline instance
# Include <stdio. h> # Include <sys/STAT. h> # Include <sys/types. h> # Include <fcntl. h> # Define FIFO "/home/lichenglong/study_key/FIFO" Int main () { Char buffer [80]; Int FD; Unlink (FIFO ); Mkfifo (FIFO, 0666 ); If (Fork ()> 0) { Char s [] = "Hello! \ N "; FD = open (FIFO, o_wronly ); Write (FD, S, sizeof (s )); Close (FD ); } Else { FD = open (FIFO, o_rdonly ); Read (FD, buffer, 80 ); Printf ("read data: % s", buffer ); Close (FD ); } } |
Running result
# GCC gfifo. c #./A. Out Read data: Hello! |