Linux用户空间线程,内核线程,轻量级进程 [英] Linux user space threads, kernel threads , lightweight processes
问题描述
我对所有这些实体以及它们在Linux中如何相互连接感到困惑. 《 Unix内部原理》一书指出lightweight process (LWP)
是内核支持的用户线程,并且内核看不到进程内部的线程.对于Linux是否仍然正确?
I am bit confused with all this enteties and how they interconnected in Linux. "Unix internals" book states that lightweight process (LWP)
is kernel-supported user thread, and that kernel doesn't see threads inside processes. Is it stil true for Linux?
据我了解,用户空间线程在进程内部调度,通过更高级别的抽象为pthread
库,而无需内核干预.我说的对吗?
As I understand, user-space threads scheduled inside process, by higher level abstraction as pthread
library, without kernel's intervention. Am I right?
推荐答案
在Linux上的pthreads中,线程调度由内核处理.
In pthreads on Linux, the thread scheduling is handled by the kernel.
在Linux中,使用clone()
系统调用创建线程或进程(fork()
是克隆的特例).根据传递给克隆的选项的不同,新创建的进程将更轻或更重(即具有单独的存储空间和单独的文件描述符集等). pthread_create()
使用clone()
且间隔最小.
In Linux, a thread or process is created with the clone()
system call (fork()
is a special case of clone). Depending on the options passed to clone, the newly created process will be lighter weight or a heavier weight (i.e. having a separate memory space and a separate set of file descriptors etc.). pthread_create()
uses clone()
with a minimum amount of separation.
也有可能根本不使用本机线程,而是使用setjmp()
和longjmp()
使用完全用户空间的线程实现.例如,这可以用于实现某种解释语言.但是,我不知道实际实现其自己的用户空间调度程序的程序或库的具体示例.
It is also possible to not make use of native threads at all, and instead use a fully userspace implementation of threading, using setjmp()
and longjmp()
. This could be used to implement some interpreted language, for example. However, I am not aware of a concrete example of a program or library that actually does implement its own userspace scheduler.
另一件事:"内核线程"通常用于指定在内核空间中运行的线程,即它是内核本身的一部分.在ps
中,这样的线程是可识别的,因为它们被方括号括起来,例如[kthreadd]
.
One more thing: a "kernel thread" is generally used to designate a thread that runs in kernel space, i.e. that is part of the kernel itself. In ps
such threads are recognisable because they are surrounded with square brackets, like [kthreadd]
.
这篇关于Linux用户空间线程,内核线程,轻量级进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!