Mr. Wang, have you heard of AIO? Foreigners, in this way, always love short form, the result is we don't know. The so-called AIO is asynchronous input/output. The basic idea is to allow the 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.
"Well, why did you take a lesson with me? Didn't you say yes? Is CS today? Isn't it so confident to give a lecture to me? "Mr. Smith complained.
"Ah? No, why? Well, you don't listen to it. Don't try the interview every day. Don't try again, "I said angrily.
"Alas, you have said this, and I have to raise my ears to hear it well." looking at Wang's reluctant expression, I also feel very pitiful.
In asynchronous non-blocking Io, we can initiate multiple transmission operations at the same time. This requires a unique context for each operation to determine which transmission operation is completed after they complete the time zone. In AIO,
The structure is distinguished by the aiocb (AIO Io control block) structure, which is as follows:
StructAiocb {IntAio_fildes;/* File descriptor */Off_taio_offset;/* File offset */Volatile Void* Aio_buf;/* Location of buffer */Size_taio_nbytes;/* Length of Transfer */IntAio_reqprio;/* Request priority offset */StructSigeventaio_sigevent;/* Signal number and value */IntAio_lio_opcode;/* Operation to be saved med */};
We can see from the above that this struct contains all information about transmission, including the user buffer for data preparation. When an IO notification is generated, the aiocb structure is used to uniquely identify the completed Io operation.
AIO APIs mainly include the following functions:
1. Int aio_read (struct aiocb * aiocbp)
This function requests an asynchronous read operation on a valid file descriptor. After the request is queued, it will be returned immediately. If the execution is successful, the return value is 0. if the error is returned,-1 is returned and the errno value is set.
2. Int aio_write (struct aiocb * aiocbp)
This function requests an asynchronous write operation. It immediately returns a message indicating that the request has been queued. If the request is successful, 0 is returned,-1 is returned for failure, and the corresponding error value is set.
3. Int aio_error (struct aiocb * aiocbp)
This function is used to determine the Request status. It can return einss SS (indicating that the request has not been completed), ecancelled (the request is applied)ProgramCanceled),-1 (indicating an error occurred. The specific cause of the error is recorded by the error ).
4. ssize_t aio_return (struct aiocb * aiocbp)
Since the read call is not blocked, we cannot immediately return the returned status of this function. This function is required, it should be noted that only when the aio_error call determines that the request has been completed (possibly
This function can be called only after it has been completed or an error may occur). The return value of this function is equivalent to the return value called by the read or write System Under synchronization (the number of transmitted bytes, if an error occurs,-1 is returned ).
5. Int aio_suspend (const struct aiocb * const cblist [], int N, const struct timespec * timeout)
You can use this function to suspend (or block) The Calling process until the asynchronous request is complete. At this time, a signal or other timeout operations will occur. The caller provides an aiocb reference list, and any completion of the list will result in a return to the function.
6. Int aio_cancel (int fd, struct aiocb * aiocbp)
This function allows you to cancel one or all IO requests executed on a file descriptor.
To cancel a request, you must provide the file descriptor and aiocb reference. If the request is successfully canceled, aio_canceled is returned. If the request is completed, aio_notcanceled is returned.
If you want to cancel all requests to a given file descriptor, you need to provide the descriptor of this file and a null reference of aiowhite. If all requests are successfully canceled, aio_canceled is returned.
If at least one of them is not canceled, this function returns aio_not_canceled. If no request can be canceled, this function returns aio_alldone.
Then, you can use aio_error to verify each AIO request. If a request has been returned, aio_error returns-1 and the error is set to ecanceled.
7. Int lio_listio (INT mode, struct aiocb * list [], int NENT, struct sigevent * sig)
This operation allows you to initiate a large number of I/O operations in one system call (a kernel context switch ). Among them, the mode parameter can be lio_wait or lio_nowait. The former will block this call until all Io operations are completed. After the operation is queued, lio_nowait will return, list is a list referenced by aiocb. the maximum number of elements is defined by NENT. If the list element is null, lio_lis
TiO () will be ignored.
I can't do either of them, right? Here's the actual point:
A) user space reading routine:
# Include <AIO. h> .. Int FD, set; Struct Aiocb my_aiocb; FD = Open (" File.txt ", O_rdonly ); If (FD <0) {perror (" Open ");} // Clear the aiocb struct Bzero (( Char *) & My_aiocb, Sizeof ( Struct Aiocb ));// Allocate data buffer for aiocb requests My_aiocb.aio_buf = Malloc (Bufsize + 1 ); If (! My_aiocb.aio_buf) perror (" Malloc "); // Initialize the aiocb Member My_aiocb.aio_fildes = FD; my_aiocb.aio_nbytes = bufsize; my_aiocb.aio_offset = 0; ret = aio_read (& my_aiocb ); If (Ret <0) perror (" Aio_read "); While (Aio_error (& my_aiocb) = einprogress ); If (Ret = aio_return (& my_iocb ))){ // Obtain the return value of asynchronous read } Else {Read failed, analysis errror}
B) user space asynchronous Io aio_suspend () function usage routine
StructAioct * cblist (max_list)// Clear the linked list of the aioct StructureBzero ((Char*) Cblist,Sizeof(Cblist ));// Put one or more aiocb into the linked list of the aioct struct.Cblist [0] = & my_aiocb; ret = aio_read (& my_aiocb); ret = aio_suspend (cblist, max_list, null );
C) user space asynchronous Io lio_list () function usage routine
StructAiocb aiocb1, aiocb2;StructAiocb * list [max_list];...// Prepare the first aiocbAiocb1.aio _ Fildes = FD; aiocb1.aio _ Buf =Malloc(Bufsize + 1); aiocb1.aio _ nbytes = bufsize; aiocb1.aio _ offset = next_offset; aiocb1.aio _ lio_opcode = lio_read;// Asynchronous read operation...// Prepare multiple aiocb instancesBzero ((Char*) List,Sizeof(List ));// Enter aiocb in the linked listList [0] = & aiocb1; list [1] = & aiocb2;... ret = lio_listio (lio_wait, list, max_list, null );// Initiate a large number of Io operations
"Tao Ge, you have said so much, it seems that you have not been connected to the driver you said," Xiao Wang complained.
"Mr. Wang, don't you worry? I'm not planning to talk about it. Look at you. Let's take a good look at today. Let's talk about it next time "...