Linux I/O

Source: Internet
Author: User
Tags epoll
Linux's I/O mechanism has gone through several stages of evolution:
(1) Synchronous blocking I/O: User processes perform I/O operations until I/O operations are completed.
(2) non-blocking I/O synchronization: the user program can set the O_NONBLOCK attribute of the file descriptor, and the I/O operation can return immediately, but it does not guarantee that the I/O operation is successful.
(3) asynchronous blocking I/O: User processes can block I/O events, but I/O operations are not blocked. This is achieved through function calls such as select/poll/epoll.
(4) asynchronous non-blocking I/O: Also called asynchronous I/O (AIO). A user program can send an I/O Request command to the kernel, you do not need to wait for an I/O event to actually occur. You can continue to do other things. After I/O operations are completed, the kernel will notify the user of the process through function callback or signal mechanism. This greatly increases the system throughput.

1. Typical I/O (synchronous blocking I/O)
The typical process is as follows:

Sample Code: While (n = read (STDIN_FILENO, buf, BUFSIZ)> 0)
If (write (STDOUT_FILENO, buf, n )! = N)
Err_sys (write error ");

From the application perspective, the read call may last for a long time. In fact, when the kernel executes read operations and other work, the application is indeed blocked, that is, the application cannot do anything else.
2. synchronous non-blocking I/O
The typical process is as follows:

There are two ways to specify a non-blocking I/O for a given descriptor:
(1) If o p e n is called to obtain this descriptor, the O _ n o n B L O C K flag can be specified.
(2) For an opened descriptor, you can use f c n t l to open the O _ n o n B L O C K file status flag.
For non-blocking I/O, if read finds that no data is readable, it simply returns-EAGAIN ("try it agin") instead of blocking the current process. Let's look at an example of non-blocking I/O: // Nbtest. c
# Include <stdio. h>
# Include <unistd. h>
# Include <fcntl. h>
# Include <stdlib. h>
# Include <errno. h>

Char buffer [4096];

Int main (int argc, char ** argv)
{
Int delay = 1, n, m = 0;

If (argc> 1)
Delay = atoi (argv [1]);
Fcntl (0, F_SETFL, fcntl (0, F_GETFL) | O_NONBLOCK);/* stdin */
Fcntl (1, F_SETFL, fcntl (1, F_GETFL) | O_NONBLOCK);/* stdout */

While (1 ){
N = read (0, buffer, 4096 );
If (n> = 0)
M = write (1, buffer, n );
If (n <0 | m <0) & (errno! = EAGAIN ))
Break;
Sleep (delay );
}
Perror (n <0? "Stdin": "stdout ");
Exit (1 );
}

We use strace to track program execution results:

The content of out.txt is as follows:

Read failure can be clearly seen. In fact, this method requires applications to read data in a round-robin manner. Multiple unnecessary system calls will increase system overhead and affect the throughput of the entire system.

3. asynchronous blocking of I/O
I/O multiplexing in a UNIX environment. The typical process is as follows:

In Linux, poll, epoll, and select functions can be used for I/O multi-channel transfer. They are essentially the same: each process is allowed to determine whether it is readable or to write one or more files without blocking. these calls can also block the process until the file descriptor of any given set can be read or written. therefore, they are often used in applications that must use multiple input and output streams.
3.1. poll Functions # Include <stropts. h>
# Include <poll. h>
Int poll (struct pollfd fdarray [], unsigned long nfds, int timeout );
Return: Number of ready descriptors. The value is 0 if time-out occurs, and-1 if an error occurs.

Struct pollfd {
Int fd;/* file descriptor to check, or <0 to ignore */
Short events;/* events of interest on fd */
Short revents;/* events that occurred on fd */
};
The number of elements in the fdarray is described by nfds.

The events members should be set to one or more of the following values. These values tell the kernel what we care about this descriptor. When returned, the kernel sets the revents member to indicate what events occurred to the descriptor. (Note that the poll does not change the events member ). Values of events and revents:

The first four lines test readability, followed by three lines test writability, and the last three lines are exception conditions. The last three rows are:
Set when the kernel returns. Even if these three values are not specified in the events field, if the corresponding condition occurs, they are also returned in revents. When a descriptor is suspended (POLLUP), it cannot be written to this descriptor. However, data may still be read from this descriptor.
The last parameter of poll indicates how long we want to wait. There are three different scenarios:
• Timeout =-1 always wait. The constant INFTIM is defined in <stropts. h>, and its value is usually-1. When
Or capture a signal. If a signal is captured, p o l returns-1 and errno is set to EINTR.
• Timeout = 0 do not wait. Test all descriptors and return immediately. This is the polling method that gets the status of many descriptors without blocking the p o l function.
• Timeout> 0: wait for timeout milliseconds. If one of the specified descriptors is ready or the specified time value is exceeded, return immediately. If a time-out condition exists but no descriptor is ready, the return value is 0. (If the system does not provide millisecond resolution, the timeout value is rounded to the nearest supported value ).

3.2 Example # Include <stdio. h>
# Include <unistd. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <sys/poll. h>
# Include <fcntl. h>

Char buffer [4096];

Int main (int argc, char ** argv)
{
Struct pollfd pfd;
Int n;

Fcntl (0, F_SETFL, fcntl (0, F_GETFL) | O_NONBLOCK);/* stdin */
Pfd. fd = 0;/* stdin */
Pfd. events = POLLIN;

While (1 ){
N = read (0, buffer, 4096 );
If (n> = 0)
Write (1, buffer, n );
N = poll (& pfd, 1,-1 );
If (n <0)
Break;
}
Perror (n <0? "Stdin": "stdout ");
Exit (1 );
}

We use strace to track program execution results:

Out.txt file:

In this method, the select (or poll) Call will still block the process. Unlike the typical I/O, it waits for event notification. However, it introduces a timeout mechanism that allows the application to have the power to avoid long waits. On the other hand, if the application needs to read and write multiple files, this method can be used. A typical application is the telnet command (for details, see advanced programming in UNIX environment).
3. asynchronous I/O
Linux asynchronous I/O (AIO), that is, asynchronous non-blocking I/O, is a fairly new enhancement provided in the Linux kernel. It is a standard feature of the 2.6 kernel, but we can also find it in the patch of the 2.4 kernel. The basic idea behind AIO is to allow a process to initiate many I/O operations without blocking or waiting for any operation to complete. The process can retrieve the results of an I/O operation later or when it receives a notification that the I/O operation is complete.
The process is as follows:

The asynchronous I/O model is a model that processes overlapping I/O. The read request is returned immediately, indicating that the read request has been initiated successfully. When the read operation is completed in the background, the application then performs other processing operations. When the read response arrives, a signal is generated or a thread-based callback function is executed to complete the I/O processing.

The overlapping processing capabilities of computing operations and I/O processing to execute multiple I/O requests in a process utilizes the difference between processing speed and I/O speed. When one or more I/O requests are suspended, the CPU can execute other tasks. or, more commonly, when other I/O operations are initiated, the completed I/O operations are performed. For more information about this method, see references.

Main reference:
Advanced Programming in UNIX environment
Http://www.ibm.com/developerworks/cn/linux/l-async/

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.