Linux创建内核线程kthread_create的用法介绍

内核线程是工作在内核空间的,不属于任何一个进程,可以发生睡眠。可以用内核线程来进行一些循环的动作,比如通过循环拉高拉低gpio设置成方波输出的信号来模拟pwm信号,比如循环控制led的闪灯效果等等都可以使用到内核线程kthread_create接口函数。

内核线程的相关代码目录:

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。

这里列举一个使用内核线程的简单的demo代码,可作为参考:

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

#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/delay.h>
 
 
#define ENTER() printk(KERN_DEBUG "%s() Enter", __func__)
#define EXIT() printk(KERN_DEBUG "%s() Exit", __func__)
#define ERR(fmt, args...) printk(KERN_ERR "%s()-%d: " fmt "\n", __func__, __LINE__, ##args)
#define DBG(fmt, args...) printk(KERN_DEBUG "%s()-%d: " fmt "\n", __func__, __LINE__, ##args)
 
static struct task_struct *test_kthread = NULL;    //定义一个task_struct结构体指针,赋值为NULL
 
static int kthread_test_func(void)   //定义一个内核线程要执行的函数
{
	ENTER();
	while (!kthread_should_stop()) {
		DBG("kthread is running");
		msleep(5000);
	}

	EXIT();
	return 0;
}
 
static __init int kthread_test_init(void)
{
	ENTER();
 
	test_kthread = kthread_run(kthread_test_func, NULL, "kthread-test");  //创建线程kthread-test,并且运行
	if (!test_kthread) {
		ERR("kthread_run fail");
		return -ECHILD;
	}
 
	EXIT();
	return 0;
}
 
static __exit void kthread_test_exit(void)
{
	ENTER();
	if (test_kthread) {
		DBG("kthread_stop");
		kthread_stop(test_kthread); //停止内核线程
		test_kthread = NULL;
	}
   
	EXIT();
}
 
module_init(kthread_test_init);
module_exit(kthread_test_exit);

MODULE_AUTHOR("czd,214241976@qq.com");
MODULE_DESCRIPTION("Device_create Driver");
MODULE_LICENSE("GPL");
  • 9
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

零意@

您的打赏将是我继续创作的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值