加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Linux内核v2.6中的pthread与kthread

发布时间:2020-12-14 01:02:04 所属栏目:Linux 来源:网络整理
导读:这是一个概念性问题. 根据这个post,pthread实际上是使用clone()系统调用实现的.因此,我们可以推断在用户空间中有一个内核线程(或轻量级进程)备份pthread.内核知道pthread并且可以像进程一样安排它. 至于kthread,根据Robert Love,kthreads也是用clone()系统调
这是一个概念性问题.

根据这个post,pthread实际上是使用clone()系统调用实现的.因此,我们可以推断在用户空间中有一个内核线程(或轻量级进程)备份pthread.内核知道pthread并且可以像进程一样安排它.

至于kthread,根据Robert Love,kthreads也是用clone()系统调用创建的:

clone(CLONE_VM| CLONE_FS | CLONE_FILES | CLONE_SIGHAND,0)

所以pthread和kthread都使用clone()调用.我的第一个问题是:

>这两种线程之间有区别吗?

为了回答我自己的问题,我继续读到:

The significant difference between kernel threads and normal processes
is that kernel threads do not have an address space (in fact,their mm
pointer is NULL).

这有区别吗?我的意思是,由pthread_create()创建的线程与正常进程共享地址空间.相反,kthread没有自己的地址空间.那是对的吗?

还有什么不同?

解决方法

In contrast,a kthread does not have its own address space. Is that correct?

a thread created by pthread_create() shares the address space with the normal process.

kernel: how to find all threads from a process’s task_struct

pthreads:pthread_create()用于用户空间,其中应用程序中的多个线程共享相同的进程地址空间.为此,您需要将程序与pthread库链接以使用此功能. pthreads在应用程序级别或用户空间中提供多线程.在内部,这会转换为clone()系统调用,它将新的struct task_struct映射到每个应用程序线程.

kthreads:内核线程的一些示例用于刷新磁盘缓存,服务softirqs,刷新脏缓冲区等.这些线程仅在内核空间内运行,无法访问用户空间虚拟内存,并且它们仅在PAGE_OFFSET之后使用内核空间内存地址因此,任务描述符中的current-> mm字段始终为NULL.在内部,这个kernel_thread()api转换为内核中的do_fork().内核线程是异步创建的,无论是init进程出现还是加载了某些内核模块(从文件系统到文件系统).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读