Check the health and availability of your Linux servers for optimal performance with Site24x7's Linux monitoring tool.
When a program is run in Linux, an instance of that program is loaded into memory along with all the resources it needs. This instance of the program in memory is called a process. Linux processes—and more specifically heavyweight processes (HWPs)—include a lot of overhead when they are created or when they are switched to from another process. But a thread is a lightweight process (LWP) in a computer operating system that can share resources such as code, data, file I/O, stack, and signal tables. That's what makes them light. Unlike processes, threads don’t need to flush and recreate all tables before they can be switched. Therefore, they can more easily and frequently be switched.
However, Linux doesn’t distinguish between a process and a thread at the operating system level. Threads are still processes in Linux, only threads can share certain resources with other processes. Unlike other operating systems, Linux doesn’t provide any threads-specific data structures or scheduling options. The kernel makes no distinction here.
But threads or lightweight processes can still be used in Linux to achieve parallelism and multi-core processing by splitting a process into multiple threads. This way, each thread can run on a separate processor core to achieve true parallelism. Because lightweight processes are already so lightweight in Linux, switching between threads doesn’t incur large overheads.
This article will cover the limitations of threads and processes in a Linux system, how to find these limits, how to increase the maximum number of threads, and finally, a few common commands for checking the number of threads per process in a Linux system.
/proc/sys/kernel/.
The configuration for the maximum number of threads that the kernel can execute is stored in proc/sys/kernel/threads-max.
Using the command
on this file will display the maximum number of threads for the system. For example, in figure 1 the maximum number of threads is 3,935. This was run on a single-core server used for hosting a static website, so the maximum number of threads is low compared to multi-core processors:
The sysctl command can also be used with an option and can be filtered using grep to check the same kernel configuration. Figure 2 shows the output of the command below:
sysctl -a | grep threads-max
You can see above that both commands give the same output. Similarly, there's a kernel configuration parameter for the maximum number of processes a kernel can execute simultaneously. This configuration is present in a file at /proc/sys/kernel/pid_max.
We can cat this file to see the number, as shown in figure 3 below:
The output in figure 3 is from the same single-core server, so the number is low. This number is also around the point that the process IDs or PIDs wrap around, so we might periodically encounter duplicate PIDs during maintenance.
Because Linux does not differentiate between processes and threads, the maximum number of processes is also the maximum number of threads. If the system reaches the limit and none of the processes are ready to be killed, the system won’t be able to create any new processes, and by extension, won’t be able to create any new threads.
The number of threads in Linux is limited by a few factors, most notably the virtual memory size. The formula used to calculate the maximum number of threads for a system is:
maximum number of threads = virtual memory size / (stack size * 1024 * 1024)
As the formula shows, increasing the virtual memory size or decreasing the stack size per thread will lead to an increase in the maximum number of threads. To more accurately make this adjustment, the ulimit
command can be used to check the current stack size on a system:
ulimit -a | grep "stack size"
Figure 4 below shows the stack size of a single-core server:
Fig 4: Stack sizeWe can change this stack size using the same ulimit
command:
ulimit -s 8192
Linux provides a few commands to easily list all processes and threads, and to filter threads for a given process. The following section shows how to use these commands to get thread-related information.
The ps
command, along with its options, is used to list all processes running in a Linux system and useful information about them. However, without any options, the command only gives a snapshot of the current processes, as seen in figure 5 below:
Using the option e
lists all process names as well. The screenshot in figure 6 shows a part of this command’s output:
To get more data in the BSD syntax, the aux options can be used as follows:
ps aux
This command will give information such as the percentage of CPU and memory used by a process, the command used to run that process, and for how long the process has been running.
proc
Information related to each process is stored in its own file in the /proc directory. Using the process ID, you can read the status file for the number of threads that particular process has created.
In figure 7 below, the process with PID 42 has created 1 thread. The command to get the thread count of a process is:
cat /proc/<pid>/status
The status file provides a lot more information about a process than just the number of threads. To find the number of threads for a process, there's one more method that uses the same /proc
directory.
Just as every process has a directory created under its PID, every thread has a directory created under its thread ID. This is found in the /proc/<pid>/task
directory. The total number of directories in the task directory is the number of threads created for a process.
This can be achieved by piping the output of the ls
command to the wc
command to count the number of directories as shown below:
ls /proc/<pid>/task | wc -l
Figure 8 shows the output of the command for the same process shown in figure 7:
Fig 8: Number of threads per process using /taskps
The ps
command helps get information about the threads of a process. With the ability to show the information for only a given PID, it’s easier to get the count for an individual process.
The options are as follows:
As before, the output of this command is then piped to the wc
command to count the number of lines and get the thread count. The complete command is as follows:
ps hH p <PID> | wc -l
All three methods consistently provide the same results.
Threads are used to speed up the execution of processes by increasing parallelism since each thread can run on a processor core. Threads can also be used to make sure a process is not blocking other processes or threads while it waits for a file I/O or other operation to complete.
But like processes, there are limits on how many processes or threads can be run simultaneously on a Linux system. This number can be increased by changing the configuration of a few kernel parameters.
Because Linux makes little distinction between threads and processes, all limits that apply to processes apply to threads as well. By using these simple commands mentioned above, Linux admins can check the number of threads for a given process.
Write for Site24x7 is a special writing program that supports writers who create content for Site24x7 “Learn” portal. Get paid for your writing.
Apply Now