Systems Programming

The Linux Thread Implementation

Linux supports POSIX threads, with all the attendent library functions and system calls. However, the Linux apporach to the implementation of threads is unique.

In the Linux kernel, there is no such thing as a thread. Threads are the same as processes, it's just that they share some resources (most importantly, a virtual address space). Linux processes are fairly lightweight anyway, so this works out well.

fork() and pthread_create() are implemented via clone() and sys_clone() calls. Cloning a process involves specifying which resources are to be shared via a set of flags that are ORed together. For example, CLONE_VM means the new process shares virtual memory with the creating process, CLONE_FS means the new process shares file system information with its parent, and CLONE_FILES mean the child and parent share open files.

This means that there is no need for a user-level threads package at all, and no need for a separate scheduler. Threads are scheduled by the kernel just like regular processes, because they are regular processes.

The Linux kernel itself is multi-threaded. All kernel code runs using physical addresses in Linux, so all kernel processes share their address space (technically, they don't have an address space, and the corresponding field in their task_struct is set to NULL. Thus the kernel_thread() call invokes a version of clone() just like fork() and pthread_create().


Author: Mark A. Sheldon
Modified: 25 March 2007