Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (6.1k points)

Is this true that "All threads in Linux are LWP but not all LWP are threads"? Well, I was learning thread realization in Linux. Pthread_create call clone syscall, but I did not find any reference about LWP.

So, can anyone tell me does Linux contains LWP or not?

1 Answer

0 votes
by (11.7k points)

Threads are defined as a flow of execution of the process. When a process has multiple execution flows, it is known as a multi-threaded process. 

But when there is a non-multithreaded process, only the main execution flows and therefore it is known as a single-threaded process. But, there is no concept of threads when it comes to the Linux kernel. Every thread is a separate process in the kernel but these processes are somewhat different from the normal one. 

We often get confused with the terms when the thread is called a lightweight process. This reasons comes because earlier, linux supported threads at user level only. Which says that even a multi-threaded process was also a single process for the Linux kernel.

This caused numerous challenges to the library that managed these user-level threads because it had to take care of cases that a thread execution could not hinder if any thread issued a blocking call. 

But after amendments done on it, processes were attached to each thread so that kernel could see them. But as the kernel of Linux does not consider them as threads, each thread is a process to kernel. These processes are known as LWP.

Lightweight process (LWP) differs from a  normal process in a way that LWPs share the same address space and other resources like open files etc. As few resources are shared so these processes are stated as lightweight if we compared to other normal processes and therefore, termed as the name lightweight processes.

Therefore, it can be said that threads and lightweight processes are the same. It’s just that thread is a word used at the user level while the lightweight process is a term used at the kernel level.

When it comes to implementation, they create threads using functions exposed by POSIX compliant pthread library in Linux. Internally, the clone() function is used to create a normal as well as a lightweight process. This implies that fork() is used to create normal processes that further calls clone() with appropriate arguments while to create a thread or LWP, a function from pthread library calls clone() with relevant flags.

You can get more insight on fork() and clone() by their documentation or manual. You can read more about threads and processes from here.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...