Five I/O models under UNIX

Source: Internet
Author: User

1. I/O model
There are five kinds of 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 (posix.1 aio_ series functions)
1). blocking I/O model
The application calls an IO function, causing the application to block and waiting 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 indicator

2). Non-blocking I/O model
We have set the interface 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. So our I/O operation function will constantly test whether the data is ready and, if not ready, continue testing until the data is ready. During this continuous testing process, the CPU time will be heavily occupied.

3). I/O multiplexing model
The I/O multiplexing model uses the Select or poll functions, which also block processes, but unlike blocking I/O, these two functions can block multiple I/O operations at the same time. It is also possible to detect multiple read operations and multiple write I/O functions at the same time until the data is readable or writable, and the I/O operation function is actually invoked.

4). Signal- driven I/O model
First we allow the socket to signal-drive 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 calls 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 descriptor, buffer pointer, buffer size, file offset, and notification method, and then return immediately. Notifies the application when the kernel copies the data to the buffer.


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

3.SyncI/Oand asynchronousI/O
A. Synchronous I/O operations cause the request process to block until I/O operation completes.
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.
4. Selectfunction
A. The Select function can instruct the kernel to wait for either of several events to occur, and only return after either event occurs or after a specified time, to wake up the process
B. You can call the Select function to notify the kernel to return when the following occurs:
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. Descriptive words can be unrestricted and sockets, and any descriptor can be tested with a select
D. Function prototypes
int select (int Maxfds, fd_set *readfds, Fd_set *writefds,
Fd_set *exceptfds, struct Tim *timeout);
Parameter description:
Timeout: Tell the kernel how long it will take to wait for any descriptor to be ready, there are three different scenarios
The first scenario is that timeout is null, so that it waits until a descriptor is ready;
The second case is that the value of the timeout is 0, then it will not wait for immediate return;
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, the select is also interrupted to return.
Readfds,writefds and Exceptfds Specify the descriptive words needed to let the kernel test read, write, and exception conditions.
Maxfds: Describes the number of descriptors being tested, and its value is the largest description to be tested alphanumeric 1.
Return value: The prepared total number of digits for all descriptor sets. When returned, any descriptor not prepared in the descriptor set is cleared by 0, and we use Fd_isset to test which descriptor is ready. Therefore, each time a select is invoked, the descriptor we are interested in is reset to 1 in the descriptor set.
E. fd_set note
Fd_set is an array of integers in which each digit corresponds to a descriptor. For example, with a 32-bit representation of an integer, the first element of the array corresponds to the descriptor 0~31, and the second element corresponds to the descriptive 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. Descriptor ready to read condition:
The data byte in the socket buffer is greater than the current value equal to the threshold limit of the socket receive buffer. The socket will not block and return a value greater than 0, which is the number of bytes of data currently ready to be read.
The sleeve interface receives a fin, and the read operation of the sleeve interface returns a 0.
The socket is a listener socket, and the number of connections completed is not 0.
There is a set of interface errors to be processed. The read operation of the sleeve interface will return-1.
G. Conditions in which descriptors are ready to write:
The free space of the socket send buffer is greater than the current value equal to the low ebb limit of the set interface send buffer. , and the socket interface does not require a connection, or a socket to connect.
Set of interfaces to write this half off, this will result in a sigpipe error.
There is a set of interface errors to be processed.
H. Conditions for Descriptor exceptions:
Out-of-band data exists in the socket interface
Still in an out-of-band marker
I. Maximum descriptors that can be used by each 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 to modify the previous customer-Server Programs
In the previous client-server program, the client uses a stop-and-wait strategy to receive user input from the standard input, which can be done on a one-to-one completion from the user input and then read the string returned from the server, which is a disadvantage when the program blocks while waiting for user input, It is not possible to handle these messages in a timely manner from the server fin. Now we use the Select function to make some changes to the client program so that we can avoid the problem mentioned previously.
In addition, the server uses a select function to avoid excessive processes, and a single process can handle multiple clients after using a select. Create an array of integers on the server side to hold the completed client connections. Each time we read the data from the Heaven socket, we add a new connection from the client to the array and modify the MAXFD value. Each time the data is read from the client socket interface, the read data is written back to the client socket interface.
A. The return value may be 0 after the server has read the data from the client socket interface, indicating that the client has closed the connection in this direction. After the data is written to the client socket, the connection is closed. and removes the customer connection from the array where the client is connected.
B. Using the above scenario, a potential problem is the possibility of denial of service attacks. A malicious user and server establish a connection, send a single character, but do not send line breaks or terminate, so the server blocks in the Read function. The possible solution is to use non-blocking I/O or have each client handle a separate process or set a time-out for I/O operations.
C. When the client receives EOF, only the connection that writes this direction is turned off because we still want to read the data from the server. You cannot close the connection by closing it with shutdown. If the access count for the socket is greater than 0, close will simply subtract the count by 1, and if the socket's access count equals 0,close will terminate the socket's two orientations, 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 descriptor
HOW:SHUT_RD--The connection that closes the read data direction of the socket
SHUT_WR--A connection that closes the write data direction of a socket
Shut_rdwr--Turn off socket two-way connections
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 nanoseconds
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: is a pointer to a struct POLLFD structure body
Nfds: Describe the number of descriptive words we care about
Timeout: Timeout wait time, in milliseconds
B.struct POLLFD Structural Body Description
struct POLLFD {
int FD;
Short events;
Short revents;
};
FD: is a descriptive word
Events: A matter of concern in a descriptive word
Revents: An event that is returned on a descriptive word

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

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.