Process Management - FreeWorld OS
Overview
The FreeWorld Process Management system provides complete process and thread management for the kernel. It supports process creation, termination, scheduling, context switching, and all advanced process/thread features.
Process Management Components
1. Process Creation and Termination
Files: kernel/process/fork.asm, kernel/process/exit.asm
- fork() - Create new process by duplicating current process
- clone() - Create process with shared resources (via CLONE_* flags)
- exit() - Terminate process
- exit_group() - Terminate all threads in process group
2. Process Waiting
File: kernel/process/wait.asm
- wait() - Wait for any child process
- waitpid() - Wait for specific child process
- Status code retrieval
3. Process Groups and Sessions
File: kernel/process/process_groups.asm
- setpgid() - Set process group ID
- setsid() - Create new session
- Process group and session management
4. Process Credentials
File: kernel/process/credentials.asm
- getuid() - Get user ID
- setuid() - Set user ID
- getgid() - Get group ID
- setgid() - Set group ID
- Effective UID/GID management
- Capabilities support
5. Process Limits
File: kernel/process/process_limits.asm
- setrlimit() - Set resource limits
- getrlimit() - Get resource limits
- RLIMIT_CPU, RLIMIT_MEM, RLIMIT_FSIZE, etc.
6. Process Namespaces
File: kernel/process/namespaces.asm
- PID namespace
- Mount namespace
- Network namespace
- UTS namespace
- IPC namespace
- User namespace
- Cgroup namespace
7. Process Accounting
File: kernel/process/accounting.asm
- CPU usage tracking
- Memory consumption tracking
- I/O statistics
8. Process Monitoring
File: kernel/process/process_monitoring.asm
- Process health monitoring
- Failure detection
- Recovery actions
9. Process Execution
File: kernel/process/execve_64.asm
- execve() - Execute program (64-bit)
- ELF loading and execution
- Argument and environment setup
10. Thread Management
Files: Multiple thread management files
- Thread Synchronization: Mutexes, condition variables, semaphores
- Thread-Local Storage (TLS): Private data per thread
- Thread Cancellation: Deferred and asynchronous cancellation
- Thread Attributes: Stack size, detach state, scheduling parameters
- Thread Pools: Worker thread pools for task execution
11. Process Manager Core (process.asm)
Location: kernel/process/process.asm
Core process management system:
Features
- Process creation and termination
- Process table management
- Context switching (save/restore CPU state)
- Round-robin scheduler
- Process states (running, ready, blocked, zombie, terminated)
- Process priority support
- Parent-child relationships
Process States
PROCESS_STATE_RUNNING: Currently executingPROCESS_STATE_READY: Ready to run, waiting for CPUPROCESS_STATE_BLOCKED: Waiting for I/O or eventPROCESS_STATE_ZOMBIE: Terminated, waiting for parent to collectPROCESS_STATE_TERMINATED: Fully terminated and cleaned up
Key Functions
| Function | Description | Parameters | Returns |
|---|---|---|---|
init_process_manager |
Initialize process management system | None | None |
create_process |
Create a new process | EAX = entry point, EBX = stack pointer, ECX = name | EAX = process ID, or -1 on error |
terminate_process |
Terminate a process | EAX = process ID, EBX = exit code | None |
get_current_process_id |
Get current process ID | None | EAX = process ID |
get_process |
Get process structure by ID | EAX = process ID | EAX = process structure pointer, or 0 if not found |
switch_to_process |
Switch to a different process (context switch) | EAX = process ID | None (does not return to caller) |
schedule |
Round-robin scheduler - find next ready process | None | None |
Process Structure
Each process has a 256-byte structure containing:
- Process ID (4 bytes)
- State (4 bytes)
- Priority (4 bytes)
- Stack pointer (ESP) (4 bytes)
- Base pointer (EBP) (4 bytes)
- Instruction pointer (EIP) (4 bytes)
- Page directory (CR3) (4 bytes)
- Memory base address (4 bytes)
- Memory size (4 bytes)
- Process name (32 bytes)
- Parent process ID (4 bytes)
- Exit code (4 bytes)
Thread Manager (thread.asm)
Location: kernel/process/thread.asm (~200 lines)
Lightweight thread management within processes:
Features
- Thread creation within processes
- Thread table (16 threads per process)
- Thread termination
- Per-thread stack allocation
- Thread state tracking
Thread States
THREAD_STATE_RUNNING: Currently executingTHREAD_STATE_READY: Ready to runTHREAD_STATE_BLOCKED: Waiting for eventTHREAD_STATE_TERMINATED: Terminated
Key Functions
| Function | Description | Parameters | Returns |
|---|---|---|---|
create_thread |
Create a new thread in a process | EAX = process ID, EBX = entry point, ECX = stack size | EAX = thread ID, or -1 on error |
terminate_thread |
Terminate a thread | EAX = process ID, EBX = thread ID | None |
get_thread |
Get thread structure | EAX = process ID, EBX = thread ID | EAX = thread structure pointer, or 0 if not found |
Integration
Process management is integrated into the kernel via kernel/integration.asm:
; In kernel_subsystems_init: call init_process_manager ; Initialize process manager ; Creates kernel process (PID 0)
Status: ✅ Integrated - Process manager initialized during kernel boot
Usage Examples
Create Process
; Create a new process mov eax, process_entry_point ; Entry point mov ebx, process_stack_top ; Stack pointer mov ecx, process_name ; Process name string call create_process cmp eax, -1 je create_error ; EAX contains process ID
Switch Process
; Switch to a different process mov eax, target_process_id call switch_to_process ; Execution continues in target process
Schedule
; Run scheduler to find next ready process call schedule ; If a ready process is found, switches to it
Create Thread
; Create a thread in current process mov eax, [current_process_id] mov ebx, thread_entry_point mov ecx, 0x10000 ; 64KB stack call create_thread cmp eax, -1 je thread_error ; EAX contains thread ID