Linux system programming pipeline (III): Command pipeline (FIFO)

Source: Internet
Author: User

I. Limitations of anonymous pipeline PIPE

The main limitations of MPs queue are as follows:

  • Only unidirectional data streams are supported;
  • It can only be used between unrelated processes;
  • No name;
  • The buffer of the MPs queue is limited (the MPs queue is in the memory and a page size is allocated to the buffer when the MPs queue is created );
  • The pipeline transmits a non-formatted byte stream, which requires that the reader and writer of the pipeline have to specify the data format in advance, for example, how many bytes are counted as a message (or command, or record) and so on;

If we want to exchange data between unrelated processes, we can use a FIFO file to do this. It is often called a named pipe and is a special type of file.

2. Name the pipe FIFO. 2.1 key concepts related to famous Pipelines

A major limitation of a canal application is that it has no name, so it can only be used for Kinship-related inter-process communication. After proposed by the famous Pipeline (named pipe or FIFO, this restriction is overcome. FIFO is different from pipelines in that it provides a path name associated with it and exists in the file system as a FIFO file. In this way, the process does not have a kinship with the FIFO creation process, as long as the path can be accessed, it can communicate with each other through FIFO (the process that can access this path and the process that creates the FIFO). Therefore, data can be exchanged through non-FIFO processes. It is worth noting that the first in first out (FIFO) is strictly followed, and data is always returned from the beginning for the read of the pipeline and the first in first out (FIFO, write to them to add the data to the end. They do not support file location operations such as lseek.

2.2 creation of famous Pipelines

You can create a named pipe from the command line by using the following command:

$ Mkfifo filename

The named pipeline can also be created from the program. Related functions include:

#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char * pathname, mode_t mode)

The first parameter of the function is a common path name, that is, the name of the first FIFO after the function is created. The second parameter is the same as the mode parameter in the open () function for opening a common file. If the first parameter of mkfifo is an existing path name, The EEXIST error is returned. Therefore, the typical Call code first checks whether the error is returned. if the error is returned, you only need to call the function to open the FIFO. General file I/O functions can be used for FIFO, such as close, read, write, and so on.

Man help description:

DESCRIPTION
       mkfifo() makes a FIFO special file with name pathname.  mode       specifies the FIFO's permissions.  It is modified by the process's       umask in the usual way: the permissions of the created file are (mode       & ~umask).       A FIFO special file is similar to a pipe, except that it is created       in a different way.  Instead of being an anonymous communications       channel, a FIFO special file is entered into the file system by       calling mkfifo().       Once you have created a FIFO special file in this way, any process       can open it for reading or writing, in the same way as an ordinary       file.  However, it has to be open at both ends simultaneously before       you can proceed to do any input or output operations on it.  Opening       a FIFO for reading normally blocks until some other process opens the       same FIFO for writing, and vice versa.  See fifo(7) for nonblocking       handling of FIFO special files.
RETURN VALUE
       On success mkfifo() returns 0.  In the case of an error, -1 is       returned (in which case, errno is set appropriately).
2.3 open rules for famous pipelines (same as that for anonymous pipelines)

The only difference between FIFO (named pipeline) and pipe (anonymous pipeline) is that they are created and opened in different ways, and they have the same semantics after completing these tasks.

Man help description: The only difference between pipes and operating OS is the manner in which they are created and opened. once these tasks have been accomplished, I/O on pipes and Guest OS has exactly the same semantics.

A famous Pipeline has one more open operation than the 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 set for the current open operation); or, it will be returned successfully (the blocking flag is not set for the current open operation ).

If the current enable operation is to enable the FIFO for writing, if a corresponding process has opened the FIFO for reading, the current enable operation will be successful; otherwise, it may be blocked until a corresponding process opens the FIFO for reading (the blocking flag is set for the current open operation); or, an ENXIO error is returned (the blocking flag is not set for the current open operation ).

2.4 read/write rules for famous Pipelines

Read data from FIFO:

Convention: if a process blocks the access to the FIFO to read data from the FIFO, the read operation in the process is a read operation with a blocking flag.

  • If a process writes to enable FIFO and there is no data in the current FIFO, the read operation with the blocking flag is always blocked. -1 is returned if no blocking mark is set for the read operation. The current errno value is EAGAIN, prompting you to try again later.
  • For read operations with the blocking flag configured, there are two reasons for blocking: data in the current FIFO, but other processes are reading the data. In addition, there is no data in the FIFO. The reason for blocking is that there are new data writes in the FIFO, regardless of the size of the data written to the message or the amount of data requested by the read operation.
  • The read blocking mark only applies to the first read operation of the process. If there are multiple read operation sequences in the process, after the first read operation is awakened and the read operation is completed, other read operations to be executed will not be blocked, even if there is no data in the FIFO when the read operation is executed (at this time, the read operation returns 0 ).
  • If no process write is enabled, the read operation with the blocking flag set will be blocked.

NOTE: If data exists in the FIFO, the read operation with the blocking flag is not blocked because the number of bytes in the FIFO is smaller than the number of bytes requested to read, the read operation returns the existing data volume in the FIFO.

Write Data to FIFO:

Convention: if a process blocks the opening of the FIFO to write data to the FIFO, the write operation in the process is a write operation with a blocking flag.

Write operations with blocking flag configured:

  • When the data volume to be written is not greater than PIPE_BUF, linux ensures the atomicity of writing. If the idle buffer of the pipeline is insufficient to accommodate the number of bytes to be written, the system goes to sleep until the buffer can accommodate the number of bytes to be written.
  • When the data volume to be written is greater than PIPE_BUF, linux will no longer guarantee the atomicity of writing. As soon as the FIFO buffer has an idle area, the write process will attempt to write data to the pipeline, and the write operation will return after writing all the data written by the request.

For write operations without blocking flag set:

  • When the data volume to be written is greater than PIPE_BUF, linux will no longer guarantee the atomicity of writing. After the buffer is fully written into all the FIFO idle buffers, the write operation returns.
  • When the data volume to be written is not greater than PIPE_BUF, linux ensures the atomicity of writing. If the current FIFO idle buffer can accommodate the number of bytes written by the request, the result is returned successfully. If the current FIFO idle buffer cannot accommodate the number of bytes written by the request, the EAGAIN error is returned, remind me to write it later;
Iii. use FIFO

Example 1: Create a FIFO file

#include <stdio.h><unistd.h><stdlib.h><sys/stat.h><sys/types.h> main( argc,  **(argc != ,argv[(mkfifo(argv[],) == - 

Result:

<Unistd. h> <stdlib. h> <sys/stat. h> <sys/types. h> <fcntl. h> main (argc, ** fd = open (, O_RDONLY); (fd =-

Result:

<Unistd. h> <stdlib. h> <sys/stat. h> <sys/types. h> <fcntl. h> main (argc, ** fd = open (, O_WRONLY); (fd =-

Result:

<Unistd. h> <stdlib. h> <sys/stat. h> <sys/types. h> <fcntl. h> main (argc, ** (argc! =, Argv [= open (argv [(infd =-(mkfifo (,) =-= open (fd =-buf [* n = (n = read (infd, buf ,*

Read process:

#include <stdio.h><unistd.h><stdlib.h><sys/stat.h><sys/types.h><fcntl.h> main( argc,  **(argc != ,argv[= open(argv[],O_WRONLY|O_CREAT|(outfd == -= open((fd == - buf[* n = ((n = read(fd,buf,* 

Result:

Copied successfully!

Note: Reference: http://blog.csdn.net/sooneboy/article/details/3915490

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.