• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

linux内核编程-----内核线程kthread

武飞扬头像
Pintitus
帮助1

一、序言

      在linux驱动中需要创建多线程,实现同一时刻执行多个任务。这个时候我们就需要内核机制----kthread。

二、内核API

1.创建线程

struct task_struct *ret = kthread_create(threadfn, data, namefmt, arg...);

2.唤醒线程
      将创建好的线程句柄传入该函数即可运行线程。

int wake_up_process(struct task_struct *tsk);

3.一键创建kthread
      其实就是将1,2合并而已。

#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;
})

4.线程绑定到cpu

void kthread_bind(struct task_struct*k, unsigned int cpu)

5.停止某个kthread

int kthread_stop(struct task_struct *k);

6.判断当前kthread是否需要停止

int kthread_should_stop(void)

三、demo程序

static int threadfn(void * __dev)
{
	while(!kthread_should_stop()) {
	}
	return 0;
}
int probe()
{
	struct task_struct *th;
	
	th = kthread_run(threadfn, dev, "threadfn_name");
	if (IS_ERR(th)) {
		dev_err(dev, "unable to start control thread\n");
		goto errout;
	}
 //...
 errout:
 	return 0;
}

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfkikkc
系列文章
更多 icon
同类精品
更多 icon
继续加载