Workqueue : schedule_delayed_work

  • In this program, you are going to learn

  • How to schedule a workqueue with a specified delay ?

  • DECLARE_DELAYED_WORK macro used to declare and initialize a delayed work. For example,

DECLARE_DELAYED_WORK(delay_workqueue, delay_workqueue_func);
  • schedule_delayed_work function to queue the delayed work item for execution on a workqueue. For example,

schedule_delayed_work(&delay_workqueue, msecs_to_jiffies(TIMEOUT));
  • cancel_delayed_work_sync is a function used to cancel a previously scheduled delayed work item that was scheduled using schedule_delayed_work. For example,

cancel_delayed_work_sync(&delay_workqueue);
  • In addition to the work item, you specify the delay in jiffies. You can use the helper functions like msecs_to_jiffies to convert milliseconds to jiffies. For example,

msecs_to_jiffies(TIMEOUT);
  • Here is the explanation of the source code.

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/workqueue.h>
#include <linux/jiffies.h>
  • Include the headers file to refer the APIs used in this program.

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Linux_usr");
MODULE_DESCRIPTION("Workqueue");
  • Add the modules macro which contains the information of module such as license, author and description.

void workqueue_func(struct work_struct *work);

DECLARE_DELAYED_WORK(delay_workqueue, delay_workqueue_func);
  • Initialize the variables and APIs which is used in this program.

static int __init queue_init(void)
{
    schedule_delayed_work(&delay_workqueue, msecs_to_jiffies(TIMEOUT));

    pr_info("Driver Loaded\n");

    return 0;
}
  • Create the module init function which will be called once the module is loaded into linux kernel.

  • schedule_work is used to schedule the workqueue.

static void __exit queue_exit(void)
{
    cancel_work_sync(&delay_workqueue);
        pr_info("Driver Unloaded...\n");
}
  • Create the module exit function which will be called once the module is unloaded into linux kernel.

void delay_workqueue_func(struct work_struct *work)
{
    pr_info("Inside Delay Workqueue function[%d]\n", count++);
}
  • Workqueue APIs is called once the work is scheduled.

  • This API will be passed as a parameter during the time of declaring the workqueue.

module_init(queue_init);
module_exit(queue_exit);
  • Mention the module init and exit functions which needs to be executed when the module is loaded and unloaded.

 1#include <linux/init.h>
 2#include <linux/kernel.h>
 3#include <linux/module.h>
 4#include <linux/fs.h>
 5#include <linux/workqueue.h>
 6#include <linux/jiffies.h>
 7
 8#define IRQ_NUMBER 1
 9#define TIMEOUT 1000
10
11static unsigned int count;
12
13MODULE_LICENSE("GPL");
14MODULE_AUTHOR("Linux_usr");
15MODULE_DESCRIPTION("Worqueue");
16
17void delay_workqueue_func(struct work_struct *work);
18
19DECLARE_DELAYED_WORK(delay_workqueue, delay_workqueue_func);
20
21void delay_workqueue_func(struct work_struct *work)
22{
23	pr_info("Inside Delay Workqueue function[%d]\n", count++);
24}
25
26static int __init queue_init(void)
27{
28	schedule_delayed_work(&delay_workqueue, msecs_to_jiffies(TIMEOUT));
29
30	pr_info("Driver Loaded...\n");
31
32	return 0;
33}
34
35static void __exit queue_exit(void)
36{
37	cancel_delayed_work_sync(&delay_workqueue);
38	pr_info("Driver unloaded\n");
39}
40
41module_init(queue_init);
42module_exit(queue_exit);
 1obj-m += work_queue.o
 2 
 3KDIR = /lib/modules/$(shell uname -r)/build
 4 
 5 
 6all:
 7	make -C $(KDIR)  M=$(shell pwd) modules
 8 
 9clean:
10	make -C $(KDIR)  M=$(shell pwd) clean
  • Run make to load the kernel module.

$ make
make -C /lib/modules/5.4.0-150-generic/build M=$HOME/kernel_work_queue modules
make[1]: Entering directory '/usr/src/linux-headers-5.4.0-150-generic'
  CC [M]  $HOME/kernel_work_queue/work_queue.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC [M]  $HOME/kernel_work_queue/work_queue.mod.o
  LD [M]  $HOME/kernel_work_queue/work_queue.ko
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-150-generic'
  • Check if work_queue.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 work_queue.c
-rw-rw-r-- 1 test test 5880 Feb 26 13:18 work_queue.ko
-rw-rw-r-- 1 test test   47 Feb 26 13:18 work_queue.mod
-rw-rw-r-- 1 test test  919 Feb 26 13:18 work_queue.mod.c
-rw-rw-r-- 1 test test 3448 Feb 26 13:18 work_queue.mod.o
-rw-rw-r-- 1 test test 3320 Feb 26 13:18 work_queue.o
  • Load the module to kernel using insmod command.

$ sudo insmod ./work_queue.ko
  • Check kernel messages to verify if the module is loaded or not.

$ dmesg
[76374.355046] Driver Loaded...
[76375.365369] Inside Delay Workqueue function[0]
  • Unload the module using rmmod command.

$ sudo rmmod work_queue
  • Check kernel messages to see if the module is unloaded or not.

$ dmesg
[76374.355046] Driver Loaded...
[76375.365369] Inside Delay Workqueue function[0]
[76455.006140] Driver unloaded

API

Learning

DECLARE_DELAYED_WORK

To declare and initialize a delayed work

schedule_delayed_work

To queue the delayed work item for execution on a workqueue

msecs_to_jiffies

To convert milliseconds

cancel_delayed_work_sync

To cancel a previously scheduled delayed work