8.4 Threads
In Linux, some processes are divided into pieces called threads. A thread is very similar to a process—it has an identifier (thread ID, or TID), and the kernel schedules and runs threads just like processes. However, unlike separate processes, which usually don’t share system resources such as memory and I/O connections with other processes, all threads inside a single process share their system resources and some memory.
8.4.1 Single-Threaded and Multithreaded Processes
Many processes have only one thread. A process with one thread is single-threaded, and a process with more than one thread is multithreaded. All p. 205 processes start out single-threaded. This starting thread is usually called the main thread. The main thread may start new threads, making the process multithreaded, similar to the way a process can call fork() to start a new process.
It’s rare to refer to threads at all when a process is single-threaded. This book doesn’t mention threads unless multithreaded processes make a difference in what you see or experience.
The primary advantage of a multithreaded process is that when the process has a lot to do, threads can run simultaneously on multiple processors, potentially speeding up computation. Although you can also achieve simultaneous computation with multiple processes, threads start faster than processes, and it’s often easier or more efficient for threads to intercommunicate using their shared memory than it is for processes to communicate over a channel, such as a network connection or a pipe.
Some programs use threads to overcome problems managing multiple I/O resources. Traditionally, a process would sometimes use fork() to start a new subprocess in order to deal with a new input or output stream. Threads offer a similar mechanism without the overhead of starting a new process.
8.4.2 Viewing Threads
By default, the output from the ps and top commands shows only processes. To display the thread information in ps, add the m option. Listing 8-1 shows some sample output.
$ ps m
PID TTY STAT TIME COMMAND
3587 pts/3 - 0:00 bash❶
- - Ss 0:00 -
3592 pts/4 - 0:00 bash❷
- - Ss 0:00 -
12534 tty7 - 668:30 /usr/lib/xorg/Xorg -core :0❸
- - Ssl+ 659:55 -
- - Ssl+ 0:00 -
- - Ssl+ 0:00 -
- - Ssl+ 8:35 -
Listing 8-1: Viewing threads with ps m
This listing shows processes along with threads. Each line with a number in the PID column (at ❶, ❷, and ❸) represents a process, as in the normal ps output. The lines with dashes in the PID column represent the threads associated with the process. In this output, the processes at ❶ and ❷ have only one thread each, but process 12534 at ❸ is multithreaded, with four threads.
If you want to view the TIDs with ps, you can use a custom output format. Listing 8-2 shows only the PIDs, TIDs, and command:
$ ps m -o pid,tid,command
PID TID COMMAND
3587 - bash
- 3587 -
3592 - bash
- 3592 -
12534 - /usr/lib/xorg/Xorg -core :0
- 12534 -
- 13227 -
- 14443 -
- 14448 -
Listing 8-2: Showing PIDs and TIDs with ps m
The sample output in this listing corresponds to the threads shown in Listing 8-1. Notice that the TIDs of the single-threaded processes are identical to the PIDs; this is the main thread. For the multithreaded process 12534, thread 12534 is also the main thread.
Normally, you won’t interact with individual threads as you would processes. You need to know a lot about how a multithreaded program was written in order to act on one thread at a time, and even then, doing so might not be a good idea.
Threads can confuse things when it comes to resource monitoring because individual threads in a multithreaded process can consume resources simultaneously. For example, top doesn’t show threads by default; you’ll need to press H to turn it on. For most of the resource monitoring tools that you’re about to see, you’ll have to do a little extra work to turn on the thread display.