Five types of IO models under UNIX

Source: Internet
Author: User

Http://blog.chinaunix.net/uid-25324849-id-247813.html

1. I/O model
A total of five I/O models under UNIX
A. Blocking I/O
B. Non-blocking I/O
C. I/O multiplexing (SELECT and poll)
D. Signal-driven I/O (SIGIO)
E. Asynchronous I/O (Aio_ series functions for Posix.1)
1). blocking I/O models
The application calls an IO function that causes the application to block and wait for the data to be ready.
If the data is not ready, wait ....
Data ready, copy from kernel to user space
I/O function returns success indication

2). non-blocking I/o model
We set up a set of interfaces to non-blocking is to tell the kernel, when the requested I/O operation cannot be completed, do not sleep the process, but return an error. This way our I/O operations function will constantly test whether the data is ready, and if not, continue testing until the data is ready. In this continuous testing process, the CPU will be a lot of time.

3). I/O multiplexing model
The I/O multiplexing model uses the Select or poll function, which also blocks the process, but unlike blocking I/O, these two functions can block multiple I/O operations at the same time. I/O functions can be detected at the same time for multiple read operations, multiple write operations, and I/O operation functions are not actually invoked until there is data readable or writable.

4). Signal-driven I/o model
First we allow the socket interface to drive the signal-driven I/O and install a signal processing function, and the process continues to run without blocking. When the data is ready, the process receives a sigio signal that can be called by the I/O operation function in the signal processing function to process the data.


5). Asynchronous I/o model
Call the Aio_read function, tell the kernel description word, buffer pointer, buffer size, file offset, and how to notify, and then immediately return. Notifies the application when the kernel copies the data to the buffer.


2. Comparison of several I/O models
The first four models are basically the same, the second phase is basically the same, and the data is copied from the kernel to the caller's buffer. The two phases of asynchronous I/O are different from the first four models.

3. Synchronous I/O and asynchronous I/O
A. synchronous I/O operations cause the request process to block until the I/O operation is complete.
asynchronous I/O operations do not cause the request process to block.
B. Our top four models are synchronous I/O, and only the last asynchronous I/O model is asynchronous I/O.

Part of the content from:http://blog.csdn.net/sunyubo458/archive/2010/12/24/6096723.aspx

4. Selectfunction
A. The Select function can instruct the kernel to wait for any one of several events to occur and to wake the process only if either event occurs or after a specified time has been returned
B. You can call the Select function to notify the kernel that it will return if the following conditions occur:
Any descriptor in the collection {1,4,5} is ready to read, or
Any descriptor in the collection {2,7} is ready to write, or
Any descriptor in the collection {1,4} has an exception condition to be processed, or
It's been 10.2 seconds.
C. Descriptors can be unrestricted and sockets, any descriptor can be tested with select
D. Function prototypes
int select (int Maxfds, fd_set *readfds, Fd_set *writefds,
Fd_set *exceptfds, struct Tim *timeout);
Parameter description:
Timeout: Tells the kernel how long it will take to wait for any descriptor to be ready, there are three scenarios,
The first case is that timeout is null, so it waits until a descriptor is ready;
The second case is that the value of timeout is 0, then it will not wait and return immediately;
The third scenario is that the seconds or microseconds in the timeout are assigned, and then the specified time is awaited.
In addition, if the process receives a signal, select is also interrupted to return.
Readfds,writefds and Exceptfds Specify the descriptive words required for the kernel to test read, write, and exception conditions.
Maxfds: Describes the number of descriptors to be tested, and its value is the maximum description to be tested multibyte 1.
Return value: The total number of ready-made digits for all descriptor sets. When returned, any descriptor that is not prepared in the descriptor set is cleared 0, and we use Fd_isset to test which descriptor is ready. Therefore, each time you invoke Select, you re-set the descriptor of our concern to 1 in the descriptor.
E. fd_set description
Fd_set is an array of integers, each of which corresponds to a descriptor. For example, with 32 bits representing an integer, the first element of the array corresponds to the descriptor 0~31, and the second element corresponds to the description word 32~63.
Four related macros:
FD_CLR (int fd, fd_set *set);
Fd_isset (int fd, fd_set *set);
Fd_set (int fd, fd_set *set);
Fd_zero (Fd_set *set);
F. The condition that the descriptor is ready to read:
The data bytes in the socket buffer are greater than or equal to the current value of the set interface receive buffer low tide limit. The socket interface will not block and return a value greater than 0, which is the number of data bytes currently ready to be read.
The socket interface receives a FIN, and the read operation of the socket interface returns a 0.
The socket interface is a listening socket interface, and the number of connections completed is not 0.
There is a set of interface errors pending processing. The read operation of the socket interface will return-1.
G. Conditions for which a descriptor is ready to write:
The free space of the set interface send buffer is greater than the current value of the set interface send buffer low tide limit. , and either the socket interface is connected, and the socket does not require a connection.
Socket interface write this half off, which will produce a sigpipe error.
There is a set of interface errors pending processing.
H. Conditions for Descriptor exceptions:
The socket interface has out-of-band data
Still in out-of-band marking
I. Maximum descriptors that can be used per process
There is a macro fd_setsize defines the maximum number of descriptors a process can use. If you want to change this value not only to change in the defined header file, but also to recompile the kernel.

