Linux Implementation of Threads
Linux Implementation of Threads
1. Linux View of Processes and Threads
Unlike many operating systems, Linux does not make a strong distinction between processes and threads.
Key idea:
In Linux, threads are just processes that share resources.
To the Linux kernel:
-
A thread is simply a process that shares certain resources with other processes
-
The kernel uses a single abstraction called a task
This design is elegant and powerful, and it avoids maintaining two separate execution models.
2. The task_struct: Core Process/Thread Descriptor
Every process or thread in Linux is represented by a task_struct.
This structure contains:
-
Process/thread state
-
Scheduling information
-
Memory management details
-
Open files
-
Signal handlers
-
Parent/child relationships
📌 Important:
There is no separate thread structure in Linux.
Both processes and threads use the same task_struct.
3. How Linux Differentiates Threads from Processes
Linux differentiates between:
-
Processes → have their own resources
-
Threads → share resources
This is controlled using flags passed during process creation.
4. Process Creation: fork(), clone(), and Threads
Traditional UNIX: fork()
-
Creates a new process
-
Child gets a separate address space
-
Initially uses copy-on-write (COW)
Linux Generalization: clone()
Linux introduces the clone() system call, which is the foundation of thread creation.
The flags determine what is shared between parent and child.
Common Clone Flags
| Flag | Shared Resource |
|---|---|
CLONE_VM | Address space |
CLONE_FS | File system info |
CLONE_FILES | Open file descriptors |
CLONE_SIGHAND | Signal handlers |
CLONE_THREAD | Same thread group |
👉 What Is a Linux Thread?
A thread in Linux is created using:
This means:
-
Same memory
-
Same open files
-
Same signal handlers
-
Same thread group
➡️ Result: Multiple execution contexts sharing resources
➡️ Kernel treats them as tasks
5. Thread Groups and tgid
Linux introduces the concept of a thread group:
-
Each thread has:
-
PID (unique)
-
TGID (thread group ID)
-
Relationship:
-
tgid= PID of the group leader -
The first thread created is the leader
-
All threads in the same process share the same
tgid
📌 From user space:
-
Tools like
psshow threads as part of the same process -
Kernel schedules each thread independently
6. Scheduling Threads in Linux
Linux schedules threads, not processes.
-
Each thread:
-
Has its own scheduling entity
-
Can run on a different CPU core
-
-
Scheduler sees threads as independent tasks
This enables:
-
True parallelism on SMP systems
-
Efficient CPU utilization
7. Context Switching Between Threads
When switching between threads:
-
Register state is saved/restored
-
No address space switch if threads share memory (
CLONE_VM) -
Faster than switching between separate processes
➡️ Thread context switches are cheaper than process context switches
8. Signals and Threads
Linux signal handling follows POSIX rules:
-
Signals may be:
-
Directed to a specific thread
-
Delivered to any thread in the thread group
-
-
Signal handlers are usually shared
📌 The kernel carefully decides which thread receives a signal.
9. Kernel Threads vs User Threads
Kernel Threads
-
Created by the kernel
-
Run entirely in kernel space
-
No user address space
Examples:
-
kthreadd -
kswapd -
ksoftirqd
These are also represented by task_struct.
User Threads
-
Created via
clone() -
Have user address space
-
Scheduled like normal tasks
10. POSIX Threads (pthreads) and Linux
From user space:
-
Applications use pthreads
-
Provided by glibc
Internally:
-
pthread_create()→ callsclone() -
Uses one-to-one threading model
➡️ Each pthread maps to one kernel task
11. Why Linux’s Thread Design Is Powerful
✔ Simple kernel design
✔ No duplicated abstractions
✔ Unified scheduling
✔ High scalability
✔ Efficient SMP support
Linux avoids:
-
Special thread schedulers
-
Separate thread control blocks
12. Comparison with Traditional UNIX
| Aspect | Classic UNIX | Linux |
|---|---|---|
| Thread abstraction | Separate | Unified |
| Kernel structures | Process + Thread | Only task |
| Scheduling | Process-based | Thread-based |
| Thread creation | Heavy | Lightweight |
| SMP scalability | Limited | Excellent |
13. Summary
In Linux, a thread is simply a process that shares resources with other processes.
Key Takeaways:
-
Linux uses
clone()for both processes and threads -
No distinction between thread and process at kernel level
-
Threads share resources using clone flags
-
Scheduling is done per thread
-
POSIX threads map directly to kernel tasks
Comments
Post a Comment