In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to use Linux asynchronous notification technology". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Linux asynchronous notification technology.
The application layer receives the SIGIO
Like other signals, the application layer needs to register a signal processing function
Registration is still done using signal () or sigaction ()
In addition, the application layer needs to add itself to the driver's notification list, with the following code
Fcntl (dev_fd,F_SETOWN,getpid ()); int oflags = fcntl (dev_fd,F_GETFL); fcntl (dev_fd,F_SETFL,oflags | FASYNC);... while (1)
After completing the above work, the application layer program can wait for the arrival of SIGIO.
Driver sends SIGIO
After the application layer is registered, the final transmission depends on the processing mode of the device driver. in order to make the device support the asynchronous notification mechanism, refer to the interface of the application layer, there are three tasks involved in the driver.
Support the F_SETOWN command, in which you can set filp- > f_owner to the ID of the corresponding process. This part of the kernel has already done this.
F_SETFL is supported. Whenever the FASYNC flag changes, the fasync () in the driver will be executed, so, and fasync () will be implemented in the driver.
Send SIGIO through kill_fasync () when device resources are available
In order to implement the above three functions in the kernel, the driver needs to use 1 structure + 2 API, the structure is struct fasync_struct, and the functions are fasync_helper () and kill_fasync ()
Struct fasync_struct {spinlock_t fa_lock; int magic; int fa_fd; struct fasync_struct * fa_next; / * singly linked list * / struct file * fa_file; struct rcu_head fa_rcu }
The function of fasync_helper () is to register an object of fasync_struct into the kernel, and when the application layer executes fcntl (dev_fd,F_SETFL,oflags | FASYNC), the driver's fops.fasync () will be called back, so fasync_helper () is usually put into the implementation of fasync ().
/ * fasync_helper-registers a fasync_struct object into the kernel * @ fd: file descriptor, passing * @ filp:file pointer from fasync and * @ sig: signal type from fasync, usually using SIGIO * @ dev_fasync: pointer to fasync_struct object pointer * / int fasync_helper (int fd, struct file * filp, int sig, struct fasync_struct * * dev_fasync)
The following API is to release the SIGIO and place it in a different location depending on the requirements.
/ * * kill_fasync-release a signal * @ dev_fasync: use fasync_helper to register the pointer to the fasync_struct object pointer * @ filp:file beforehand, and pass * @ sig: signal type from fasync, usually using the SIGIO * @ flag: flag, usually, if the resource is readable with POLLIN, if the resource is writable with POLLOUT * / void kill_fasync (struct fasync_struct * * dev_fasync, int sig, int flag)
Driving template
The following driver template is designed to signal the application layer when the hardware interrupt arrives (resources are available). The actual operation indicates that there are many situations in which resources are available.
Static struct fasync_struct * fasync = NULL;static irqreturn_t handler (int irq, void * dev) {kill_fasync (& fasync, SIGIO, POLLIN); return IRQ_HANDLED;} static int demo_fasync (int fd, struct file * filp, int mode) {return fasync_helper (fd, filp, mode, & fasync) } struct file_operations fops = {. Fasync = demo_fasync,...} static int _ _ init demo_init (void) {. Request_irq (irq, handler, IRQF_TRIGGER_RISING, "demo", NULL);.} so far, I believe you have a better understanding of "how to use Linux asynchronous notification technology". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.