内核线程的使用

来源:互联网 发布:东京交通知乎 编辑:程序博客网 时间:2024/04/29 13:31

首先介绍几个创建线程相关的函数:

 

 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状态,以等待被唤醒。

原创粉丝点击