没有合适的资源?快使用搜索试试~ 我知道了~
kthread usage 用kthread_create创建线程。
资源推荐
资源详情
资源评论
1 使用kthread_create创建线程:
struct task_struct *kthread_create(int (*threadfn)(void *data),
void *data,
const char *namefmt, ...);
这个函数可以像printk一样传入某种格式的线程名
线程创建后,不会马上运行,而是需要将kthread_create() 返回的
task_struct指针传给wake_up_process(),然后通过此函数运行线程。
2. 当然,还有一个创建并启动线程的函数:kthread_run
struct task_struct *kthread_run(int (*threadfn)(void *data),
void *data,
const char *namefmt, ...);
3. 线程一旦启动起来后,会一直运行,除非该线程主动调用do_exit函
数,或者其他的进程调用kthread_stop函数,结束线程的运行。
int kthread_stop(struct task_struct *thread);
kthread_stop() 通过发送信号给线程。
如果线程函数正在处理一个非常重要的任务,它不会被中断的。当然如
果线程函数永远不返回并且不检查信号,它将永远都不会停止。
参考:Kernel threads made easy
--
view plaincopy to clipboardprint?
#include <linux/kthread.h>
static struct task_struct * _task;
static struct task_struct * _task2;
static struct task_struct * _task3;
static int thread_func(void *data)
{
int j,k;
int timeout;
wait_queue_head_t timeout_wq;
static int i = 0;
i++;
j = 0;
k = i;
printk("thread_func %d started", i);
init_waitqueue_head(&timeout_wq);
while(!kthread_should_stop())
{
interruptible_sleep_on_timeout(&timeout_wq, HZ);
printk("[%d]sleeping..%d", k, j++);
}
return 0;
}
void my_start_thread(void)
{
资源评论
ntjxw
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功