简体   繁体   English

Linux 中的线程和 LWP

[英]threads and LWP in Linux

Is this sentence correct: "All threads in Linux are LWP but not all LWP are threads".这句话是否正确:“Linux 中的所有线程都是 LWP,但并非所有 LWP 都是线程”。 Actually, I try to understand thread realisation in Linux .实际上,我试图了解Linux 中的线程实现。 pthread_create call clone syscall, but in man clone, I didn't find any reference to LWP. pthread_create 调用 clone syscall,但是在 man clone 中,我没有找到任何对 LWP 的引用。

So, does Linux have LWP at all?那么,Linux 有 LWP 吗?

From this blog you can find your answer http://www.thegeekstuff.com/2013/11/linux-process-and-threads/从这个博客你可以找到你的答案http://www.thegeekstuff.com/2013/11/linux-process-and-threads/

Threads in Linux are nothing but a flow of execution of the process. Linux 中的线程只不过是进程的执行流。 A process containing multiple execution flows is known as multi-threaded process.包含多个执行流的进程称为多线程进程。

For a non multi-threaded process there is only execution flow that is the main execution flow and hence it is also known as single threaded process.对于非多线程进程,只有执行流是主要执行流,因此也称为单线程进程。 For Linux kernel , there is no concept of thread.对于 Linux 内核,没有线程的概念。 Each thread is viewed by kernel as a separate process but these processes are somewhat different from other normal processes.每个线程被内核视为一个单独的进程,但这些进程与其他正常进程有些不同。 I will explain the difference in following paragraphs.我将在以下段落中解释差异。

Threads are often mixed with the term Light Weight Processes or LWPs.线程通常与术语轻量级进程或 LWP 混合在一起。 The reason dates back to those times when Linux supported threads at user level only.原因可以追溯到 Linux 仅在用户级别支持线程的时代。 This means that even a multi-threaded application was viewed by kernel as a single process only.这意味着即使是多线程应用程序也仅被内核视为单个进程。 This posed big challenges for the library that managed these user level threads because it had to take care of cases that a thread execution did not hinder if any other thread issued a blocking call.这给管理这些用户级线程的库带来了巨大挑战,因为它必须处理线程执行不会妨碍任何其他线程发出阻塞调用的情况。

Later on the implementation changed and processes were attached to each thread so that kernel can take care of them.后来实现发生了变化,进程被附加到每个线程,以便内核可以处理它们。 But, as discussed earlier, Linux kernel does not see them as threads, each thread is viewed as a process inside kernel.但是,如前所述,Linux 内核并不将它们视为线程,每个线程都被视为内核内部的一个进程。 These processes are known as light weight processes.这些过程被称为轻量级过程。

The main difference between a light weight process (LWP) and a normal process is that LWPs share the same address space and other resources like open files etc. As some resources are shared so these processes are considered to be light weight as compared to other normal processes and hence the name light weight processes.轻量级进程 (LWP) 和普通进程之间的主要区别在于,LWP 共享相同的地址空间和其他资源,例如打开的文件等。由于某些资源是共享的,因此与其他普通进程相比,这些进程被认为是轻量级的进程,因此得名轻量级进程。

So, effectively we can say that threads and light weight processes are the same.因此,实际上我们可以说线程和轻量级进程是相同的。 It's just that thread is a term that is used at user level while light weight process is a term used at kernel level.只是线程是在用户级别使用的术语,而轻量级进程是在内核级别使用的术语。

From implementation point of view, threads are created using functions exposed by POSIX compliant pthread library in Linux.从实现的角度来看,线程是使用 Linux 中符合 POSIX 的 pthread 库公开的函数创建的。 Internally, the clone() function is used to create a normal as well as a light weight process.在内部,clone() 函数用于创建正常和轻量级进程。 This means that to create a normal process fork() is used that further calls clone() with appropriate arguments while to create a thread or LWP, a function from pthread library calls clone() with relevant flags.这意味着要创建一个正常的进程 fork() 使用适当的参数进一步调用 clone() 而创建线程或 LWP,来自 pthread 库的函数调用带有相关标志的 clone() 。 So, the main difference is generated by using different flags that can be passed to clone() function.因此,主要区别是通过使用可以传递给 clone() 函数的不同标志来生成的。

Read more about fork() and clone() on their respective man pages.在各自的手册页上阅读有关 fork() 和 clone() 的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM