A Operating System scheduler simulator written in C. This project simulates core OS concepts including process context switching (using ucontext), multiple CPU scheduling algorithms, and resource management with mutexes, including deadlock avoidance logic.
-
Context Switching Uses
ucontext.hto save and restore task states, closely simulating actual CPU context switches. -
Resource Management Simulates mutex locks. Tasks can block when a resource is unavailable and are awakened when it is released.
-
Performance Metrics Automatically calculates and reports:
- Turnaround Time
- Waiting Time
- Response Time
-
Five Scheduling Algorithms Implemented
- FCFS (First-Come, First-Served)
- SJF (Shortest Job First)
- RR (Round Robin)
- Priority Scheduling (Preemptive)
- MLFQ (Multilevel Feedback Queue)
.
├── main.c # Entry point: parses arguments and initializes simulation
├── scheduler.c # Scheduler core, timer interrupts, algorithm logic
├── task.c # Task behavior, workload simulation, mutex/resource logic
├── scheduler.h # Shared structs, enums, and function prototypes
-
GCC Compiler
-
Linux / Unix environment
- Required for
ucontext.handsys/time.h - Works on native Linux or WSL (Windows Subsystem for Linux)
- Required for
Compile the project using the following command:
gcc main.c scheduler.c task.c -o schedulerNote: If you encounter errors related to
sigactionorucontext, ensure you are compiling on a Linux-compatible environment.
Run the simulator using:
./scheduler <num_tasks> <quantum_ms> <algo_id>| Argument | Description |
|---|---|
<num_tasks> |
Number of tasks to create (Max: 10) |
<quantum_ms> |
Time slice in milliseconds (used by RR and MLFQ) |
<algo_id> |
Scheduling algorithm ID |
| ID | Algorithm | Description | Type |
|---|---|---|---|
| 0 | FCFS | First-Come, First-Served | Non-Preemptive |
| 1 | SJF | Shortest Job First | Non-Preemptive |
| 2 | RR | Round Robin | Preemptive |
| 3 | Priority | Priority Scheduling | Preemptive |
| 4 | MLFQ | Multilevel Feedback Queue | Preemptive |
./scheduler 5 50 2Quantum is ignored for SJF but must be provided as a placeholder.
./scheduler 10 10 1./scheduler 8 20 4--- Simulating 3 Tasks with Algo 2 ---
Created Task 0: Work=450ms, Prio=2, UsesRes=0
Created Task 1: Work=320ms, Prio=5, UsesRes=-1
Created Task 2: Work=280ms, Prio=1, UsesRes=0
Task 0 STARTED (Work: 450ms)
[Task 0] Acquired Res 0
...
--- All Tasks Completed ---
ID | Prio | Workload | Arrive | Start | Finish | Wait | Turnaround
---|------|----------|--------|-------|--------|------|-----------
0 | 2 | 450 | 10 | 10 | 560 | 110 | 550
1 | 5 | 320 | 10 | 60 | 480 | 160 | 470
2 | 1 | 280 | 10 | 110 | 600 | 320 | 590
Average Turnaround: 536.67 ms