Introduction to asynchronous notifications for Linux Device Drivers

Source: Internet
Author: User
Article Title: Brief Introduction to asynchronous notifications driven by Linux devices. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

The combination of blocking and non-blocking access and poll functions can better solve the reading and writing of devices, but it is more convenient if asynchronous notifications are provided. Asynchronous notification means that once the device is ready, the application is actively notified so that the application does not need to query the device status at all. This is very similar to the concept of "interruption" on hardware, the more accurate title is "SIGIO asynchronous I/O ".

Let's take a look at a signal-driven example. It uses signal (SIGIO, input_handler) to start the signal mechanism for STDIN_FILENO. When the input is available, input_handler is called. The source code is as follows:

       
        
# Include
        
         
# Include
         
          
# Include
          
           
# Include
           
            
# Include
            
              # Include
             
               # Define MAX_LEN 100 void input_handler (int num) {char data [MAX_LEN]; int len; // read and output the input len = read (STDIN_FILENO, & data, MAX_LEN); data [len] = 0; printf ("input available: % s \ n", data) ;}main () {int oflags; // enable signal driving mechanism signal (SIGIO, input_handler); fcntl (signature, F_SETOWN, getpid (); oflags = fcntl (signature, F_GETFL); fcntl (STDIN_FILENO, F_SETFL, oflags | FASYNC); // finally enters an endless loop, and the program does nothing. Only the signal can stimulate input_handler to run. // if the program does not have this endless loop, the execution will be completed immediately while (1 );}
             
            
           
          
         
        
       

To enable the device to support this mechanism, we need to implement the fasync () function in the driver, and call kill_fasync () in the write () function when data is written () the function inspires a signal, which is left for the reader to complete.

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.