內核線程的使用

首先介紹幾個創建線程相關的函數:

 

 struct task_struct *kthread_create(int (*threadfn)(void *data),
                                    void *data,
                                    const char namefmt[],
                                    ...)

 

創建一個內核線程,但是並不會開始執行,返回task_struct結構體。

創建的線程可以通過:

int wake_up_process(struct task_struct *p)

函數來喚醒。一個線程即一個task。

int kthread_should_stop(void)
通過該函數查看線程是否要停下,
int kthread_stop(struct task_struct *k)
通過此函數來停止一個線程。
一個典型的線程函數的實例如下:
thread_func(){
    wait_queue_head_t wait_queue;
    DECLARE_WAITQUEUE(wait, current);

    add_wait_queue(&p_thread->wait_queue, &wait);

    __set_current_state(TASK_RUNNING);
    do {
    __set_current_state(TASK_INTERRUPPTABLE);
    schedule();
  __set_current_state(TASK_RUNNING);
	/*todo:......*/
}while(!kthread_should_stop());
    remove_wait_queue(&wait_queue, &wait);

}
其中的DECLARE_WAITQUEUE與進程調度相關,通過該函數可使線程進入wait狀態,以等待被喚醒。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章