Linux: A Basic Guide to Processes and Threads

Processes form the base on which programs are run. They hold data relating to which files are open, what signals are pending, the processors state and address space, together with a list of what threads are executing.

Whilst the process describes the environment in which a program is executed, the specifics of that programs execution is detailed in a thread, which holds information relating to program position and stack.

A process begins with a call to fork() which creates a new process by duplicating the old one. The new process begins by executing code at the line after the call to fork(). To create a new address space and begin the execution of a new program a call to exec() is needed.

A program executes until it reaches the exit() call which terminates the process and frees its resources. A parent process can request the state of a child by calling the wait() routine, which allows it to wait for the termination of the child. If the child ends before the parent makes the wait() call then the child enters the zombie state and remains there until the parent makes the call to wait().

Threads

Threads provide a means of executing multiple code paths within a shared address space with shared open files and other resources.

Linux treats threads as processes, but sets their process descriptors up in such a way that memory, files and resources can be shared between multiple instances. The Call to create a thread for example results in a call to clone() similar to that shown below

clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0)

meaning that parent and child share address space, file system information, open files and signal handlers.

A standard call to fork on the other had results in the more compact

clone(SIGCHLD, 0)