linux 内核线程

内核线程类似于用户进程,通常用于并发处理些工作,它是一种在内核空间实现后台任务的方式,并且可以参与时间片轮转调度。

内核线程可以进行繁忙的异步事件处理,也可以睡眠等待某事件的发生,内核线程可以访问内核函数和数据结构。

内核线程可以使用ps命令查看,名称带[]为内核线程。

内核线程的实现流程:

定义一个线程指针

编写线程函数

创建线程

开启线程

停止线程(需接受)

内核线程使用相关函数

#include <linux/kthread.h> //内核线程接口函数头文件

1.定义线程指针

struct task_struct *kernel_thread;

2.创建内核线程,返回值为创建线程的指针(方法一)

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

void *data,const char namefmt[], ...);

参数1:线程函数指针,线程开启后将运行此函数

参数2:函数参数data,传递给线程函数

参数3:线程名称,这个函数可以像printk一样传入某种格式的线程名

3.内核线程绑定CPU

/**

* kthread_bind - bind a just-created kthread to a cpu.

* @k: thread created by kthread_create().

* @cpu: cpu (might not be online, must be possible) for @k to run on.

*

* Description: This function is equivalent to set_cpus_allowed(),

* except that @cpu doesn't need to be online, and the thread must be

* stopped (i.e., just returned from kthread_create()).

*/

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

4.内核线程创建后不会马上运行,需要通过以下函数启动

int wake_up_process(struct task_struct *p);

创建内核线程函数并运行(方法二)

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

})

5.判断停止线程检测函数 (线程函数内使用)

int kthread_should_stop(void)/*接收到停止信号,返回真.*/

6.停止内核线程函数 (线程函数外使用)

int kthread_stop(struct task_struct *k);

1)该函数发送信号给内核线程,如果线程函数不检测信号也不返回,那么此函数它将一直进行等待

2)在调用kthread_stop函数时,线程函数不能已经运行结束。

7.线程函数的运行

线程函数,内核线程开启后会运行该函数

int threadfunc(void *data);

1)该函数由用户自己实现,函数格式如上所示

2)该函数必须能让出CPU,以便其他线程能够得到执行,也必须能重新得到调度。

kernel_thread() kthread_create()/kthread_run()创建内核线程的区别与使用

kernel_thread() 函数创建内核线程

pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)

{

return do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn,

(unsigned long)arg, NULL, NULL);

}

代码分析中,kernel_thread()是通过do_fork()进行创建的线程,在2.6的版本中,这种创建内核线程的方式还可以用于驱动模块中,但是在4.x的内核版本中就不可以在驱动模块中使用,也就是说想要使用kernel_thread之内将驱动程序编译进内核才能够创建线程,想要使用insmod方式创建内核线程,就会被拒绝insmod,因为kernel_thread没有EXPORT_SYSMBOL出来。

kthread_create()/kthread_run() 函数创建线程

将一个函数传递给kernel_thread创建并初始化一个task,该函数接下来负责帮助内核调用daemonize (3.5已经移除了daemonize接口)已转换为内核守护进程,daemonize随后完成一些列操作, 如该函数释放其父进程的所有资源,不然这些资源会一直锁定直到线程结束。阻塞信号的接收, 将kthreadd_task用作守护进程的父进程。

kthread_create需要使用wake_up_process()唤醒,kernel_thread是通过_do_fork来实现的。

两种方式创建线程的方式其实是一种线程,因为,kthrad_run()是一个宏定义,最终调用到kthread_create()

#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()其实也是宏定义

#define kthread_create(threadfn, data, namefmt, arg...) \

kthread_create_on_node(threadfn, data, -1, namefmt, ##arg)

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

void *data,

int node,

const char namefmt[], ...);

最终是调用到kthread_create_on_node()函数, kthread_create_on_node()已经被EXPORT_SYSMBOL出来,所以,无论是在内核中使用还是在驱动模块中,都可以创建出新线程。

注意:kthreatd_create()创建出的新线程的父进程是kthreadd,在kthread_create创建出的线程也对其上下文环境也进行了清理,所以kthread_create()是比较正统的创建线程的方法。推荐使用kthread_create()的方式创建线程。

相关推荐
bug道一7 分钟前
路由 下一跳 网关 两个不同网段的ip如何通过路由器互通
网络
1892280486111 分钟前
NW622NW623美光固态闪存NW624NW635
大数据·网络·数据库·人工智能·microsoft·性能优化
轻松Ai享生活18 分钟前
一文搞懂应用程序 core dump 和内核 core dump
linux
云飞云共享云桌面21 分钟前
1台电脑10个画图设计用怎么实现
linux·运维·服务器·网络·数据库·自动化·电脑
小MarkK24 分钟前
[Ubuntu][mount]ubuntu电脑挂载新硬盘
linux·ubuntu·电脑·mount·硬盘挂载
心 一31 分钟前
Web安全基石:深入理解与防御越权问题
网络·安全·web安全
艾莉丝努力练剑35 分钟前
【Linux】初始Linux:从计算机历史发展、操作系统历史脉络的角度详谈Linux相关的话题,附Linux安装和用户创建(环境准备)详解
linux·运维·服务器·经验分享
l1t43 分钟前
how to build tbox xml into the demo
xml·linux·c语言·parser·tbox
努力学习的小廉1 小时前
深入了解linux系统—— 线程池
linux·运维·服务器
不是编程家1 小时前
Linux第十五讲:Socket编程UDP
linux·运维·udp