5.UseSelectfunction modifies the previous customer-Server Programs
In the previous client-server program, the client was using a stop-and-wait policy to receive user input from the standard input, the advantage is that it can be done one-on-a-time from the user input, and then read the string returned from the server, the disadvantage is that when the program is blocked waiting for user input, These messages cannot be processed in a timely manner from the fin on the server. Now we use the Select function to make some modifications to the client program, so as to avoid the problems mentioned earlier.
In addition, the server uses the SELECT function to avoid producing too many processes, and with select, you can process multiple clients with just one process. An array of integers is created on the server side to hold the client connections that have been completed. Each time the data is read from the Heaven Socket interface, we add a new connection from the client to the array and modify the value of the MAXFD. Each time the data is read from the client socket interface, the read data is re-written back to the client socket interface.
A. After the server reads the data from the client socket interface, the return value may be 0, indicating that the client has closed the connection to write in this direction. After the data is written to the client socket interface, the connection is closed. and remove the client connection from the array that holds the client connection.
B. With the above scenario, there is a potential problem with the possibility of a denial of service attack. A malicious user and server establish a connection, send a single character, but do not send a newline character or terminate, so the server will block in the Read function. The possible workaround is to use non-blocking I/O or to have each client handle it with a separate process or set a timeout for I/O operations.
C. When the client receives EOF, it can only turn off the connection that writes in that direction, because we still want to read the data from the server. You cannot use close to close the connection, but use shutdown to close it. If the access count for a socket is greater than 0, then close simply will subtract the count by 1, and if the access count of the socket equals 0,close will terminate the socket in two directions, then we will not be able to read data that is still not sent back from the server.
6. Shutdownfunction
int shutdown (int s, int how);
Parameter description:
S: Represents the socket description word
HOW:SHUT_RD--Closes the connection for the read data direction of the socket
SHUT_WR--Closes the connection to the socket's write data direction
Shut_rdwr--Close the socket bidirectional connection
7. Pselectfunction
int pselect (int n, fd_set *readfds, Fd_set *writefds,
Fd_set *exceptfds, const struct TIMESPEC *timeout, const
sigset_t *sigmask);
The A.pselect function uses the TIMESPEC structure, which supports nanosecond
B.sigmask is a signal mask that will prohibit the submission of certain signals
8. Pollfunction
int poll (struct POLLFD *ufds, unsigned int nfds, int timeout);
A. Parameter description
UFDs: A pointer to a struct POLLFD struct
Nfds: Description of the number of descriptive words we care about
Timeout: Time to wait, in milliseconds
B.STRUCT POLLFD Structure Description
struct POLLFD {
int FD;
Short events;
Short revents;
};
FD: is a descriptive word
Events: An event of concern in a descriptive word
Revents: Is the event returned on the descriptive word

After the poll function returns, we want to test whether the events in Revents are our concern.

Five types of IO models under UNIX

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.