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)
------------