Asynchronous programming in Linux drivers

Source: Internet
Author: User

Asynchronous programming in Linux drivers

A
The wait queue and polling programming described earlier provide a better mechanism for device access, but these mechanisms are
is initiated by the application and requires the application to proactively access the device. The more perfect way is by the driver master
Notification application, that is, when the driver satisfies certain conditions, it proactively notifies the application to process
, these processes are somewhat like object-oriented programming events, and the events used in the Linux kernel are the next
The signal.
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#define Max_len 100

void input_handler (int num)//capture handler function
{
Char Data[max_len];
int Len;

Len=read (Stdin_fileno,&data,max_len);
data[len]=0;
printf ("Input available:%s\n", data);
}



Main ()
{
int oflags;

signal (Sigio,input_handler); Capturing Sigio signals
The owner of the standard input device file (driver) stdin_fileno--is set to F_setown via the
The current process so that signals from the device-driven Stdin_fileno can be received by the current process.
Fcntl (Stdin_fileno,f_setown,getpid ());
Oflags=fcntl (STDIN_FILENO,F_GETFL);
//F_SETFL enables device files to support Fasync, which is asynchronous notification mode.
Fcntl (stdin_fileno,f_setfl,oflags| Fasync);

while (1);
}


B
Asynchronous notification programming in device drivers is relatively simple, mainly using a data structure and two functions.
The data structure is a FASYNC_STRUCT structure,
The two functions are as follows.
A function that handles changes to the FASYNC flag.
int fasync_helper (int fd, struct file *filp, int mode, struct fasync_struct **fa);
The function used to release the signal.
void Kill_fasync (struct fasync_struct **fa, int sig, int band);
As with other device drivers, placing the FASYNC_STRUCT structure pointer in the device structure remains the most
Best choice, the code listing gives the device structure template that supports asynchronous notifications.
struct XXX_DEV
{
struct Cdev Cdev; /*cdev Structural Body */
...
struct fasync_struct *async_queue;/* Async struct pointer * /
};
In the device-driven fasync () function, simply put 3 parameters of the function and
The pointer to the FASYNC_STRUCT struct pointer is passed into the Fasync_helper () function as a 4th parameter.

Can The code listing provides a template for the device driver Fasync () function that supports asynchronous notifications.

Fasync function to process the application's F_SETFL command

static int Xxx_fasync (int fd, struct file *filp, int mode)
{
struct Xxx_dev *dev = filp->private_data;
ReturnFasync_helper (FD, FILP, mode, &dev->async_queue);
}
static struct File_operations dev_fops=
{
. owner=.
. read= ....
. write=.
. Fasync=xxx_fasync;
}
When a device resource is available, it should call Kill_fasync () to release the SIGIO signal, which is readable when the 3rd
parameter is set to Poll_in, and the 3rd parameter is set to Poll_out when writable. The following code is the release signal
The example.
Static ssize_t xxx_write (struct file *filp, const char _ _user *buf, size_t count,loff_t *f_pos}
{
struct Xxx_dev *dev = filp->private_data;
...

if (dev->async_queue)
Kill_fasync (&dev->async_queue, SIGIO, poll_in); / * Generates an asynchronous read signal * /
...
}

Finally, when the file is closed, that is, in the device-driven release () function, you should call the device-driven
The Fasync () function removes a file from the list of asynchronous notifications. The following code listing provides support for asynchronous notifications
A template for the device driver release () function.
static int xxx_release (struct inode *inode, struct file *filp)
{
struct Xxx_dev *dev = filp->private_data;
/ * Remove the file from the asynchronous notification list */
Xxx_fasync ( -1, FILP, 0);
...
return 0;

}


Application:

Open two terminals, one to run the application, and the other to execute echo "Hello" >/dev/device name,

After executing this instruction, the driver calls the Write function, which is called in the Write function in the driver.

Kill_fasync (&dev->async_queue, SIGIO, poll_in); / * Generates an asynchronous read signal */,

So the application snaps to Sigio immediately, and the application enters the processing accept signal function.





Asynchronous programming in Linux drivers

Related Article

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.