首页 > 编程语言 > 详细

kernel-线程thread

时间:2014-12-15 20:24:17      阅读:482      评论:0      收藏:0      [点我收藏+]


kernel线程管理在2.6 一般是:

#include<pthread.h>

pthread_ttid; sigset_tset;
voidmyfunc()
{
 printf("hello\n");
}
intmain(){
创造线程

pthread_create(&tid,NULL,mythread,NULL);

退出线程

pthread_kill(tid,SIGUSR2);

等待线程结束

pthread_join(tid,&status);

}

到了3.0 提出了一个更加简单的线程管理方法:

kthread_create:创建线程。

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, ...);
kthread_stop:通过发送信号给线程,使之退出。
int kthread_stop(struct task_struct *thread);
线程一旦启动起来后,会一直运行,除非该线程主动调用do_exit函数,或者其他的进程调用kthread_stop函数,结束线程的运行。

但如果线程函数正在处理一个非常重要的任务,它不会被中断的。当然如果线程函数永远不返回并且不检查信号,它将永远都不会停止。

引用文件

#include <linux/kthread.h>

创造线程结构

staticstruct task_struct *test_task;

test_task = kthread_create(test_thread, NULL, "test_task");

启动线程

wake_up_process(test_task);

关闭线程

  kthread_stop(test_task);

kernel-线程thread

原文:http://blog.csdn.net/weiwei_xiaoyu/article/details/41946319

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!