Linux内核线程

来源:互联网 发布:bodog信誉第一js 编辑:程序博客网 时间:2024/04/29 23:23

内核线程和普通的进程间的区别在于内核线程没有独立的地址空间,它只在 内核空间运行,从来不切换到用户空间去;并且和普通进程一样,可以被调度,也可以被抢占。

一 线程的创建

struct task_struct *kthread_create(int (*threadfn)(void *data), void *data, const char namefmt[], ...);  线程创建后,不会马上运行,而是需要将kthread_create() 返回的task_struct指针传给wake_up_process()才能驱动线程。也可以使用kthread_run创建线程并启动线程  struct task_struct *kthread_run(int (*threadfn)(void *data),void *data,const char *namefmt, ...);  #define kthread_run(threadfn, data, namefmt, ...)              \  ({                                     \   struct task_struct *__k= kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \  if (!IS_ERR(__k))                          \  wake_up_process(__k);                      \  __k;                                   \  })  
        可见kthread_run是在调用了kthread_create后执行了wake_up_process.
        在非内核线程中调用kernel_thread,必须在调用daemonize(...)来释放资源,成为真正的内核线程,kthread_create实际调用kernel_thread但是内部已经做了处理,不需要自己调用daemonize。

二 线程的退出

      kthread_stop:设置线程的退出标记(线程函数内应用int kthread_should_stop(void)函数,当返回真时应退出函数),kthread_stop会一直等待至线程结束,线程结束前会发送完成结束给kthread_stop,如果直接使用do_exit直接退出线程那么kthread_stop不会收到完成信号将一直等待下去。如果线程已经退出那么kthread_stop会先设置退出标记再唤醒一下thread,唤醒线程后会判断退出标记因此设定的处理函数不会被调用。如果线程已经被唤醒并已经退出那么kthread_stop会一直等待。

int kthread_stop(struct task_struct *thread);  如果处理函数没用kthread_should_stop判断退出,那么 kthread_stop会一直等待处理函数主动退出。  

三 内核线程例程

触摸屏驱动中,内核线程的一个例程。

开启线程:

i2c.thread = kthread_create(ilitek_i2c_polling_thread, NULL, "ilitek_i2c_thread");if(i2c.thread == (struct task_struct*)ERR_PTR){i2c.thread = NULL;printk(ILITEK_ERROR_LEVEL "%s, kthread create, error\n", __func__);}else{wake_up_process(i2c.thread);}

线程函数:

static int i2c_polling_thread(void *arg){int ret=0;// mainloopwhile(1){// check whether we should exit or notif(kthread_should_stop()){break;}// this delay will influence the CPU usage and response latencymsleep(10);// when i2c is in suspend or shutdown mode, we do nothingif(i2c.stop_polling){msleep(1000);continue;}// read i2c dataif(ilitek_i2c_process_and_report() < 0){msleep(3000);printk(ILITEK_ERROR_LEVEL "%s, process error\n", __func__);}}printk(ILITEK_DEBUG_LEVEL "%s, exit\n", __func__);return ret;}

kthread_should_stop()会去判断是不是有某人调用了kthread_stop去终止你的内核线程,如果是就会退出无限循环, 然后内核会通知kthread_stop,表明线程退出了,kthread_stop函数等到线程退出后才会返回。