Linux device drivers-asynchronous notifications and asynchronous I/o__linux

Source: Internet
Author: User
Tags signal handler

1, the concept and role of asynchronous notification

Impact: Blocking-The application does not need to poll the device for access

Non-blocking--notification of interrupts

That is, initiated by the driver, actively notifies the application

2. Linux Asynchronous Notification programming

2.1 Linux Signal

Role: In Linux systems, asynchronous notifications use signals to implement

2.2 processing function of the signal (capturing the signal on the application side)

Signal () function

Example:

Starting signal mechanism

Signal (Sigio,input_handler);

Fcntl (Stdin_fileno,f_setown,getpid ())// set process to Stdin_fileno file owner without this step the kernel does not know that the signal should be sent to that process .

Oflags = Fcntl (Stdin_fileno,f_getel);//

Fcntl (stdin_fileno,f_setfl,oflags| Fasync)// to enable asynchronous notification mechanism, you also need to set the FASYNC flag for the device

while (1);//finally into a dead loop, just to keep the process from terminating, if the program does not have this dead loop, it will be executed immediately

2.3 Release of the signal (Signals are released at the device driver side)

In order for the device to support asynchronous notification mechanisms, the following 3 tasks are involved in the driver

(1), Support F_setown command, can set Filp->f_owner as the corresponding process ID in this control command processing. However, this work has been done by the kernel and the device driver does not need to be processed.

(2) Support F_SETFL command processing, the Fasync () function in the driver function is executed whenever the FASYNC flag is changed. Therefore, the Fasync () function should be implemented in the driver

(3), in the device resources can be obtained, call the Kill_fasync () function to stimulate the corresponding signal

Asynchronous notification programming in device drivers:

(1), fasync_struct into the device structure template

(2), two functions

Two functions for processing fasync flags: int fasync_helper (int fd,struct file *filp,int mode,struct fasync_struct);

function to release the signal: void Kill_fasync (struct fasync_struct **fa,int sig,int band);

3. GLOBALFIFO driver supporting asynchronous notification

(1) Adding asynchronous structural body fasync_struct to GLOBALFIFO device structure information

(2), Globalfifo device-driven fasync () function that supports asynchronous notification

(3), Globalfifo device-driven write functions that support asynchronous notification

(4), increase the asynchronous notification after the GLOBALFIFO device-driven release () function

Write an application that validates the GLOBALFIFO asynchronous notification in user space

4. linux2.6 asynchronous I/O

The most commonly used input/output (I/O) model in a synchronous i/o:linux system is synchronous I/O, in which the application blocks when a request is made, knowing that the request is satisfied

Asynchronous I/O:I/O requests may need to overlap with other processes

4.1, AIO Series API:

(1), aio_read--asynchronous reading

(2), aio_write--asynchronous write

(3), aio_error--determine the status of the request

(4), aio_return--get the return value of the asynchronous operation

(5), aio_suspend--suspend asynchronous operation, until the asynchronous request is complete

(6), aio_cancel--Cancel asynchronous request

(7), lio_listio--to initiate multiple transmissions at the same time (a system call can initiate a large number of I/O operations)

4.2, use the signal as the notice of AIO

The signal as a mechanism for asynchronous notification is still used in AIO, in order to use the signal, the application that uses AIO also needs to define a signal handler, which is invoked when the specified signal is triggered.

4.3 use callback function as notification for AIO

4.4 Aio and device driver

In the kernel, each I/O request corresponds to a KIOCB structure whose KI_FILP members only want the corresponding file pointer, by IS_SYNC_KIOCB to determine whether a KIOCB is a synchronous I/O request and, if it is, to return true, Represents an asynchronous I/O request.

Block devices and network devices: they are asynchronous

Character devices: Aio must be explicitly supported (very few asynchronous I/O operations)

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.