kernel_thread.c
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kmod.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#define CLONE_KERNEL (CLONE_FS | CLONE_FILES | CLONE_SIGHAND)
struct task_struct *px_practice_task = NULL;
int px_kernel_thread(void *arg)
{
int n = 0;
while(1)
{
printk("%s: %d\n",__func__,n++);
ssleep(3);
if(kthread_should_stop())
{
break;
}
}
return 0;
}
static int __init px_practiceCall(void)
{
printk("%s:\n",__func__);
px_practice_task = kthread_create(px_kernel_thread,NULL,"px_practice task");
if(!IS_ERR(px_practice_task))
wake_up_process(px_practice_task);
return 0;
}
static void __exit px_practiceCallExit(void)
{
printk("%s:\n",__func__);
kthread_stop(px_practice_task);
}
module_init(px_practiceCall);
module_exit(px_practiceCallExit);
MODULE_AUTHOR("zhao");
MODULE_LICENSE("GPL");
Makefile
MODULE_NAME := kernel_thread
obj-m :=$(MODULE_NAME).o
KERNELDIR = /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean