当前位置 博文首页 > lyndon:Linux 内核线程实例
thread.c
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/sched.h>
static struct task_struct *test_TaskStruct;
int threadTask(void *arg)
{
static int count = 0;
while (1) {
if (kthread_should_stop()) {
printk("threadTask: kthread_should_stop\n");
break;
}
// if(((++count)/8000000)==2)
{
printk("threadTask: count=%d\n", ++count);
// schedule_timeout(80000*HZ);//让出CPU,使其他线程可以运行。
// 或者使用
ssleep(1); // msleep(1000);的底层实现就是schedule_timeout().
}
}
return 0;
}
static int __init init_kernel_Thread(void)
{
test_TaskStruct = kthread_create(threadTask, NULL, "KernelThead");
if (IS_ERR(test_TaskStruct)) {
printk("kthread_create error\n");
} else {
wake_up_process(test_TaskStruct);
}
return 0;
}
static void __exit exit_kernel_Thread(void)
{
kthread_stop(test_TaskStruct);
}
module_init(init_kernel_Thread);
module_exit(exit_kernel_Thread);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("kernel_Thread from linux");
MODULE_DESCRIPTION(" driver for kernel Thread test");
Makefile
obj-m:=thread.o
CURRENT_PATH:=$(shell pwd)
VERSION_NUM:=$(shell uname -r)
LINUX_PATH:=/usr/src/linux-headers-$(VERSION_NUM)
all :
make -C $(LINUX_PATH) M=$(CURRENT_PATH) modules
clean:
make -C $(LINUX_PATH) M=$(CURRENT_PATH) clean
编译
$ ls
Makefile thread.c
$ make
make -C /usr/src/linux-headers-5.8.0-44-generic M=/home/liyongjun/project/c/study/kernel/thread modules
make[1]: 进入目录“/usr/src/linux-headers-5.8.0-44-generic”
CC [M] /home/liyongjun/project/c/study/kernel/thread/thread.o
MODPOST /home/liyongjun/project/c/study/kernel/thread/Module.symvers
CC [M] /home/liyongjun/project/c/study/kernel/thread/thread.mod.o
LD [M] /home/liyongjun/project/c/study/kernel/thread/thread.ko
make[1]: 离开目录“/usr/src/linux-headers-5.8.0-44-generic”
$ ls
Makefile modules.order Module.symvers thread.c thread.ko thread.mod thread.mod.c thread.mod.o thread.o
安装
$ sudo insmod thread.ko
$
$ tail -f /var/log/kern.log
Mar 4 20:52:35 Box20 kernel: [41011.008386] threadTask: count=1
Mar 4 20:52:36 Box20 kernel: [41012.033767] threadTask: count=2
Mar 4 20:52:37 Box20 kernel: [41013.055642] threadTask: count=3
Mar 4 20:52:38 Box20 kernel: [41014.083436] threadTask: count=4
Mar 4 20:52:39 Box20 kernel: [41015.122674] threadTask: count=5
Mar 4 20:52:41 Box20 kernel: [41016.213002] threadTask: count=6
Mar 4 20:52:42 Box20 kernel: [41017.257125] threadTask: count=7
Mar 4 20:52:43 Box20 kernel: [41018.281019] threadTask: count=8
Mar 4 20:52:44 Box20 kernel: [41019.301674] threadTask: count=9
Mar 4 20:52:45 Box20 kernel: [41020.328317] threadTask: count=10
Mar 4 20:52:46 Box20 kernel: [41021.346402] threadTask: count=11
Mar 4 20:52:47 Box20 kernel: [41022.372944] threadTask: count=12
Mar 4 20:52:48 Box20 kernel: [41023.396810] threadTask: count=13
Mar 4 20:52:49 Box20 kernel: [41024.420669] threadTask: count=14
Mar 4 20:52:50 Box20 kernel: [41025.449693] threadTask: count=15
Mar 4 20:52:51 Box20 kernel: [41026.470620] threadTask: count=16
卸载
$ sudo rmmod thread.ko
$
$ tail -f /var/log/kern.log
Mar 4 20:52:52 Box20 kernel: [41027.497992] threadTask: kthread_should_stop