- What are Linux Processes, Threads, Light Weight Processes, and Process State
- Linux Processes
- Linux Threads vs Light Weight Processes
- Linux Process States
- How threads are created in Linux?
- How are threads created?
- How many threads can be created in Linux?
- Which system call is used to create a thread in Linux?
- How do threads work?
- What are the types of threads?
- What are threads What is the concept of threads?
- Can two threads run at the same time?
- Are threads expensive?
- How many threads can you create?
- How do I count threads in Linux?
- How many maximum threads can you create?
- What is the function of thread?
- Why multiprocessing comes as multithreading was already there?
- How do I run a thread program?
What are Linux Processes, Threads, Light Weight Processes, and Process State
Linux has evolved a lot since its inception. It has become the most widely used operating system when in comes to servers and mission critical work. Though its not easy to understand Linux as a whole but there are aspects which are fundamental to Linux and worth understanding. In this article, we will discuss about Linux processes, threads and light weight processes and understand the difference between them. Towards the end, we will also discuss various states for Linux processes.
Linux Processes
In a very basic form, Linux process can be visualized as running instance of a program. For example, just open a text editor on your Linux box and a text editor process will be born. Here is an example when I opened gedit on my machine :
$ gedit & [1] 5560 $ ps -aef | grep gedit 1000 5560 2684 9 17:34 pts/0 00:00:00 gedit
First command (gedit &) opens gedit window while second ps command (ps -aef | grep gedit) checks if there is an associated process. In the result you can see that there is a process associated with gedit. Processes are fundamental to Linux as each and every work done by the OS is done in terms of and by the processes. Just think of anything and you will see that it is a process. This is because any work that is intended to be done requires system resources ( that are provided by kernel) and it is a process that is viewed by kernel as an entity to which it can provide system resources. Processes have priority based on which kernel context switches them. A process can be pre-empted if a process with higher priority is ready to be executed.
For example, if a process is waiting for a system resource like some text from text file kept on disk then kernel can schedule a higher priority process and get back to the waiting process when data is available. This keeps the ball rolling for an operating system as a whole and gives user a feeling that tasks are being run in parallel. Processes can talk to other processes using Inter process communication methods and can share data using techniques like shared memory. In Linux, fork() is used to create new processes. These new processes are called as child processes and each child process initially shares all the segments like text, stack, heap etc until child tries to make any change to stack or heap. In case of any change, a separate copy of stack and heap segments are prepared for child so that changes remain child specific. The text segment is read-only so both parent and child share the same text segment. C fork function article explains more about fork().
Linux Threads vs Light Weight Processes
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. How to Create Threads in Linux explains more about threads.
Linux Process States
- RUNNING – This state specifies that the process is either in execution or waiting to get executed.
- INTERRUPTIBLE – This state specifies that the process is waiting to get interrupted as it is in sleep mode and waiting for some action to happen that can wake this process up. The action can be a hardware interrupt, signal etc.
- UN-INTERRUPTIBLE – It is just like the INTERRUPTIBLE state, the only difference being that a process in this state cannot be waken up by delivering a signal.
- STOPPED – This state specifies that the process has been stopped. This may happen if a signal like SIGSTOP, SIGTTIN etc is delivered to the process.
- TRACED – This state specifies that the process is being debugged. Whenever the process is stopped by debugger (to help user debug the code) the process enters this state.
- ZOMBIE – This state specifies that the process is terminated but still hanging around in kernel process table because the parent of this process has still not fetched the termination status of this process. Parent uses wait() family of functions to fetch the termination status.
- DEAD – This state specifies that the process is terminated and entry is removed from process table. This state is achieved when the parent successfully fetches the termination status as explained in ZOMBIE state.
How threads are created in Linux?
Linux implements all threads as standard processes. The Linux kernel does not provide any special scheduling semantics or data structures to represent threads. Instead, a thread is merely a process that shares certain resources with other processes.
How are threads created?
The creation of new Threads requires Objects that implement the Runnable Interface, which means they contain a method “public void run( )” . … Creating a Thread Object does not start the thread running – To do that the program must call the Thread’s “start( )” method.
How many threads can be created in Linux?
Linux doesn’t have a separate threads per process limit, but has a limit on the total number of processes on the system (as threads just processes with a shared address space on Linux). This thread limit for linux can be modified at runtime by writing desired limit to /proc/sys/kernel/threads-max.
Which system call is used to create a thread in Linux?
The underlying system call to create threads is clone(2) (it is Linux specific).
How do threads work?
A thread is the unit of execution within a process. … Each thread in the process shares that memory and resources. In single-threaded processes, the process contains one thread. The process and the thread are one and the same, and there is only one thing happening.
What are the types of threads?
Six Most Common Types of Threads
- UN/UNF.
- NPT/NPTF.
- BSPP (BSP, parallel)
- BSPT (BSP, tapered)
- metric parallel.
- metric tapered.
What are threads What is the concept of threads?
A thread is a flow of execution through the process code, with its own program counter that keeps track of which instruction to execute next, system registers which hold its current working variables, and a stack which contains the execution history. … Each thread represents a separate flow of control.
Can two threads run at the same time?
Within a process or program, we can run multiple threads concurrently to improve the performance. Threads, unlike heavyweight process, are lightweight and run inside a single process – they share the same address space, the resources allocated and the environment of that process.
Are threads expensive?
Creating a thread is expensive, and the stack requires memory. … More commmonly (IMO), OS level threads are expensive because they are not used correctly by the engineers – either there are too many and there is a ton of context switching, there is competition for the same set of resources, the tasks are too small.
How many threads can you create?
You have 4 CPU sockets, each CPU can have, up to, 12 cores and each core can have two threads. Your max thread count is, 4 CPU x 12 cores x 2 threads per core, so 12 x 4 x 2 is 96. Therefore the max thread count is 96 and max core count is 48.
How do I count threads in Linux?
Each thread in a process creates a directory under /proc//task . Count the number of directories, and you have the number of threads. ps -eLf on the shell shall give you a list of all the threads and processes currently running on the system. Or, you can run top command then hit ‘H’ to toggle thread listings.
How many maximum threads can you create?
For the 32-bit JVM, the stack size appears to limit the number of threads you can create. This may be due to the limited address space.
…
Creating threads gets slower.
Bitness | Stack Size | Max threads |
---|---|---|
64-bit | 128K | 32,072 |
64-bit | 512K | 32,072 |
What is the function of thread?
The implementation of threads and processes differs between operating systems, but in most cases a thread is a component of a process. Multiple threads can exist within one process, executing concurrently and sharing resources such as memory, while different processes do not share these resources.
Why multiprocessing comes as multithreading was already there?
Multiprocessing allocates separate memory and resources for each process or program. Multithreading threads belonging to the same process share the same memory and resources as that of the process. Multithreading avoids pickling. Multiprocessing relies on pickling objects in memory to send to other processes.
How do I run a thread program?
To compile C program with pthread. h library, you have to put -lpthread just after the compile command gcc thread. c -o thread, this command will tell to the compiler to execute program with pthread. h library.