Read notes on asynchronous notification of Linux Device Driver Programming

Source: Internet
Author: User

Asynchronous notification for Linux Device Driver Programming

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 <sys/types. h>
# Include <sys/STAT. h>
# Include <stdio. h>
# Include <fcntl. h>
# Include <signal. h>
# Include <unistd. h>
# Deprecision max_len 100
Void input_handler (INT num)
{
Char data [max_len];
Int Len;
// Read and output the input on stdin_fileno
Len = read (stdin_fileno, & Data, max_len );
Data [Len] = 0;
Printf ("input available: % s/n", data );
}

Main ()
{
Int oflags;

// Start the signal Driving Mechanism
Signal (sigio, input_handler );
Fcntl (stdin_fileno, f_setown, getpid ());
Oflags = fcntl (stdin_fileno, f_getfl );
Fcntl (stdin_fileno, f_setfl, oflags | fasync );

// Finally enters an endless loop, and the program does nothing. Only the signal can stimulate the input_handler operation.
// If this endless loop does not exist in the program, 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.

 

---------------------------

The above is excerpted from: http://dev.yesky.com/87/2633087.shtml

---------------------------

In UNIX system calls, stdin is used for standard input description and stdout is used for standard output. stderr is used for standard errors. However, in some call functions, stdin_fileno is referenced to indicate standard input. Similarly, stdout_fileno is used for standard access and stderr_fileno is used for standard errors.
What are their differences?
Stdin is of the file * type and belongs to the standard I/O, in <stdio. h>.
Stdin_fileno is a file descriptor and is a non-negative integer. It is generally defined as 0, 1, and 2. It belongs to the I/O without buffer and directly calls the system call in <unistd. h>.

 

(From: http://blog.csdn.net/hwz119/archive/2007/07/05/1679863.aspx)

------------

 

 

 

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.