#include
#include
#include
static struct task_struct *_task;
static int __monitor(void *p)
{
while (!kthread_should_stop()) {
//...
set_current_state(TASK_INTERRUPTIBLE);
schedule();
// or
// schedule_timeout(HZ);
}
return 0;
}
1.创建线程的结构体(_task);
_task = kthread_create(__monitor, NULL, "_monitor");
if(IS_ERR(_task)) {
printk("Unable to start kernel thread[_monitor]./n");
err = PTR_ERR(_task);
_task = NULL;
}
kthread_create的第一个参数是线程体;
第二个参数是线程体的参数;
第三个参数是线程的名称,可以在shell中用top命令查看我们在此建立的内核线程&