linux 内核线程启动,内核线程(kthread)的简单使用

内核线程的简单使用

内核线程是工作在内核空间的,不属于任何一个进程,可以发生睡眠。内核线程的相关代码在:include/linux/kthread.h , kernel/kthread.c

1. 创建并启动一个内核线程

struct task_struct *kthread_create(int (*threadfn)(void *data),

void *data,

const char namefmt[], ...);

/**

* kthread_run - create and wake a thread.

* @threadfn: the function to run until signal_pending(current).

* @data: data ptr for @threadfn.

* @namefmt: printf-style name for the thread.

*

* Description: Convenient wrapper for kthread_create() followed by

* wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).

*/

#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;                                                     \

})

其中kthread_create()只是创建一个内核线程,但并没有启动,需要调用wake_up_process()来启动线程,所以内核又帮我们定义了一个宏kthread_run来帮我们搞定。内核线程创建成功后,会返回一个struct task_struct对象指针,方便我们的后续操作。

2. 关闭一个内核线程:

int kthread_stop(struct task_struct *k);

这个调用是会阻塞等待,直到内核线程k退出为止。原因为因为此函数内部会调用wait_for_completion()的方法(通过等待队列来实现),阻塞等待内核线程自身的退出。

3.内核线程函数,如何判断自身需要退出:

int kthread_should_stop(void);

如果该内核线程已经被设置stop标志了,则会返回1,否则返回0。

4. 一个简单的例子:

在模块初始化的时候创建一个内核线程,此内核线程的功能是每隔2秒中打印一条信息。当我们卸载模块的时候,会关闭该内核线程。

#include 

#include 

#include 

MODULE_LICENSE("GPL");

static int demo_thr(void *data)

{

while (!kthread_should_stop()) {

msleep_interruptible(2000);

}

return 0;

}

static struct task_struct *thr = NULL;

static __init int kthread_demo_init(void)

{

thr = kthread_run(demo_thr, NULL, "kthread-demo");

if (!thr) {

ERR("kthread_run fail");

return -ECHILD;

}

return 0;

}

static __exit void kthread_demo_exit(void)

{

if (thr) {

kthread_stop(thr);

thr = NULL;

}

}

module_init(kthread_demo_init);

module_exit(kthread_demo_exit);

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux内核中,可以通过内核线程(kernel thread)来实现多线程内核线程是独立运行在内核空间的标准进程,与普通进程相比,内核线程没有独立的地址空间,mm指针被设置为NULL,只在内核空间运行,不切换到用户空间去。内核线程可以被调度和抢占。 在Linux内核中,可以使用kthread_run()函数来创建内核线程。这个函数接受一个执行函数和一个参数作为参数,可以在执行函数中完成一些后台任务。创建的内核线程可以通过kthread_stop()函数来停止。 在早期的Linux 2.6版本中,可以使用kernel_thread()函数来创建内核线程。但在较新的版本中已不推荐使用该方式,因为在4.1版本中不再使用export。使用kernel_thread()创建的非内核线程需要在其执行函数中调用daemonize()函数来释放资源。 除了以上两种方式,还可以使用kthread_create()函数来创建内核线程。这个函数与kthread_run()类似,用法也相似。 总之,在Linux内核中可以通过内核线程来实现多线程的功能,这些内核线程可以在后台执行一些任务,具有调度和抢占的特性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Linux内核线程](https://blog.csdn.net/Frank_sample/article/details/116455771)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值