Interruptible Wait Event APIs
Topics in this section,
Explanation of miscellaneous APIs
Example 1: Waiting for an event using wait_event_interruptible
Example 2: Waiting for an event using wait_event_interruptible_timeout
Example 3: Waiting for an event using wait_event_interruptible_locked
Example 4: Waiting for an event wait_event_interruptible_locked_irq
Example 5: Waiting for an event using wait_event_interruptible_exclusive_locked_irq
# |
Version |
---|---|
Ubuntu |
Ubuntu 22.10 |
Kernel |
6.7.9 |
In this program, you are going to learn
How to create waitqueue?
How to use interruptible wait event APIs?
How to wakeup the waiting threads once the task is completed?
How to use Kthread APIs ?
How to use below APIs ?
Here is the function prototype of the API: kthread_should_stop
#include <linux/kthread.h>
bool kthread_should_stop(void);
where
kthread_should_stop: this is used to determine whether the thread should return now. Whenever the kthread_stop() is called it will be woken and returns true.
return type: returns true when the thread is stopped, false when the thread is still in execution
Here is an example of how to use the API,
while (!kthread_should_stop())
{
//add the instructions to be performed during thread execution.
}
Here is the function prototype of the API: kthread_run
#include <linux/kthread.h>
#define kthread_run(threadfn, data, namefmt, ...)
where
kthread_run : is used to create and wake a kthread
return type: struct task_struct* (i.e) address of the kthread
threadfn: function to executed by kthread
data: data pointer for threadfn which can be used to send any possible arguments required for threadfn.
namefmt: printf-style format for the thread name which will be displayed on ps output when the thread is in execution.
Here is an example of how to use the API,
kthread_run(mythread,NULL,"sample kthread");
Here is the function prototype of the API: kthread_stop
#include <linux/kthread.h>
int kthread_stop(struct task_struct *k);
where
kthread_stop: stops a kthread.
return type: returns the return value of threadfn() which is passed as an argument during kthread creation.
k: kthread created by one of the API used to created kthread.
Here is the example of how to use the API,
kthread_stop(mythread);
Here is the prototype of the API: DECLARE_WAIT_QUEUE_HEAD
# include <linux/wait.h>
#define DECLARE_WAIT_QUEUE_HEAD(name) \
struct wait_queue_head name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
where
DECLARE_WAIT_QUEUE_HEAD : To declare and initialize wait queue head.
name : name which is given to the wait queue head on declaration.
Here is the example of the API,
DECLARE_WAIT_QUEUE_HEAD(mywaitqueue);
Here is the prototype of the API: wait_event_interruptible
# include <linux/wait.h>
#define wait_event_interruptible(wq_head, condition)
where
wait_event_interruptible : sleep until a condition gets true
wq_head : the waitqueue to wait on
condition : a C expression for the event to wait for
Here is an example of how to use the API
wait_event_interruptible(mywq_head, i<10);
Here is the prototype of the API: wake_event_interruptible_timeout
#include <linux/wait.h>
#define wait_event_interruptible_timeout(wq_head,condition,timeout)
where
wait_event_interruptible_timeout : sleep until a condition gets true or a timeout elapses
wq_head: the waitqueue to wait on
condition: a C expression for the event to wait for
timeout: timeout, in jiffies
Here is an example of the API
wait_event_interruptible_timeout(mywq_head,i<10,msecs_to_jiffies(100));
Here is the prototype of the API: wait_event_interruptible_locked
#include <linux/wait.h>
#define wait_event_interruptible_locked(wq, condition)
where
wait_event_interruptible_locked: sleep until a condition gets true
wq_head: the waitqueue to wait on
condition: a C expression for the event to wait for
Here is an example of how to use the API
wait_event_interruptible_lock(mywq_head,i<10);
Here is the prototype of the API: wait_event_interruptible_locked_irq
#include <linux/wait.h>
#define wait_event_interruptible_locked_irq(wq, condition)
where
wait_event_interruptible_locked_irq : sleep until a condition gets true
wq: the waitqueue to wait on
condition: a C expression for the event to wait for
Here is an example of how to use the API
wait_event_interruptible_locked_irq(mywq,i<3);
Here is the prototype of the API: wait_event_interruptible_exclusive_locked_irq
#include <linux/wait.h>
#define wait_event_interruptible_exclusive_locked_irq(wq, condition)
where
wait_event_interruptible_exclusive_locked_irq : sleep until a condition gets true
wq: the waitqueue to wait on
condition: a C expression for the event to wait for
Here is the prototype of module paramter APIs
#include <linux/module.h>
#define MODULE_LICENSE(_license) MODULE_FILE MODULE_INFO(license, _license)
#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
where
MODULE_LICENSE: tells the kernel what license is used by our module.
MODULE_AUTHOR: denotes the author of this kernel module.
MODULE_DESCRIPTION: gives a basic idea about what the kernel module does.
These information can be found when modinfo command is used which lists out all these above mentioned information.
Here is the example of how to use the Module parameter APIs,
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Linux Usr");
MODULE_DESCRIPTION("Sample kernel module");
Here is the prototype of the API: IS_ERR
#include <linux/err.h>
static inline bool __must_check IS_ERR(__force const void *ptr);
where
IS_ERR: Detects an error pointer
return type: return true if it’s an error pointer else return false.
ptr: pointer which needs to detected.
Here is an example of how to use the API,
if(IS_ERR(sample_ptr)) {
//instructions to be executed when error ptr is detected
} else {
//instructions to be executed when error ptr is not detected
}
Here is the example of the API: wake_up_process
#include <linux/sched.h>
extern int wake_up_process(struct task_struct *tsk);
where
wake_up_process: wake up a specific process
return type: returns 1 if the process is woken up, 0 if the process is in running state.
tsk: process to be woken up.
Here is the example of how to use the API,
wake_up_process(mythread);
Here is the prototype of the Driver entry point API’s
#include <linux/module.h>
#define module_init(x) __initcall(x);
#define module_exit(x) __exitcall(x);
where
module_init: driver initialization entry point which will be called at module insertion time.
module_exit: driver exit entry point which will be called during the removal of module.
- x:
function to be run at module insertion for module_init function.
function to be run when driver is removed for module_exit function.
Here is an example of how to use the driver entry point API’s
module_init(myinitmodule);
module_exit(myexitmodule);
In this example let’s see how to wait for an event using wait_event_interruptible.
Include the follow header files(.h) to refer the API being used for the execution.
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/wait.h>
#include <linux/delay.h>
Add the following module macros to display information about the license, author and description about the module.
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Linux Usr");
MODULE_DESCRIPTION("Example of wait_event_interruptible");
Declare the thread variables which we are going to create and use in this example
static struct task_struct *wait_thread;
static struct task_struct *wake_thread;
static DECLARE_WAIT_QUEUE_HEAD(wqueue);
wait_queue_entry_t wait;
int wait_flag = 0;
Add the module init function which will be executed once we load the kernel module using insmod command.
static int __init wait_event_interruptible_init(void)
{
pr_info("Inside kthread init function\n");
thread_start();
return 0;
}
Add the thread start function called from the module init function which is used to create the thread and execute it.
void thread_start(void)
{
pr_info("initialized waitqueue head\n");
wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event_interruptible wake_thread example");
if (IS_ERR(wake_thread))
pr_info("error creating thread\n");
else
pr_info("kthread created successfully\n");
wait_thread = kthread_run(wait_thread_fn,NULL,"wait_event_interruptible wait_thread example");
if(IS_ERR(wait_thread))
pr_info("error creating thread\n");
else
pr_info("kthread created successfully\n");
}
Add the module exit function which will be executed once we unload the kernel module using rmmod command.
static void __exit wait_event_interruptible_exit(void)
{
pr_info("Inside kthread exit function\n");
thread_stop();
}
Add the thread stop function called from the module exit function which is used to destroy the thread and stop its execution.
void thread_stop(void)
{
kthread_stop(wake_thread);
kthread_stop(wait_thread);
pr_info("destroyed threads and completed tasks\n");
}
Add the thread function API which will be called as soon as the kthread is created and is in running state.
int wait_thread_fn(void *data) {
while(!kthread_should_stop()) {
wait_event_interruptible(wqueue,i > 3);
pr_info("wait thread execution i value = %d\n",i);
msleep(1000);
}
return 0;
}
int wake_thread_fn(void *data)
{
while(!kthread_should_stop()) {
pr_info("wake thread execution i value = %d\n",i);
i++;
wake_up(&wqueue);
msleep(1000);
}
return 0;
}
Add the driver entry points which will be executed once the module is inserted or removed from the kernel.
module_init(wait_event_interruptible_init);
module_exit(wait_event_interruptible_exit);
1#include <linux/module.h>
2#include <linux/init.h>
3#include <linux/kernel.h>
4#include <linux/kthread.h>
5#include <linux/delay.h>
6#include <linux/sched.h>
7#include <linux/wait.h>
8
9MODULE_LICENSE("GPL");
10MODULE_AUTHOR("linux usr");
11MODULE_DESCRIPTION("Example of wait_event_interruptible");
12
13static struct task_struct *wait_thread;
14static struct task_struct *wake_thread;
15DECLARE_WAIT_QUEUE_HEAD(wqueue);
16int i = 0;
17
18/* wait_thread_fn - executes when wait_thread is created,
19 * waits for an event to be completed by wake_thread,
20 * prints a message and sleeps for 1000ms,
21 * stops when kthread_stop is called */
22
23int wait_thread_fn(void *data) {
24 while(!kthread_should_stop()) {
25 wait_event_interruptible(wqueue,i > 3);
26 pr_info("wait thread execution i value = %d\n",i);
27 msleep(1000);
28 }
29 return 0;
30}
31
32/* wake_thread_fn - executes when wake_thread is created,
33 * prints a message, increments the value and sleeps for 1000ms,
34 * sends wake_up signal to check the condition in wait_thread,
35 * stops when kthread_stop is called */
36
37int wake_thread_fn(void *data)
38{
39 while(!kthread_should_stop()) {
40 pr_info("wake thread execution i value = %d\n",i);
41 i++;
42 wake_up(&wqueue);
43 msleep(1000);
44 }
45 return 0;
46}
47
48/* thread_start - creates thread and starts execution */
49
50void thread_start(void)
51{
52 wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event_interruptible wake_thread example");
53 if (IS_ERR(wake_thread))
54 pr_info("error in creating wake thread\n");
55 else
56 pr_info("successfully created wake thread\n");
57 wait_thread = kthread_run(wait_thread_fn,NULL,"wait_event_interruptible wait_thread example");
58 if (IS_ERR(wait_thread))
59 pr_info("error in creating wait thread\n");
60 else
61 pr_info("successfully created wait thread\n");
62}
63
64/* wait_event_interruptible_init - calls thread_start,
65 * executes when module is loaded */
66
67static int __init wait_event_interruptible_init(void)
68{
69 pr_info("inside kthread init function\n");
70 thread_start();
71 return 0;
72}
73
74/* thread_stop - destroys thread and stops execution */
75
76void thread_stop(void)
77{
78 kthread_stop(wake_thread);
79 kthread_stop(wait_thread);
80 pr_info("destroyed threads and completed tasks\n");
81}
82
83/* wait_event_interruptible_exit - calls thread_stop,
84 * executes when module is unloaded */
85
86static void __exit wait_event_interruptible_exit(void)
87{
88 pr_info("inside kthread exit function\n");
89 thread_stop();
90}
91
92module_init(wait_event_interruptible_init);
93module_exit(wait_event_interruptible_exit);
1obj-m += kthread.o
2
3all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5
6clean:
7 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Run make to compile the kernel source and generate the .ko image.
make -C /lib/modules/6.7.9/build M=$HOME/kthread_examples/
make[1]: Entering directory '/usr/src/linux-headers-6.7.9'
warning: the compiler differs from the one used to build the kernel
The kernel was built by: x86_64-linux-gnu-gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0
You are using: gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0
CC [M] $HOME/kthread_examples/kthread.o
MODPOST $HOME/kthread_examples/Module.symvers
CC [M] $HOME/kthread_examples/kthread.mod.o
LD [M] $HOME/kthread_examples/kthread.ko
BTF [M] $HOME/kthread_examples/kthread.ko
Skipping BTF generation for $HOME/kthread_examples/kthread.ko due to unavailability of vmlinux
make[1]: Leaving directory '/usr/src/linux-headers-6.7.9'
Check if the .ko is generated or not using ls command.
test@test-V520-15IKL:~$ ls -l
total 360
-rw-rw-r-- 1 test test 713 Mar 18 16:06 kthread.c
-rw-rw-r-- 1 test test 169784 Mar 18 16:08 kthread.ko
-rw-rw-r-- 1 test test 58 Mar 18 16:08 kthread.mod
-rw-rw-r-- 1 test test 1047 Mar 18 16:08 kthread.mod.c
-rw-rw-r-- 1 test test 96512 Mar 18 16:08 kthread.mod.o
-rw-rw-r-- 1 test test 74696 Mar 18 16:08 kthread.o
-rw-rw-r-- 1 test test 161 Mar 18 16:00 Makefile
-rw-rw-r-- 1 test test 58 Mar 18 16:08 modules.order
-rw-rw-r-- 1 test test 0 Mar 18 16:08 Module.symvers
Run modinfo command to get the information about the kernel module.
test@test-V520-15IKL:~/.../tc_1$ modinfo kthread.ko
filename: $HOME/kthread_examples/kthread.ko
description: Example of wait_event_interruptible
author: Linux Usr
license: GPL
srcversion: 8D2147F67AB01CF0E482DAC
depends:
retpoline: Y
name: kthread
vermagic: 6.7.9 SMP preempt mod_unload modversions
insert the module using insmod command.
test@test-V520-15IKL:~$ sudo insmod ./kthread.ko
check if the module is loaded or not using lsmod command.
test@test-V520-15IKL:~$ sudo lsmod | grep kthread
kthread 16384 0
check if the thread is created or not using ps command.
test@test-V520-15IKL:~$ ps -N | grep wait_event_interruptible
10002 ? 00:00:00 wait_event_interruptible wake_thread example
10003 ? 00:00:00 wait_event_interruptible wait_thread example
check for the kernel messages from init function and thread function once the module is loaded and thread is created.
test@test-V520-15IKL:~$ sudo dmesg
[14725.272797] inside kthread init function
[14725.272885] successfully created wake thread
[14725.272888] wake thread execution i value = 0
[14725.272905] successfully created wait thread
[14726.280279] wake thread execution i value = 1
[14727.304261] wake thread execution i value = 2
[14728.328289] wake thread execution i value = 3
[14728.328403] wait thread execution i value = 4
[14729.352301] wake thread execution i value = 4
[14729.352303] wait thread execution i value = 4
[14730.376299] wake thread execution i value = 5
remove the module from kernel using rmmod command.
test@test-V520-15IKL:~$ sudo rmmod kthread
check if the module is still loaded after removing the kernel module using lsmod if it is not displayed in lsmod output it is verified that the module is removed successfully.
test@test-V520-15IKL:~$ sudo lsmod | grep kthread
test@test-V520-15IKL:~$
check if the thread is destroyed using ps command if it is not displayed in ps output we can confirm that the thread is destroyed successfully.
test@test-V520-15IKL:~$ ps -N | grep wait_event_interruptible
test@test-V520-15IKL:~$
Check for kernel messages from exit function using dmesg command.
test@test-V520-15IKL:~$ sudo dmesg
[14734.186330] inside kthread exit function
[14735.496434] destroyed threads and completed tasks
In this example let’s see how to wait for an event using wait_event_interruptible_timeout.
Include the follow header files(.h) to refer the API being used for the execution.
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/wait.h>
#include <linux/delay.h>
Add the following module macros to display information about the license, author and description about the module.
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Linux Usr");
MODULE_DESCRIPTION("Example of wait_event_interruptible_timeout");
Declare the thread variables which we are going to create and use in this example
static struct task_struct *wait_thread;
static struct task_struct *wake_thread;
static DECLARE_WAIT_QUEUE_HEAD(wqueue);
int i = 0;
Add the module init function which will be executed once we load the kernel module using insmod command.
static int __init wait_event_interruptible_timeout_init(void)
{
pr_info("Inside kthread init function\n");
thread_start();
return 0;
}
Add the thread start function called from the module init function which is used to create the thread and execute it.
void thread_start(void)
{
pr_info("initialized waitqueue head\n");
wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event_interruptible_timeout wake_thread example");
if (IS_ERR(wake_thread))
pr_info("error creating thread\n");
else
pr_info("kthread created successfully\n");
wait_thread = kthread_run(wait_thread_fn,NULL,"wait_event_interruptible_timeout wait_thread example");
if(IS_ERR(wait_thread))
pr_info("error creating thread\n");
else
pr_info("kthread created successfully\n");
}
Add the module exit function which will be executed once we unload the kernel module using rmmod command.
static void __exit wait_event_interruptible_timeout_exit(void)
{
pr_info("Inside kthread exit function\n");
thread_stop();
}
Add the thread stop function called from the module exit function which is used to destroy the thread and stop its execution.
void thread_stop(void)
{
kthread_stop(wake_thread);
kthread_stop(wait_thread);
pr_info("destroyed threads and completed tasks\n");
}
Add the thread function API which will be called as soon as the kthread is created and is in running state.
int wait_thread_fn(void *data) {
while(!kthread_should_stop()) {
wait_event_interruptible_timeout(wqueue,i > 3,msecs_to_jiffies(100));
pr_info("wait thread execution i value = %d\n",i);
msleep(1000);
}
return 0;
}
int wake_thread_fn(void *data)
{
while(!kthread_should_stop()) {
pr_info("wake thread execution i value = %d\n",i);
i++;
wake_up(&wqueue);
msleep(1000);
}
return 0;
}
Add the driver entry points which will be executed once the module is inserted or removed from the kernel.
module_init(wait_event_interruptible_timeout_init);
module_exit(wait_event_interruptible_timeout_exit);
1#include <linux/module.h>
2#include <linux/init.h>
3#include <linux/kernel.h>
4#include <linux/kthread.h>
5#include <linux/delay.h>
6#include <linux/sched.h>
7#include <linux/wait.h>
8
9MODULE_LICENSE("GPL");
10MODULE_AUTHOR("linux usr");
11MODULE_DESCRIPTION("Example of wait_event_interruptible_timeout");
12
13static struct task_struct *wait_thread;
14static struct task_struct *wake_thread;
15DECLARE_WAIT_QUEUE_HEAD(wqueue);
16int i = 0;
17
18/* wait_thread_fn - executes when wait_thread is created,
19 * waits for an event to be completed by wake_thread,
20 * prints a message and sleeps for 1000ms,
21 * stops when kthread_stop is called */
22
23int wait_thread_fn(void *data) {
24 while(!kthread_should_stop()) {
25 wait_event_interruptible_timeout(wqueue,i > 3,msecs_to_jiffies(1000));
26 pr_info("wait thread execution i value = %d\n",i);
27 msleep(1000);
28 }
29 return 0;
30}
31
32/* wake_thread_fn - executes when wake_thread is created,
33 * prints a message, increments the value and sleeps for 1000ms,
34 * sends wake_up signal to check the condition in wait_thread,
35 * stops when kthread_stop is called */
36
37int wake_thread_fn(void *data)
38{
39 while(!kthread_should_stop()) {
40 pr_info("wake thread execution i value = %d\n",i);
41 i++;
42 wake_up(&wqueue);
43 msleep(1000);
44 }
45 return 0;
46}
47
48/* thread_start - creates thread and starts execution */
49
50void thread_start(void)
51{
52 wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event_interruptible_timeout wake_thread example");
53 if (IS_ERR(wake_thread))
54 pr_info("error in creating wake thread\n");
55 else
56 pr_info("successfully created wake thread\n");
57 wait_thread = kthread_run(wait_thread_fn,NULL,"wait_event_interruptible_timeout wait_thread example");
58 if (IS_ERR(wait_thread))
59 pr_info("error in creating wait thread\n");
60 else
61 pr_info("successfully created wait thread\n");
62}
63
64/* wait_event_interruptible_timeout_init - calls thread_start,
65 * executes when module is loaded */
66
67static int __init wait_event_interruptible_timeout_init(void)
68{
69 pr_info("inside kthread init function\n");
70 thread_start();
71 return 0;
72}
73
74/* thread_stop - destroys thread and stops execution */
75
76void thread_stop(void)
77{
78 kthread_stop(wake_thread);
79 kthread_stop(wait_thread);
80 pr_info("destroyed threads and completed tasks\n");
81}
82
83/* wait_event_interruptible_timeout_exit - calls thread_stop,
84 * executes when module is unloaded */
85
86static void __exit wait_event_interruptible_timeout_exit(void)
87{
88 pr_info("inside kthread exit function\n");
89 thread_stop();
90}
91
92module_init(wait_event_interruptible_timeout_init);
93module_exit(wait_event_interruptible_timeout_exit);
1obj-m += kthread.o
2
3all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5
6clean:
7 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Run make to compile the kernel source and generate the .ko image.
make -C /lib/modules/6.7.9/build M=$HOME/kthread_examples/
make[1]: Entering directory '/usr/src/linux-headers-6.7.9'
warning: the compiler differs from the one used to build the kernel
The kernel was built by: x86_64-linux-gnu-gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0
You are using: gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0
CC [M] $HOME/kthread_examples/kthread.o
MODPOST $HOME/kthread_examples/Module.symvers
CC [M] $HOME/kthread_examples/kthread.mod.o
LD [M] $HOME/kthread_examples/kthread.ko
BTF [M] $HOME/kthread_examples/kthread.ko
Skipping BTF generation for $HOME/kthread_examples/kthread.ko due to unavailability of vmlinux
make[1]: Leaving directory '/usr/src/linux-headers-6.7.9'
Check if the .ko is generated or not using ls command.
test@test-V520-15IKL:~$ ls -l
total 360
-rw-rw-r-- 1 test test 713 Mar 18 16:06 kthread.c
-rw-rw-r-- 1 test test 169784 Mar 18 16:08 kthread.ko
-rw-rw-r-- 1 test test 58 Mar 18 16:08 kthread.mod
-rw-rw-r-- 1 test test 1047 Mar 18 16:08 kthread.mod.c
-rw-rw-r-- 1 test test 96512 Mar 18 16:08 kthread.mod.o
-rw-rw-r-- 1 test test 74696 Mar 18 16:08 kthread.o
-rw-rw-r-- 1 test test 161 Mar 18 16:00 Makefile
-rw-rw-r-- 1 test test 58 Mar 18 16:08 modules.order
-rw-rw-r-- 1 test test 0 Mar 18 16:08 Module.symvers
Run modinfo command to get the information about the kernel module.
test@test-V520-15IKL:~/.../tc_1$ modinfo kthread.ko
filename: $HOME/kthread_examples/kthread.ko
description: Example of wait_event_interruptible_timeout
author: Linux Usr
license: GPL
srcversion: 8D2147F67AB01CF0E482DAC
depends:
retpoline: Y
name: kthread
vermagic: 6.7.9 SMP preempt mod_unload modversions
insert the module using insmod command.
test@test-V520-15IKL:~$ sudo insmod ./kthread.ko
check if the module is loaded or not using lsmod command.
test@test-V520-15IKL:~$ sudo lsmod | grep kthread
kthread 16384 0
check if the thread is created or not using ps command.
test@test-V520-15IKL:~$ ps -N | grep wait_event_interruptible_timeout
11202 ? 00:00:00 wait_event_interruptible_timeout wake_thread example
11203 ? 00:00:00 wait_event_interruptible_timeout wait_thread example
check for the kernel messages from init function and thread function once the module is loaded and thread is created.
test@test-V520-15IKL:~$ sudo dmesg
[15872.375515] inside kthread init function
[15872.375597] successfully created wake thread
[15872.375599] wake thread execution i value = 0
[15872.375664] successfully created wait thread
[15873.381504] wait thread execution i value = 1
[15873.381503] wake thread execution i value = 1
[15874.405544] wake thread execution i value = 2
[15875.429546] wait thread execution i value = 3
[15875.433549] wake thread execution i value = 3
[15876.453574] wake thread execution i value = 4
[15876.453576] wait thread execution i value = 4
[15877.477618] wake thread execution i value = 5
remove the module from kernel using rmmod command.
test@test-V520-15IKL:~$ sudo rmmod kthread
check if the module is still loaded after removing the kernel module using lsmod if it is not displayed in lsmod output it is verified that the module is removed successfully.
test@test-V520-15IKL:~$ sudo lsmod | grep kthread
test@test-V520-15IKL:~$
check if the thread is destroyed using ps command if it is not displayed in ps output we can confirm that the thread is destroyed successfully.
test@test-V520-15IKL:~$ ps -N | grep wait_event_interruptible_timeout
test@test-V520-15IKL:~$
Check for kernel messages from exit function using dmesg command.
test@test-V520-15IKL:~$ sudo dmesg
[15885.710747] inside kthread exit function
[15887.718020] destroyed threads and completed tasks
In this example let’s see how to wait for an event using wait_event_interruptible_locked
Include the follow header files(.h) to refer the API being used for the execution.
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/wait.h>
#include <linux/delay.h>
Add the following module macros to display information about the license, author and description about the module.
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Linux Usr");
MODULE_DESCRIPTION("Example of wait_event_interruptible_locked");
Declare the thread variables which we are going to create and use in this example
static struct task_struct *wait_thread;
static struct task_struct *wake_thread;
static DECLARE_WAIT_QUEUE_HEAD(wqueue);
int i = 0;
Add the module init function which will be executed once we load the kernel module using insmod command.
static int __init wait_event_interruptible_locked_init(void)
{
pr_info("Inside kthread init function\n");
thread_start();
return 0;
}
Add the thread start function called from the module init function which is used to create the thread and execute it.
void thread_start(void)
{
pr_info("initialized waitqueue head\n");
wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event_interruptible_locked wake_thread example");
if (IS_ERR(wake_thread))
pr_info("error creating thread\n");
else
pr_info("kthread created successfully\n");
wait_thread = kthread_run(wait_thread_fn,NULL,"wait_event_interruptible_locked wait_thread example");
if(IS_ERR(wait_thread))
pr_info("error creating thread\n");
else
pr_info("kthread created successfully\n");
}
Add the module exit function which will be executed once we unload the kernel module using rmmod command.
static void __exit wait_event_interruptible_locked_exit(void)
{
pr_info("Inside kthread exit function\n");
thread_stop();
}
Add the thread stop function called from the module exit function which is used to destroy the thread and stop its execution.
void thread_stop(void)
{
kthread_stop(wake_thread);
kthread_stop(wait_thread);
pr_info("destroyed threads and completed tasks\n");
}
Add the thread function API which will be called as soon as the kthread is created and is in running state.
int wait_thread_fn(void *data) {
while(!kthread_should_stop()) {
spin_lock(&wqueue.lock);
wait_event_interruptible_locked(wqueue,i > 3);
pr_info("wait thread execution i value = %d\n",i);
spin_unlock(&wqueue.lock);
msleep(1000);
}
return 0;
}
int wake_thread_fn(void *data)
{
while(!kthread_should_stop()) {
pr_info("wake thread execution i value = %d\n",i);
i++;
wake_up(&wqueue);
msleep(1000);
}
return 0;
}
Add the driver entry points which will be executed once the module is inserted or removed from the kernel.
module_init(wait_event_interruptible_locked_init);
module_exit(wait_event_interruptible_locked_exit);
1#include <linux/module.h>
2#include <linux/init.h>
3#include <linux/kernel.h>
4#include <linux/kthread.h>
5#include <linux/delay.h>
6#include <linux/sched.h>
7#include <linux/wait.h>
8
9MODULE_LICENSE("GPL");
10MODULE_AUTHOR("linux usr");
11MODULE_DESCRIPTION("Example of wait_event_interruptible_locked");
12
13static struct task_struct *wait_thread;
14static struct task_struct *wake_thread;
15DECLARE_WAIT_QUEUE_HEAD(wqueue);
16int i = 0;
17
18/* wait_thread_fn - executes when wait_thread is created,
19 * waits for an event to be completed by wake_thread,
20 * prints a message and sleeps for 1000ms,
21 * stops when kthread_stop is called */
22
23int wait_thread_fn(void *data) {
24 while(!kthread_should_stop()) {
25 spin_lock(&wqueue.lock);
26 wait_event_interruptible_locked(wqueue,i > 3);
27 pr_info("wait thread execution i value = %d\n",i);
28 spin_unlock(&wqueue.lock);
29 msleep(1000);
30 }
31 return 0;
32}
33
34/* wake_thread_fn - executes when wake_thread is created,
35 * prints a message, increments the value and sleeps for 1000ms,
36 * sends wake_up signal to check the condition in wait_thread,
37 * stops when kthread_stop is called */
38
39int wake_thread_fn(void *data)
40{
41 while(!kthread_should_stop()) {
42 pr_info("wake thread execution i value = %d\n",i);
43 i++;
44 wake_up(&wqueue);
45 msleep(1000);
46 }
47 return 0;
48}
49
50/* thread_start - creates thread and starts execution */
51
52void thread_start(void)
53{
54 wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event_interruptible_locked wake_thread example");
55 if (IS_ERR(wake_thread))
56 pr_info("error in creating wake thread\n");
57 else
58 pr_info("successfully created wake thread\n");
59 wait_thread = kthread_run(wait_thread_fn,NULL,"wait_event_interruptible_locked wait_thread example");
60 if (IS_ERR(wait_thread))
61 pr_info("error in creating wait thread\n");
62 else
63 pr_info("successfully created wait thread\n");
64}
65
66/* wait_event_interruptible_locked_init - calls thread_start,
67 * executes when module is loaded */
68
69static int __init wait_event_interruptible_locked_init(void)
70{
71 pr_info("inside kthread init function\n");
72 thread_start();
73 return 0;
74}
75
76/* thread_stop - destroys thread and stops execution */
77
78void thread_stop(void)
79{
80 kthread_stop(wake_thread);
81 kthread_stop(wait_thread);
82 pr_info("destroyed threads and completed tasks\n");
83}
84
85/* wait_event_interruptible_locked_exit - calls thread_stop,
86 * executes when module is unloaded */
87
88static void __exit wait_event_interruptible_locked_exit(void)
89{
90 pr_info("inside kthread exit function\n");
91 thread_stop();
92}
93
94module_init(wait_event_interruptible_locked_init);
95module_exit(wait_event_interruptible_locked_exit);
1obj-m += kthread.o
2
3all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5
6clean:
7 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Run make to compile the kernel source and generate the .ko image.
make -C /lib/modules/6.7.9/build M=$HOME/kthread_examples/
make[1]: Entering directory '/usr/src/linux-headers-6.7.9'
warning: the compiler differs from the one used to build the kernel
The kernel was built by: x86_64-linux-gnu-gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0
You are using: gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0
CC [M] $HOME/kthread_examples/kthread.o
MODPOST $HOME/kthread_examples/Module.symvers
CC [M] $HOME/kthread_examples/kthread.mod.o
LD [M] $HOME/kthread_examples/kthread.ko
BTF [M] $HOME/kthread_examples/kthread.ko
Skipping BTF generation for $HOME/kthread_examples/kthread.ko due to unavailability of vmlinux
make[1]: Leaving directory '/usr/src/linux-headers-6.7.9'
Check if the .ko is generated or not using ls command.
test@test-V520-15IKL:~$ ls -l
total 360
-rw-rw-r-- 1 test test 713 Mar 18 16:06 kthread.c
-rw-rw-r-- 1 test test 169784 Mar 18 16:08 kthread.ko
-rw-rw-r-- 1 test test 58 Mar 18 16:08 kthread.mod
-rw-rw-r-- 1 test test 1047 Mar 18 16:08 kthread.mod.c
-rw-rw-r-- 1 test test 96512 Mar 18 16:08 kthread.mod.o
-rw-rw-r-- 1 test test 74696 Mar 18 16:08 kthread.o
-rw-rw-r-- 1 test test 161 Mar 18 16:00 Makefile
-rw-rw-r-- 1 test test 58 Mar 18 16:08 modules.order
-rw-rw-r-- 1 test test 0 Mar 18 16:08 Module.symvers
Run modinfo command to get the information about the kernel module.
test@test-V520-15IKL:~/.../tc_1$ modinfo kthread.ko
filename: $HOME/kthread_examples/kthread.ko
description: Example of wait_event_interruptible_locked
author: Linux Usr
license: GPL
srcversion: 8D2147F67AB01CF0E482DAC
depends:
retpoline: Y
name: kthread
vermagic: 6.7.9 SMP preempt mod_unload modversions
insert the module using insmod command.
test@test-V520-15IKL:~$ sudo insmod ./kthread.ko
check if the module is loaded or not using lsmod command.
test@test-V520-15IKL:~$ sudo lsmod | grep kthread
kthread 16384 0
check if the thread is created or not using ps command.
test@test-V520-15IKL:~$ ps -N | grep wait_event_interruptible_locked
15393 ? 00:00:00 wait_event_interruptible_locked wake_thread example
15394 ? 00:00:00 wait_event_interruptible_locked wait_thread example
check for the kernel messages from init function and thread function once the module is loaded and thread is created.
test@test-V520-15IKL:~$ sudo dmesg
[27764.569719] inside kthread init function
[27764.569747] successfully created wake thread
[27764.569749] wake thread execution i value = 0
[27764.569765] successfully created wait thread
[27765.579192] wake thread execution i value = 1
[27766.599227] wake thread execution i value = 2
[27767.623052] wake thread execution i value = 3
[27767.623194] wait thread execution i value = 4
[27768.647208] wake thread execution i value = 4
[27768.647208] wait thread execution i value = 4
[27769.671197] wake thread execution i value = 5
remove the module from kernel using rmmod command.
test@test-V520-15IKL:~$ sudo rmmod kthread
check if the module is still loaded after removing the kernel module using lsmod if it is not displayed in lsmod output it is verified that the module is removed successfully.
test@test-V520-15IKL:~$ sudo lsmod | grep kthread
test@test-V520-15IKL:~$
check if the thread is destroyed using ps command if it is not displayed in ps output we can confirm that the thread is destroyed successfully.
test@test-V520-15IKL:~$ ps -N | grep wait_event_interruptible_locked
test@test-V520-15IKL:~$
Check for kernel messages from exit function using dmesg command.
test@test-V520-15IKL:~$ sudo dmesg
[27774.940643] inside kthread exit function
[27776.839328] destroyed threads and completed tasks
In this example let’s see how to wait for an event using wait_event_interruptible_locked_irq.
Include the follow header files(.h) to refer the API being used for the execution.
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/wait.h>
#include <linux/delay.h>
Add the following module macros to display information about the license, author and description about the module.
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Linux Usr");
MODULE_DESCRIPTION("Example of wait_event_interruptible_locked_irq");
Declare the thread variables which we are going to create and use in this example
static struct task_struct *wait_thread;
static struct task_struct *wake_thread;
static DECLARE_WAIT_QUEUE_HEAD(wqueue);
wait_queue_entry_t wait;
int wait_flag = 0;
Add the module init function which will be executed once we load the kernel module using insmod command.
static int __init wait_event_interruptible_locked_irq_init(void)
{
pr_info("Inside kthread init function\n");
thread_start();
return 0;
}
Add the thread start function called from the module init function which is used to create the thread and execute it.
void thread_start(void)
{
pr_info("initialized waitqueue head\n");
wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event_interruptible_locked_irq wake_thread example");
if (IS_ERR(wake_thread))
pr_info("error creating thread\n");
else
pr_info("kthread created successfully\n");
wait_thread = kthread_run(wait_thread_fn,NULL,"wait_event_interruptible_locked_irq wait_thread example");
if(IS_ERR(wait_thread))
pr_info("error creating thread\n");
else
pr_info("kthread created successfully\n");
}
Add the module exit function which will be executed once we unload the kernel module using rmmod command.
static void __exit wait_event_interruptible_locked_irq_exit(void)
{
pr_info("Inside kthread exit function\n");
thread_stop();
}
Add the thread stop function called from the module exit function which is used to destroy the thread and stop its execution.
void thread_stop(void)
{
kthread_stop(wake_thread);
kthread_stop(wait_thread);
pr_info("destroyed threads and completed tasks\n");
}
Add the thread function API which will be called as soon as the kthread is created and is in running state.
int wait_thread_fn(void *data) {
unsigned long flags;
while(!kthread_should_stop()) {
spin_lock_irqsave(&wqueue.lock,flags);
wait_event_interruptible_locked_irq(wqueue,i > 3);
pr_info("wait thread execution i value = %d\n",i);
spin_unlock_irqrestore(&wqueue.lock,flags);
msleep(1000);
}
return 0;
}
int wake_thread_fn(void *data)
{
while(!kthread_should_stop()) {
pr_info("wake thread execution i value = %d\n",i);
i++;
wake_up(&wqueue);
msleep(1000);
}
return 0;
}
Add the driver entry points which will be executed once the module is inserted or removed from the kernel.
module_init(wait_event_interruptible_locked_irq_init);
module_exit(wait_event_interruptible_locked_irq_exit);
1#include <linux/module.h>
2#include <linux/init.h>
3#include <linux/kernel.h>
4#include <linux/kthread.h>
5#include <linux/delay.h>
6#include <linux/sched.h>
7#include <linux/wait.h>
8
9MODULE_LICENSE("GPL");
10MODULE_AUTHOR("linux usr");
11MODULE_DESCRIPTION("Example of wait_event_interruptible_locked_irq");
12
13static struct task_struct *wait_thread;
14static struct task_struct *wake_thread;
15DECLARE_WAIT_QUEUE_HEAD(wqueue);
16int i = 0;
17
18/* wait_thread_fn - executes when wait_thread is created,
19 * waits for an event to be completed by wake_thread,
20 * prints a message and sleeps for 1000ms,
21 * stops when kthread_stop is called */
22
23int wait_thread_fn(void *data) {
24 unsigned long flags;
25 while(!kthread_should_stop()) {
26 spin_lock_irqsave(&wqueue.lock,flags);
27 wait_event_interruptible_locked_irq(wqueue,i > 3);
28 pr_info("wait thread execution i value = %d\n",i);
29 spin_unlock_irqrestore(&wqueue.lock,flags);
30 msleep(1000);
31 }
32 return 0;
33}
34
35/* wake_thread_fn - executes when wake_thread is created,
36 * prints a message, increments the value and sleeps for 1000ms,
37 * sends wake_up signal to check the condition in wait_thread,
38 * stops when kthread_stop is called */
39
40int wake_thread_fn(void *data)
41{
42 while(!kthread_should_stop()) {
43 pr_info("wake thread execution i value = %d\n",i);
44 i++;
45 wake_up(&wqueue);
46 msleep(1000);
47 }
48 return 0;
49}
50
51/* thread_start - creates thread and starts execution */
52
53void thread_start(void)
54{
55 wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event_interruptible_locked_irq wake_thread example");
56 if (IS_ERR(wake_thread))
57 pr_info("error in creating wake thread\n");
58 else
59 pr_info("successfully created wake thread\n");
60 wait_thread = kthread_run(wait_thread_fn,NULL,"wait_event_interruptible_locked_irq wait_thread example");
61 if (IS_ERR(wait_thread))
62 pr_info("error in creating wait thread\n");
63 else
64 pr_info("successfully created wait thread\n");
65}
66
67/* wait_event_interruptible_locked_irq_init - calls thread_start,
68 * executes when module is loaded */
69
70static int __init wait_event_interruptible_locked_irq_init(void)
71{
72 pr_info("inside kthread init function\n");
73 thread_start();
74 return 0;
75}
76
77/* thread_stop - destroys thread and stops execution */
78
79void thread_stop(void)
80{
81 kthread_stop(wake_thread);
82 kthread_stop(wait_thread);
83 pr_info("destroyed threads and completed tasks\n");
84}
85
86/* wait_event_interruptible_locked_irq_exit - calls thread_stop,
87 * executes when module is unloaded */
88
89static void __exit wait_event_interruptible_locked_irq_exit(void)
90{
91 pr_info("inside kthread exit function\n");
92 thread_stop();
93}
94
95module_init(wait_event_interruptible_locked_irq_init);
96module_exit(wait_event_interruptible_locked_irq_exit);
1obj-m += kthread.o
2
3all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5
6clean:
7 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Run make to compile the kernel source and generate the .ko image.
make -C /lib/modules/6.7.9/build M=$HOME/kthread_examples/
make[1]: Entering directory '/usr/src/linux-headers-6.7.9'
warning: the compiler differs from the one used to build the kernel
The kernel was built by: x86_64-linux-gnu-gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0
You are using: gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0
CC [M] $HOME/kthread_examples/kthread.o
MODPOST $HOME/kthread_examples/Module.symvers
CC [M] $HOME/kthread_examples/kthread.mod.o
LD [M] $HOME/kthread_examples/kthread.ko
BTF [M] $HOME/kthread_examples/kthread.ko
Skipping BTF generation for $HOME/kthread_examples/kthread.ko due to unavailability of vmlinux
make[1]: Leaving directory '/usr/src/linux-headers-6.7.9'
Check if the .ko is generated or not using ls command.
test@test-V520-15IKL:~$ ls -l
total 360
-rw-rw-r-- 1 test test 713 Mar 18 16:06 kthread.c
-rw-rw-r-- 1 test test 169784 Mar 18 16:08 kthread.ko
-rw-rw-r-- 1 test test 58 Mar 18 16:08 kthread.mod
-rw-rw-r-- 1 test test 1047 Mar 18 16:08 kthread.mod.c
-rw-rw-r-- 1 test test 96512 Mar 18 16:08 kthread.mod.o
-rw-rw-r-- 1 test test 74696 Mar 18 16:08 kthread.o
-rw-rw-r-- 1 test test 161 Mar 18 16:00 Makefile
-rw-rw-r-- 1 test test 58 Mar 18 16:08 modules.order
-rw-rw-r-- 1 test test 0 Mar 18 16:08 Module.symvers
Run modinfo command to get the information about the kernel module.
test@test-V520-15IKL:~/.../tc_1$ modinfo kthread.ko
filename: $HOME/kthread_examples/kthread.ko
description: Example of wait_event_interruptible_locked_irq
author: Linux Usr
license: GPL
srcversion: 8D2147F67AB01CF0E482DAC
depends:
retpoline: Y
name: kthread
vermagic: 6.7.9 SMP preempt mod_unload modversions
insert the module using insmod command.
test@test-V520-15IKL:~$ sudo insmod ./kthread.ko
check if the module is loaded or not using lsmod command.
test@test-V520-15IKL:~$ sudo lsmod | grep kthread
kthread 16384 0
check if the thread is created or not using ps command.
test@test-V520-15IKL:~$ ps -N | grep wait_event_interruptible_locked_irq
23209 ? 00:00:00 wait_event_interruptible_locked_irq wake_thread example
23210 ? 00:00:00 wait_event_interruptible_locked_irq wait_thread example
check for the kernel messages from init function and thread function once the module is loaded and thread is created.
test@test-V520-15IKL:~$ sudo dmesg
[40011.772449] inside kthread init function
[40011.772477] successfully created wake thread
[40011.772480] wake thread execution i value = 0
[40011.772492] successfully created wait thread
[40012.808287] wake thread execution i value = 1
[40013.832275] wake thread execution i value = 2
[40014.856419] wake thread execution i value = 3
[40014.856531] wait thread execution i value = 4
[40015.880471] wait thread execution i value = 4
[40015.880474] wake thread execution i value = 4
[40016.904524] wake thread execution i value = 5
[40016.904524] wait thread execution i value = 5
[40017.928553] wait thread execution i value = 6
remove the module from kernel using rmmod command.
test@test-V520-15IKL:~$ sudo rmmod kthread
check if the module is still loaded after removing the kernel module using lsmod if it is not displayed in lsmod output it is verified that the module is removed successfully.
test@test-V520-15IKL:~$ sudo lsmod | grep kthread
test@test-V520-15IKL:~$
check if the thread is destroyed using ps command if it is not displayed in ps output we can confirm that the thread is destroyed successfully.
test@test-V520-15IKL:~$ ps -N | grep wait_event_interruptible_locked_irq
test@test-V520-15IKL:~$
Check for kernel messages from exit function using dmesg command.
test@test-V520-15IKL:~$ sudo dmesg
[40027.601355] inside kthread exit function
[40029.193068] destroyed threads and completed tasks
In this example let’s see how to wait for an event using wait_event_interruptible_exclusive_locked_irq
Include the follow header files(.h) to refer the API being used for the execution.
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/wait.h>
#include <linux/delay.h>
Add the following module macros to display information about the license, author and description about the module.
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Linux Usr");
MODULE_DESCRIPTION("Example of wait_event_interruptible_exclusive_locked_irq");
Declare the thread variables which we are going to create and use in this example
static struct task_struct *wait_thread;
static struct task_struct *wake_thread;
static DECLARE_WAIT_QUEUE_HEAD(wqueue);
int i = 0;
Add the module init function which will be executed once we load the kernel module using insmod command.
static int __init wait_event_interruptible_exclusive_locked_irq_init(void)
{
pr_info("Inside kthread init function\n");
thread_start();
return 0;
}
Add the thread start function called from the module init function which is used to create the thread and execute it.
void thread_start(void)
{
pr_info("initialized waitqueue head\n");
wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event_interruptible_exclusive_locked_irq wake_thread example");
if (IS_ERR(wake_thread))
pr_info("error creating thread\n");
else
pr_info("kthread created successfully\n");
wait_thread = kthread_run(wait_thread_fn,NULL,"wait_event_interruptible_exclusive_locked_irq wait_thread example");
if(IS_ERR(wait_thread))
pr_info("error creating thread\n");
else
pr_info("kthread created successfully\n");
}
Add the module exit function which will be executed once we unload the kernel module using rmmod command.
static void __exit wait_event_interruptible_exclusive_locked_irq_exit(void)
{
pr_info("Inside kthread exit function\n");
thread_stop();
}
Add the thread stop function called from the module exit function which is used to destroy the thread and stop its execution.
void thread_stop(void)
{
kthread_stop(wake_thread);
kthread_stop(wait_thread);
pr_info("destroyed threads and completed tasks\n");
}
Add the thread function API which will be called as soon as the kthread is created and is in running state.
int wait_thread_fn(void *data) {
unsigned long flags;
while(!kthread_should_stop()) {
spin_lock_irqsave(&wqueue.lock,flags);
wait_event_interruptible(wqueue,i > 3);
pr_info("wait thread execution i value = %d\n",i);
spin_lock_irqrestore(&wqueue.lock,flags);
msleep(1000);
}
return 0;
}
int wake_thread_fn(void *data)
{
while(!kthread_should_stop()) {
pr_info("wake thread execution i value = %d\n",i);
i++;
wake_up(&wqueue);
msleep(1000);
}
return 0;
}
Add the driver entry points which will be executed once the module is inserted or removed from the kernel.
module_init(wait_event_interruptible_exclusive_locked_irq_init);
module_exit(wait_event_interruptible_exclusive_locked_irq_exit);
1#include <linux/module.h>
2#include <linux/init.h>
3#include <linux/kernel.h>
4#include <linux/kthread.h>
5#include <linux/delay.h>
6#include <linux/sched.h>
7#include <linux/wait.h>
8
9MODULE_LICENSE("GPL");
10MODULE_AUTHOR("linux usr");
11MODULE_DESCRIPTION("Example of wait_event_interruptible_exclusive_locked_irq");
12
13static struct task_struct *wait_thread;
14static struct task_struct *wake_thread;
15DECLARE_WAIT_QUEUE_HEAD(wqueue);
16int i = 0;
17
18/* wait_thread_fn - executes when wait_thread is created,
19 * waits for an event to be completed by wake_thread,
20 * prints a message and sleeps for 1000ms,
21 * stops when kthread_stop is called */
22
23int wait_thread_fn(void *data) {
24 unsigned long flags;
25 while(!kthread_should_stop()) {
26 spin_lock_irqsave(&wqueue.lock,flags);
27 wait_event_interruptible_exclusive_locked_irq(wqueue,i > 3);
28 pr_info("wait thread execution i value = %d\n",i);
29 spin_unlock_irqrestore(&wqueue.lock,flags);
30 msleep(1000);
31 }
32 return 0;
33}
34
35/* wake_thread_fn - executes when wake_thread is created,
36 * prints a message, increments the value and sleeps for 1000ms,
37 * sends wake_up signal to check the condition in wait_thread,
38 * stops when kthread_stop is called */
39
40int wake_thread_fn(void *data)
41{
42 while(!kthread_should_stop()) {
43 pr_info("wake thread execution i value = %d\n",i);
44 i++;
45 wake_up(&wqueue);
46 msleep(1000);
47 }
48 return 0;
49}
50
51/* thread_start - creates thread and starts execution */
52
53void thread_start(void)
54{
55 wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event_interruptible_exclusive_locked_irq wake_thread example");
56 if (IS_ERR(wake_thread))
57 pr_info("error in creating wake thread\n");
58 else
59 pr_info("successfully created wake thread\n");
60 wait_thread = kthread_run(wait_thread_fn,NULL,"wait_event_interruptible_exclusive_locked_irq wait_thread example");
61 if (IS_ERR(wait_thread))
62 pr_info("error in creating wait thread\n");
63 else
64 pr_info("successfully created wait thread\n");
65}
66
67/* wait_event_interruptible_exclusive_locked_irq_init - calls thread_start,
68 * executes when module is loaded */
69
70static int __init wait_event_interruptible_exclusive_locked_irq_init(void)
71{
72 pr_info("inside kthread init function\n");
73 thread_start();
74 return 0;
75}
76
77/* thread_stop - destroys thread and stops execution */
78
79void thread_stop(void)
80{
81 kthread_stop(wake_thread);
82 kthread_stop(wait_thread);
83 pr_info("destroyed threads and completed tasks\n");
84}
85
86/* wait_event_interruptible_exclusive_locked_irq_exit - calls thread_stop,
87 * executes when module is unloaded */
88
89static void __exit wait_event_interruptible_exclusive_locked_irq_exit(void)
90{
91 pr_info("inside kthread exit function\n");
92 thread_stop();
93}
94
95module_init(wait_event_interruptible_exclusive_locked_irq_init);
96module_exit(wait_event_interruptible_exclusive_locked_irq_exit);
1obj-m += kthread.o
2
3all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5
6clean:
7 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Run make to compile the kernel source and generate the .ko image.
make -C /lib/modules/6.7.9/build M=$HOME/kthread_examples/
make[1]: Entering directory '/usr/src/linux-headers-6.7.9'
warning: the compiler differs from the one used to build the kernel
The kernel was built by: x86_64-linux-gnu-gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0
You are using: gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0
CC [M] $HOME/kthread_examples/kthread.o
MODPOST $HOME/kthread_examples/Module.symvers
CC [M] $HOME/kthread_examples/kthread.mod.o
LD [M] $HOME/kthread_examples/kthread.ko
BTF [M] $HOME/kthread_examples/kthread.ko
Skipping BTF generation for $HOME/kthread_examples/kthread.ko due to unavailability of vmlinux
make[1]: Leaving directory '/usr/src/linux-headers-6.7.9'
Check if the .ko is generated or not using ls command.
test@test-V520-15IKL:~$ ls -l
total 360
-rw-rw-r-- 1 test test 713 Mar 18 16:06 kthread.c
-rw-rw-r-- 1 test test 169784 Mar 18 16:08 kthread.ko
-rw-rw-r-- 1 test test 58 Mar 18 16:08 kthread.mod
-rw-rw-r-- 1 test test 1047 Mar 18 16:08 kthread.mod.c
-rw-rw-r-- 1 test test 96512 Mar 18 16:08 kthread.mod.o
-rw-rw-r-- 1 test test 74696 Mar 18 16:08 kthread.o
-rw-rw-r-- 1 test test 161 Mar 18 16:00 Makefile
-rw-rw-r-- 1 test test 58 Mar 18 16:08 modules.order
-rw-rw-r-- 1 test test 0 Mar 18 16:08 Module.symvers
Run modinfo command to get the information about the kernel module.
test@test-V520-15IKL:~/.../tc_1$ modinfo kthread.ko
filename: $HOME/kthread_examples/kthread.ko
description: Example of wait_event_interruptible_exclusive_locked_irq
author: Linux Usr
license: GPL
srcversion: 8D2147F67AB01CF0E482DAC
depends:
retpoline: Y
name: kthread
vermagic: 6.7.9 SMP preempt mod_unload modversions
insert the module using insmod command.
test@test-V520-15IKL:~$ sudo insmod ./kthread.ko
check if the module is loaded or not using lsmod command.
test@test-V520-15IKL:~$ sudo lsmod | grep kthread
kthread 16384 0
check if the thread is created or not using ps command.
test@test-V520-15IKL:~$ ps -N | grep wait_event_interruptible_exclusive_locked_irq
24178 ? 00:00:00 wait_event_interruptible_exclusive_locked_irq wake_thread example
24179 ? 00:00:00 wait_event_interruptible_exclusive_locked_irq wait_thread example
check for the kernel messages from init function and thread function once the module is loaded and thread is created.
test@test-V520-15IKL:~$ sudo dmesg
[42047.260115] inside kthread init function
[42047.260201] successfully created wake thread
[42047.260207] wake thread execution i value = 0
[42047.260259] successfully created wait thread
[42048.282972] wake thread execution i value = 1
[42049.306983] wake thread execution i value = 2
[42050.331051] wake thread execution i value = 3
[42050.331074] wait thread execution i value = 4
[42051.355088] wait thread execution i value = 4
[42051.355088] wake thread execution i value = 4
[42052.379120] wait thread execution i value = 5
remove the module from kernel using rmmod command.
test@test-V520-15IKL:~$ sudo rmmod kthread
check if the module is still loaded after removing the kernel module using lsmod if it is not displayed in lsmod output it is verified that the module is removed successfully.
test@test-V520-15IKL:~$ sudo lsmod | grep kthread
test@test-V520-15IKL:~$
check if the thread is destroyed using ps command if it is not displayed in ps output we can confirm that the thread is destroyed successfully.
test@test-V520-15IKL:~$ ps -N | grep wait_event_interruptible_exclusive_locked_irq
test@test-V520-15IKL:~$
Check for kernel messages from exit function using dmesg command.
test@test-V520-15IKL:~$ sudo dmesg
[42058.828661] inside kthread exit function
[42060.571530] destroyed threads and completed tasks
kthread API |
Learning |
---|---|
kthread_run |
Create and wake a thread |
kthread_should_stop |
To determine when thread should exit |
kthread_stop |
Stop a thread created by kthread_create |
Waitqueue API |
Learning |
---|---|
DECLARE_WAIT_QUEUE_HEAD |
Declare waitqueue head |
wait_event_interruptible |
sleeps until the condition is true |
wait_event_interruptible_timeout |
sleeps until the condition is true or timer gets expired |
wake_up |
wake up the waiting waitqueues under wait_event APIs |
API |
Learning |
---|---|
MODULE_LICENSE |
Used to denote the license used in the kernel module |
MODULE_AUTHOR |
Used to mention the author of the kernel module |
MODULE_DESCRIPTION |
Used to describe what the module does |
IS_ERR |
Detects an error pointer |
wake_up_process |
wake up a specific process |
msleep |
will put in sleep for a certain amount of msecs time. |
module_init |
Driver initialization entry point |
module_exit |
Driver exit entry point |
pr_info |
Print an info-level message |