demo: 创建一个内核线程,每隔1000毫秒打印一次my_thread running
sample.c
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/delay.h>
static int state;
static struct task_struct *my_tsk;
static int my_thread(void *args)
{
for(;;)
{
if(state == 1){
break;
}
msleep(1000);
printk("my_thread running\n");
}
return 0;
}
static int __init sample_init(void)
{
state = 0;
my_tsk = kthread_create(my_thread, NULL, "%s", "my_threaddd");
if (my_tsk){
wake_up_process(my_tsk);
}
return 0;
}
static void __exit sample_exit(void)
{
state = 1;
kthread_stop(my_tsk);
printk("my_thread stopped\n");
}
module_init(sample_init);
module_exit(sample_exit);
MODULE_AUTHOR("XXX");
MODULE_LICENSE("GPL");
Makefile:
obj-m := sample.o
KERNEL :=/usr/src/kernels/`uname -r`/
al