Linux中的线程和LWP

这句话是否正确:“ Linux中的所有线程都是LWP,但并非所有LWP都是线程”.实际上,我尝试了解Linux中的线程实现. pthread_create调用clone syscall但是在man clone中我没有找到任何对LWP的引用.
那么,Linux有LWP吗?

解决方法

从这篇博客中你可以找到你的答案 http://www.thegeekstuff.com/2013/11/linux-process-and-threads/

Threads in Linux are nothing but a flow of execution of the process. 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. 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.
The reason dates back to those times when Linux supported threads at
user level only. 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. 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 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.

So,effectively we can say that threads and light weight processes are
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. Internally,the
clone() function is used to create a normal as well as alight weight
process. 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. So,the main difference is generated by using
different flags that can be passed to clone() function.

Read more about fork() and clone() on their respective man pages.

相关文章

/etc/sysctl.conf这个目录主要是配置一些系统信息,/etc/sys...
1.作用 useradd或adduser命令用来建立用户帐号和创建用户的起...
它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅...
不管是我们在安装软件还是监测软件的使用性能,我们都要随时...
装好Tomcat7后,发现除了本机能访问外界访问不了,岂有此理。...
修改防火墙配置需要修改 /etc/sysconfig/iptables 这个文件,...