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.