Kernel/Tasks/ and provides fair CPU time distribution across multiple threads.
Core Concepts
Process
AProcess (Kernel/Tasks/Process.h) represents a program in execution with its own:
- Address space (
AddressSpace) - Open file descriptors
- Credentials (UID, GID, groups)
- Security context (pledge promises, unveil paths)
- One or more threads
- Process group and session membership
ProcessID (pid) and have a parent process (ppid).
Key Process State
Thread
AThread (Kernel/Tasks/Thread.h) is the schedulable unit of execution. Each thread has:
- Unique thread ID (
ThreadID) - Reference to parent process
- Priority level (1-31)
- CPU affinity mask
- Register state
- Kernel and user stacks
- Thread-local storage (TLS)
Thread States
Only threads in the
Runnable state are eligible for scheduling.The Scheduler
TheScheduler (Kernel/Tasks/Scheduler.h) is responsible for:
- Selecting the next thread to run
- Context switching between threads
- Managing runnable thread queues
- Timer-based preemption
- Idle loop execution
Scheduling Algorithm
SerenityOS uses a priority-based round-robin scheduler with multiple priority queues:THREAD_PRIORITY_MIN (1) to THREAD_PRIORITY_MAX (31):
- Higher values = higher priority
- Default priority: 30 (normal)
- Idle threads: priority 1
Priority Queue Implementation
Priority Queue Implementation
The scheduler maintains a bitmask of non-empty queues for efficient queue selection:When selecting the next thread, the scheduler uses
bit_scan_forward() to find the highest-priority non-empty queue in O(1) time.Time Slicing
Threads are given time slices based on their type:Scheduling Operations
pick_next()
Selects the next thread to run:- Checks for runnable threads in priority order
- Respects CPU affinity masks
- Skips threads already running on other cores
- Returns
NoRunnableThreadFoundif all threads blocked
yield()
Allows a thread to voluntarily give up the CPU:context_switch()
Performs the actual CPU context switch to a new thread:- Saves current thread’s register state
- Switches page directory (address space)
- Loads new thread’s register state
- Updates TLS and stack pointers
- Restores execution
Thread Management
Creating Threads
Threads are created viaThread::create():
create_thread syscall with parameters:
Thread Blocking
Threads block when waiting for resources using theBlockResult mechanism:
- WaitQueue: Waiting for events (e.g., child process exit)
- Mutex: Waiting to acquire a lock
- I/O: Waiting for data from files/sockets
- Futex: User space synchronization primitives
Thread Finalization
When threads exit, they transition throughDying to Dead state. The finalizer thread (g_finalizer) performs cleanup:
- Frees thread kernel stacks
- Releases thread resources
- Notifies joining threads
- Removes thread from process
Process Management
Process Creation
Processes are created via:- fork(): Duplicate current process (copy-on-write)
- exec(): Replace process with new program
- posix_spawn(): Combined fork+exec optimization
Process Groups and Sessions
Processes are organized into: Process Groups (ProcessGroup)
- Collection of related processes
- Share a process group ID (pgid)
- Used for signal delivery to multiple processes
- Collection of process groups
- Associated with controlling terminal
- Managed via
setsid(),getsid()
Process Security
Pledge
Restricts process capabilities via promises:Unveil
Restricts filesystem access to specific paths:Pledge and unveil provide defense-in-depth security by limiting process capabilities after initialization.
CPU Affinity
Threads can be bound to specific CPUs via affinity masks:Performance Tracking
The scheduler tracks CPU time usage:- Time in user mode
- Time in kernel mode
- Context switches
- Page faults
Work Queues
WorkQueue (Kernel/Tasks/WorkQueue.h) provides deferred work execution:
Key Operations
Scheduling a Thread
Setting Thread Priority
Blocking and Unblocking
Related Files
Kernel/Tasks/Scheduler.{h,cpp}- Core scheduler implementationKernel/Tasks/Thread.{h,cpp}- Thread abstractionKernel/Tasks/Process.{h,cpp}- Process managementKernel/Tasks/ProcessGroup.{h,cpp}- Process group managementKernel/Tasks/WaitQueue.{h,cpp}- Thread blocking mechanismKernel/Tasks/WorkQueue.{h,cpp}- Deferred work execution
