Asynchronous notifications:
The user program needs to complete two steps:
1, the owner of the main process is specified
Using the FCNTL system call F_setown, the process ID of the main process is saved in Filp->f_owner 2, the asynchronous notification mechanism is really enabled
Set the FASYNC flag in the device, through the Fcntl F_SETFL
After performing these two steps, the input file can request a Sigio signal when the new data arrives, which is sent to the process saved in Filp->f_owner (negative value is the process group)
Not all devices support asynchronous notifications, and we can choose not to provide asynchronous notification capabilities, and applications often assume that only sockets and terminals have the ability to communicate asynchronously. When more than one file can be notified asynchronously, the process receives the signal still must use poll or select to determine the source of the input.
Implementation in the driver:
1, F_setown is called when the Filp->f_owner assignment, in addition to do nothing
2, when executing F_SETFL enable Fasync, call the driver's Fasync method, as long as the FASYNC flag in the File->f_flags changes, the method will be called to notify the driver of this change, so that it can respond correctly. When the file is opened, the FASYNC flag is implicitly considered to be cleared.
3. When the data arrives, all processes registered as asynchronous notifications will be sent a sigio signal.
There should be an asynchronous notification list in the device data structure, and the following function adds a list:
Intfasync_helper (int fd, struct file *filp, int mode, structfasync_struct **FA);
Similar to the implementation of Wait_queue, use the following function to notify the host process of a file in the asynchronous list when the data arrives:
Intkill_fasync (struct fasync_struct **fa, int sig, int band);
Remove FILP from the asynchronous notification list when the file is closed
Positioning device:
Llseek implementation:
If you do not implement the Llseek method, the kernel modifies filp->f_pos to perform the targeting by default.
Some devices cannot be located, such as serial ports and keyboards, and should be called Onseekable_open in the Open method to notify the kernel that the device does not support Llseek:
Intnonseekable_open (struct inode* inode, struct file *filp);
These calls will mark the given FILP as non-locating, and the calls to Lseek and Pread and Pwrite will fail.
This article is from the "No Front" blog, please be sure to keep this source http://qianyang.blog.51cto.com/7130735/1620575
Asynchronous notifications and device positioning