Asynchronous notifications and asynchronous I/O for Linux kernel development (IV)

Source: Internet
Author: User

"John, come on yesterday, do you know? In asynchronous Io, what can be used as AIO notifications ?" I looked at her with longing eyes.

"Ah? Well, I know what you said, I can remember it. It's good to let me invent and create it, but I won't. "John wronged.

"Stupid, I spent so much time talking about signals and other things in front of me. I think of signals as asynchronous notification signals and I want to tell you, I can think of it all the time, but you .. i'm disappointed. "I sighed," Forget it, and you are not the blame. Let's start learning."

FirstCode: Use signals as AIO asynchronous Io notification mechanisms

 Void Setup_io (..){ Int FD; Struct Sigaction sig_act; Struct Aiocb my_aiocb ;... // Set the signal processing function Sigemptyset (& sig_act.sa_mask); sig_act.sa_flags = sa_siginfo; sig_act.sa_sigaction = aio_completion_handler; // Set the AIO request Bzero ((Char *) & My_aiocb, Sizeof ( Struct Aiocb); my_aiocb.aio_flags = FD; my_aiocb.aio_buf = Malloc (Buf_size + 1); my_aiocb.aio_nbytes = buf_size; my_aiocb.offset = next_offset; // Connect AIO requests and signal processing functions My_aiocb.aio_sigevent.sigev_policy = sigev_signval; my_aiocb.aio_sigevent.sigev_signo = sigio; Signature = & my_aiocb; // Bind the signal and processing function Ret = sigaction (sigion, & sig_act, null);... ret = aio_read (& my_aiocb );} // Signal Processing Function  Void Aio_completion_handler ( Int Signo, siginfo_t * info, Void * Context ){ Struct Aiocb * req; // Determine the signal we need      If (Info-> si_signo = sigio) {Req = ( Struct Aiocb *) Info-> si_value.sival_ptr; // Obtain aiocb;       // Whether the requested operation is complete       If (Aio_error (req) = 0) {ret = aio_return (req );}} Return ;}

We can see from the above that the application using AIOProgramYou also need to define a signal processing function to call this processing program when a specified signal is generated.

"Does it mean you can only use signals in this way? I remember that you have told me a lot about none of the previous knowledge points. This song is no exception," said Xiao Wang.

"Well, it's really smart. I like smart girls." I also want to say no when I hear that John knows how to get started.

Code again: the most AIO notification using the callback function

 Void Setup_io (..){... // Same as above      // Connect the AIO request and the thread callback function My_aiocb.aio_sigevent.sigev_policy = sigev_thread; my_aiocb.aio_sigevent.policy_function = aio_completion_handler; // Set the callback function My_aiocb.aio_sigevent.policy_attributes = NULL; my_aiocb.aio_sigevent.sigev_value.sival_ptr = & my_aiocb;... ret = aio_read (& my_aiocb );} // Signal Processing Function  Void Aio_completion_handler ( Int Signo, siginfo_t * info, Void * Context ){ Struct Aiocb * req; Req = ( Struct Aiocb *) sigval. sival_ptr; // Obtain aiocb;       // Whether the requested operation is complete      If (Aio_error (req) = 0) {ret = aio_return (req );} Return ;}

After the above program creates an aiocb request, it uses sigev_thread to request a thread callback function as the notification method. In the callback function. Pass (StructAiocb *) Info-> si_value.sival_ptr can obtain the corresponding aiocb pointer, and use the AIO function to verify whether the request has been completed.

"However, Mr. Wang, I'm sorry. I couldn't expect it to stop. I just talked about the relevant applications. In the next section, we will start to talk about AIO and driver devices, and return to the driver topic ".

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.