Ways to drive asynchronous notification applications in Linux

Source: Internet
Author: User

The driver is running in kernel space and the application is running in user space and the two cannot communicate directly. But in the actual application, when the equipment is ready, we want to notify the user program device is OK, the user program can read, so that the application does not need to always query the state of the device, thereby saving resources, this is asynchronous notification. OK, so the next question is, how does this process come about? Simple, two-way work.

A driving aspect:
1. Add a struct fasync_struct pointer to the data structure of the device abstraction
2. Implement the Fasync function in device operation, this function is very simple, its main body is to call the kernel Fasync_helper function.
3. Call the kernel's Kill_fasync function where you need to notify the user space (for example, in a break).
4. Call the previously defined Fasync function in the drive release method
Oh, simple bar, just three points. Where Fasync_helper and Kill_fasync are kernel functions, we just need to invoke them. In
The pointer defined in 1 is an important parameter, and Fasync_helper and Kill_fasync use this parameter.

Two application layer aspects
1. Use signal or sigaction to set the processing function of the Sigio signal
2. Fcntl the f_setown instruction to set the current process for the device file owner
3. Fcntl F_SETFL instruction Set Fasync flag
With the above work done, when the kernel executes to the Kill_fasync function, the processing function of the user-space Sigio function is called.
Oh, it does not look very complicated, let us combine the specific code to see more clearly.
Let's start with the application layer code:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>

#define Max_len 100

Processing functions, nothing to say, user-defined
void input_handler (int num)
{
Char Data[max_len];
int Len;
Read and output input on the Stdin_fileno
Len = Read (Stdin_fileno, &data, Max_len);
Data[len] = 0;
printf ("Input available:%s\n", data);
}

void Main ()
{
int oflags;

Start the signal-driven mechanism, associate the Sigio signal with the Input_handler function, and once the Sigio signal is generated, it will execute Input_handler
Signal (SIGIO, Input_handler);

Stdin_fileno is an open device file descriptor, F_setown is used to determine what the action is, Getpid () is a system call,
The function is to return the process number of the current process, and the function of the whole function is Stdin_fileno set the owner of the device file as the current process.
Fcntl (Stdin_fileno, F_setown, Getpid ());

Get the status of the Open file descriptor
Oflags = Fcntl (Stdin_fileno, F_GETFL);

The status of the set file descriptor is Oflags | The Fasync property, once the file descriptor is set to a state with the Fasync attribute,
That is, switching the device files to asynchronous operation mode. The system automatically calls the driver's Fasync method.
Fcntl (Stdin_fileno, F_SETFL, Oflags | Fasync);

Finally into a dead loop, the program does nothing, only the signal can stimulate the operation of the Input_handler
If there is no such loop in the program, it will be executed immediately.
while (1);
}
Look at the driver layer code, the other part of the driver layer code is not changed, is to add a Fasync method implementation and some changes
The first is to define a structure, in fact, this structure is a list of the body, this
The list holds a series of device files, and the Sigio signal is sent to these devices.
static struct fasync_struct *fasync_queue;

Implementation of Fasync method
static int My_fasync (int fd, struct file * filp, int on)
{
int retval;
Register the device in the Fasync_queue queue
Retval=fasync_helper (Fd,filp,on,&fasync_queue);
if (retval<0)
{
return retval;
}
return 0;
}
In the drive release method we call the My_fasync method again.
int my_release (struct inode *inode, struct file *filp)
{
//.. Processing.
Drm_fasync ( -1, FILP, 0);
//.. Processing.
}
Then we call the following code where we need it (such as interrupts) and send a sigio signal to the device in the Fasync_queue queue.
, the application receives a signal, executes the handler
if (fasync_queue)
Kill_fasync (&fasync_queue, SIGIO, poll_in);
OK, now you know how to use the asynchronous notification mechanism?

Here are a few notes [1]:
12 Prototypes of functions
int fasync_helper (struct inode *inode, struct file *filp, int mode, struct fasync_struct **fa);
A "help person" to implement the Fasync device method. The mode parameter is the same value passed to the method, while the FA pointer points to a set
Specific Fasync_struct *

void Kill_fasync (struct fasync_struct *fa, int sig, int band);
If this driver supports asynchronous notifications, this function can be used to send a signal to the process registered in FA.

2.
Fasync_helper is used to add or remove device files to the list of devices that wait for an asynchronous signal, and Kill_fasync is used to notify the process that owns the device. Its parameters are the signals that are passed (often SIGIO) and band, which are almost poll_in[25] (but this can be used to send "emergency" or out-of-band data in the network code).

Ways to drive asynchronous notification applications in Linux

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.