Tasklet : Periodic
In this program, you are going to learn
How to schedule the tasklet ?
How to use below APIs ?
Topics in this section,
Tasklets are used to queue up work to be done at a later time.
DECLARE_TASKLET
macro used to create the tasklet structure and assigns the parameters to that structure. For example,
DECLARE_TASKLET(tasklet, (void * )my_tasklet_handler, 0);
tasklet_schedule
Schedule a tasklet with a normal priority. If a tasklet has previously been scheduled (but not yet run), the new schedule will be silently discarded. For example,
tasklet_schedule(&tasklet);
tasklet_kill
after a tasklet has been created, it’s possible to delete a tasklet.
tasklet_kill(&tasklet);
To schedule the tasklet periodically, again we need to schedule the tasklet inside the
my_tasklet_handler
.
void my_tasklet_handler(unsigned long data)
{
pr_info("Tasklet function executed\n");
tasklet_schedule(&tasklet);
}
Here is the explanation of the program part by part
This contains the list of all .h files which needs to be included to refer to the APIs included in this program.
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
Add the macro modules which lists out the information about the module such as license, author and description.
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Linux_usr");
MODULE_DESCRIPTION("Tasklet");
Initialize tasklet and the tasklet handler APIs.
void my_tasklet_handler(unsigned long data);
DECLARE_TASKLET(tasklet, (void *)my_tasklet_handler, 0);
Add the definitions for the module init functions which contains the instructions what the module need to do once it is loaded.
static int __init tasklet_init(void)
{
pr_info("Hello, world\n");
tasklet_schedule(&tasklet);
return 0;
}
Add the definitions for the module exit function which contains the instructions which needs to executed once the module is unloaded from the kernel.
static void __exit tasklet_exit(void)
{
tasklet_kill(&tasklet);
pr_info("Goodbye, world\n");
}
Add the function definition of the tasklet handler API since this tasklet handler API is passed as an argument in
DECLARE_TASKLET
and will be executed once thetasklet_schedule
is called.
void my_tasklet_handler(unsigned long data)
{
pr_info("Tasklet function executed\n");
tasklet_schedule(&tasklet);
}
Add the init and exit functions as a parameters to
module_init
andmodule_exit
respectively.
module_init(tasklet_init);
module_exit(tasklet_exit);
1#include <linux/init.h>
2#include <linux/kernel.h>
3#include <linux/module.h>
4#include <linux/fs.h>
5#include <linux/interrupt.h>
6
7MODULE_LICENSE("GPL");
8MODULE_AUTHOR("Linux_usr");
9MODULE_DESCRIPTION("Character device driver");
10
11void my_tasklet_handler(unsigned long data);
12DECLARE_TASKLET(tasklet, (void *)my_tasklet_handler, 0);
13
14void my_tasklet_handler(unsigned long data)
15{
16 pr_info("Tasklet function executed\n");
17 tasklet_schedule(&tasklet);
18}
19
20static int __init chardev_init(void)
21{
22 pr_info("Hello, world\n");
23 tasklet_schedule(&tasklet);
24 return 0;
25}
26
27static void __exit chardev_exit(void)
28{
29 tasklet_kill(&tasklet);
30 pr_info("Goodbye, world\n");
31}
32
33module_init(chardev_init);
34module_exit(chardev_exit);
1obj-m += tasklets.o
2all:
3 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
4clean:
5 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Run make to compile the kernel module.
$make
make -C /lib/modules/5.4.0-150-generic/build M=$HOME/kernel_tasklets modules
make[1]: Entering directory '/usr/src/linux-headers-5.4.0-150-generic'
CC [M] $HOME/kernel_tasklets/tasklets.o
Building modules, stage 2.
MODPOST 1 modules
CC [M] $HOME/kernel_tasklets/tasklets.mod.o
LD [M] $HOME/kernel_tasklets/tasklets.ko
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-150-generic'
Check whether the tasklets.ko is generated or not using ls command.
$ ls -l
total 36
-rw-rw-r-- 1 test test 154 Feb 26 13:18 Makefile
-rw-rw-r-- 1 test test 47 Feb 26 13:18 modules.order
-rw-rw-r-- 1 test test 0 Feb 26 13:18 Module.symvers
-rw-rw-r-- 1 test test 820 Feb 26 13:18 tasklets.c
-rw-rw-r-- 1 test test 5880 Feb 26 13:18 tasklets.ko
-rw-rw-r-- 1 test test 47 Feb 26 13:18 tasklets.mod
-rw-rw-r-- 1 test test 919 Feb 26 13:18 tasklets.mod.c
-rw-rw-r-- 1 test test 3448 Feb 26 13:18 tasklets.mod.o
-rw-rw-r-- 1 test test 3320 Feb 26 13:18 tasklets.o
Load the module using insmod command.
$ sudo insmod ./tasklets.ko
Check the kernel messages using dmesg to verify whether the module is loaded or not.
$ dmesg
[ 4456.243173] Tasklet function executed
[ 4456.243173] Tasklet function executed
[ 4456.243174] Tasklet function executed
[ 4456.243174] Tasklet function executed
[ 4456.243175] Tasklet function executed
[ 4456.243175] Tasklet function executed
[ 4456.243176] Tasklet function executed
[ 4456.243177] Tasklet function executed
[ 4456.243177] Tasklet function executed
[ 4456.243177] Tasklet function executed
[ 4456.243178] Tasklet function executed
[ 4456.243178] Tasklet function executed
[ 4456.243179] Tasklet function executed
[ 4456.243179] Tasklet function executed
[ 4456.243180] Tasklet function executed
[ 4456.243180] Tasklet function executed
[ 4456.243181] Tasklet function executed
[ 4456.243181] Tasklet function executed
[ 4456.246604] Tasklet function executed
[ 4456.246604] Tasklet function executed
[ 4456.246605] Tasklet function executed
[ 4456.246605] Tasklet function executed
[ 4456.246606] Tasklet function executed
[ 4456.246606] Tasklet function executed
[ 4456.246607] Tasklet function executed
[ 4456.246607] Tasklet function executed
[ 4456.246608] Tasklet function executed
[ 4456.246608] Tasklet function executed
[ 4456.246609] Tasklet function executed
[ 4456.246609] Tasklet function executed
[ 4456.246610] Tasklet function executed
[ 4456.246610] Tasklet function executed
[ 4456.246611] Tasklet function executed
[ 4456.246611] Tasklet function executed
[ 4456.246611] Tasklet function executed
[ 4456.246612] Tasklet function executed
[ 4456.246612] Tasklet function executed
[ 4456.246613] Tasklet function executed
[ 4456.246613] Tasklet function executed
[ 4456.246614] Tasklet function executed
[ 4456.246614] Tasklet function executed
Unload the module from linux kernel using rmmod command.
$ sudo rmmod tasklets
Check the kernel messages to verify whether the module is unloaded or not.
$ dmesg
[ 4456.243173] Tasklet function executed
[ 4456.243173] Tasklet function executed
[ 4456.243174] Tasklet function executed
[ 4456.243174] Tasklet function executed
[ 4456.243175] Tasklet function executed
[ 4456.243175] Tasklet function executed
[ 4456.243176] Tasklet function executed
[ 4456.243177] Tasklet function executed
[ 4456.243177] Tasklet function executed
[ 4456.243177] Tasklet function executed
[ 4456.243178] Tasklet function executed
[ 4456.243178] Tasklet function executed
[ 4456.243179] Tasklet function executed
[ 4456.243179] Tasklet function executed
[ 4456.243180] Tasklet function executed
[ 4456.243180] Tasklet function executed
[ 4456.243181] Tasklet function executed
[ 4456.243181] Tasklet function executed
[ 4456.246604] Tasklet function executed
[ 4456.246604] Tasklet function executed
[ 4456.246605] Tasklet function executed
[ 4456.246605] Tasklet function executed
[ 4456.246606] Tasklet function executed
[ 4456.246606] Tasklet function executed
[ 4456.246607] Tasklet function executed
[ 4456.246607] Tasklet function executed
[ 4456.246608] Tasklet function executed
[ 4456.246608] Tasklet function executed
[ 4456.246609] Tasklet function executed
[ 4456.246609] Tasklet function executed
[ 4456.246610] Tasklet function executed
[ 4456.246610] Tasklet function executed
[ 4456.246611] Tasklet function executed
[ 4456.246611] Tasklet function executed
[ 4456.246611] Tasklet function executed
[ 4456.246612] Tasklet function executed
[ 4456.246612] Tasklet function executed
[ 4456.246613] Tasklet function executed
[ 4456.246613] Tasklet function executed
[ 4456.246614] Tasklet function executed
[ 4456.246614] Tasklet function executed
[ 4456.246615] Goodbye, world
API |
Learning |
---|---|
DECLARE_TASKLET |
To create the tasklet structure |
tasklet_schedule |
To schedule a tasklet with a normal priority |
tasklet_kill |
This will wait for its completion and then kill it |
API |
Learning |
---|---|
pr_info |
Prints an info-level message |