Process and thread: what is the difference?

Video 069 of 140 in Virtualization Foundations, in the unit “Processes, threads and scheduling”. 4:24, with English and Hindi audio.

A busy warehouse system seems to do many things at once. A database, for example, answers many clients at the same time. Inside Linux, that work is carried by processes and threads. They look alike, but they differ in what they own and what they share. This article explains each one, the trade-offs between them, and how to see the threads of a process on a Linux machine.

What you will learn

What a process and a thread are

A process is a running program. The operating system gives it its own memory, its own open files, and a number called a PID, short for process ID. Linux uses that number to name and manage the process.

A thread is one stream of work inside a process. It runs one step after another, like one worker who finishes a list. Threads inside the same process share its memory, so they can all read and change the same data.

Two ways to run many jobs side by side

Real databases make different choices. PostgreSQL starts a separate process for each connection. MySQL uses threads for its connections. Both work well, with different trade-offs.

What happens when a program starts

The kernel first creates a process, with its own memory and its own PID. The program then starts its threads. Each thread gets its own thread ID, but all of them belong to the same process.

Last, the kernel's scheduler gives CPU time to each thread. In Linux, the scheduler works with threads, so threads are what actually run.

Look for threads on a Linux machine

Linux keeps each process's details in a special folder named proc. The status file of a process has a Threads line that gives its thread count. Here the file is read for the cat program itself.

Count the threads of the cat program
cat /proc/self/status | grep Threads

The cat program is single-threaded, so the line reports one thread. The ps command can also list the threads of running processes, one row for each thread.

List the first threads of running processes
ps -eLf | head -n 4

Note: In that list, the PID column is the process, and the LWP column is the thread ID. Rows with the same PID belong to one process.

Where sharing goes wrong

Sharing memory is powerful and risky at the same time. Separate processes are safer from each other, but each one needs its own memory.

Count the threads of a process

You need a Linux terminal. You will read the thread count of a single-threaded program, then list the first threads on the machine with their process and thread IDs.

  1. Read the Threads line of the cat program's status file.

    cat /proc/self/status | grep Threads

    What you should see: The Threads line shows one, because cat is a single-threaded program.

  2. List the first threads of running processes.

    ps -eLf | head -n 4

    What you should see: The PID column is the process and the LWP column is the thread ID. Rows with one PID share a process.

Terms

Process
A running program, with its own memory, open files and a process ID.
Thread
One stream of work inside a process. Threads in the same process share its memory.
PID
The process ID, a number that Linux gives each process so it can be named and managed.

Check yourself

What do threads inside one process share?

They share that process's memory, so they can all read and change the same data.

Which column in ps -eLf holds the thread ID?

The LWP column holds the thread ID. Rows with the same PID belong to one process.

Why might a database start a separate process for each connection?

Each process has its own memory, so a fault stays inside that one process. PostgreSQL works this way.

Previous: Where a boot stops when it fails. Next: Process states: running, waiting, stopped or zombie?.