Taskqueue : One Shot
Topics in this section,
# |
Version |
---|---|
Freebsd |
14.1.0 |
In this program, you are going to learn
How to schedule the taskqueue ?
1#include<sys/param.h>
2#include<sys/module.h>
3#include<sys/kernel.h>
4#include<sys/malloc.h>
5#include<sys/queue.h>
6#include<sys/taskqueue.h>
7
8 // Define the task structure
9static struct task my_task;
10
11 // Taskqueue handle
12static struct taskqueue *my_taskqueue;
13
14 // Taskqueue handler
15static void task_handler(void *context,int pending)
16{
17 printf("Taskqueue excuted\n");
18}
19
20static int event_handler(struct module *module, int event, void *arg)
21{
22 int e = 0;
23
24 switch(event)
25 {
26 case MOD_LOAD:
27 printf("Module loaded\n");
28
29 // Initialize the taskqueue (shared taskqueue thread)
30 my_taskqueue = taskqueue_create("my_taskqueue", M_NOWAIT, taskqueue_thread_enqueue, &my_taskqueue);
31
32 // Create taskqueue thread
33 taskqueue_start_threads(&my_taskqueue,1, PI_NET, "my_taskqueue_thread");
34
35 // Initialize the task
36 TASK_INIT(&my_task, 0, task_handler, NULL);
37
38 // Enqueue task to taskqueue
39 taskqueue_enqueue(my_taskqueue, &my_task);
40 break;
41
42 case MOD_UNLOAD:
43 printf("Module unloaded\n");
44
45 //Stop the taskqueue
46 taskqueue_drain(my_taskqueue, &my_task);
47
48 // Free the taskqueue
49 taskqueue_free(my_taskqueue);
50 break;
51
52 default:
53 e = EOPNOTSUPP;
54 break;
55 }
56return(e);
57}
58
59static moduledata_t taskqueue_data = {
60 "taskqueue_module",
61 event_handler,
62 NULL
63};
64
65DECLARE_MODULE(taskqueue_module, taskqueue_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
66
1SRCS=taskqueue.c
2KMOD=taskqueue_module
3
4.include <bsd.kmod.mk>
1$ pwd
2basic_device_drivers/taskqueue_one_shot
3
4
5$ ls
6taskqueue.c Makefile
7
8
9$ make
10
11$ ls
12.depend.taskqueue.o export_syms machine taskqueue.c taskqueue_module.ko
13Makefile i386 opt_global.h taskqueue.o x86
Load the module using kldload
1$ kldload -v ./taskqueue_module.ko
2Loaded ./taskqueue_module.ko, id=8
Run kldstat command to check the loaded modules
1$ kldstat
2Id Refs Address Size Name
3 1 29 0xffffffff80200000 1f6c698 kernel
4 2 1 0xffffffff8216e000 772c70 zfs.ko
5 3 1 0xffffffff828e1000 7850 cryptodev.ko
6 4 1 0xffffffff83218000 3220 intpm.ko
7 5 1 0xffffffff8321c000 2178 smbus.ko
8 6 1 0xffffffff8321f000 3360 uhid.ko
9 7 1 0xffffffff83223000 3360 wmt.ko
10 8 1 0xffffffff83227000 20b8 taskqueue_module.ko
Run dmesg command to check the kernel logs
1$ dmesg
2Module loaded
3Taskqueue excuted: pending : 1
Unload the module using kldunload
1$ kldunload -v ./taskqueue_module.ko
2Unloading taskqueue_module.ko, id=8
Run kldstat command to check the loaded modules
1$ kldstat
2Id Refs Address Size Name
3 1 27 0xffffffff80200000 1f6c698 kernel
4 2 1 0xffffffff8216e000 772c70 zfs.ko
5 3 1 0xffffffff828e1000 7850 cryptodev.ko
6 4 1 0xffffffff83218000 3220 intpm.ko
7 5 1 0xffffffff8321c000 2178 smbus.ko
8 6 1 0xffffffff8321f000 3360 uhid.ko
9 7 1 0xffffffff83223000 3360 wmt.ko
Run dmesg command to check the kernel logs
1$ dmesg
2Module loaded
3Taskqueue excuted: pending : 1
4Module unloaded