Linux Pipeline Analysis-general Linux technology-Linux programming and kernel information. The following is a detailed description. I learned about the Linux pipeline in the past two days. There are many methods for inter-process communication, such as semaphores, messages, and pipelines. In general, communication between simple processes to run synchronization, semaphores, and even mutex (single-value semaphores) is enough; For formatted communication, messages meet the requirements well. Because the meaning and processing of messages are normalized for the operating system, the risks are also relatively small. However, the communication methods between the above two processes are too simple. When flexible control is required and the data volume is large, these two methods cannot be competent, the pipeline meets this requirement.
MPs queues behave like common file descriptors. Without communication, you cannot know whether a file descriptor is a file or a pipeline. This is a feature; programs that read and write standard outputs do not need to know or care whether they are communicating with other processes. If you want to know, the more standard check method is to call 'lseek (fd, 0, SEEK_CUR) 'for the file descriptor; this call attempts to search 0 bytes from the current location, that is, this is an operation that has nothing to do. Generally, such an operation is called "no-op. However, nothing is done, and no result is produced. (otherwise, what is the significance of this ?), For pipelines, this operation will fail and will not cause any damage to other files. This seems to be a trick that smart programmers often play. Do you still remember to write an assembly on Intel X86? And ax seem to have done nothing, but they can set the flag bit to obtain many features of the number in the AX register.
The system calls pipe () to create an MPS queue:
# Include
Int pipe (int filedes [2]);
The parameter value is the address of an integer array of two elements. If pipe () succeeds, 0 is returned. If an error occurs,-1 is returned.
If the call is successful, the process now has two additional open file descriptors. The value in filedes [0] is the read end of the pipeline, while filedes [1] is the writer end of the pipeline) (a convenient memory mode is that the Reading end uses index 0, similar to the standard input file descriptor 0 (stdin), while the writing end uses index 1, similar to the standard output file descriptor 1 (stdout )).
As mentioned above, the data written to the write end can be read from the read end. Call close () to close both ends of the MPs queue. The following simple program demonstrates how to create an MPS queue, write data to it, and read data from it: