Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OS Scheduler Simulator

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.


Features

  • Context Switching Uses ucontext.h to 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)

Project Structure

.
├── 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

Prerequisites

  • GCC Compiler

  • Linux / Unix environment

    • Required for ucontext.h and sys/time.h
    • Works on native Linux or WSL (Windows Subsystem for Linux)

Compilation

Compile the project using the following command:

gcc main.c scheduler.c task.c -o scheduler

Note: If you encounter errors related to sigaction or ucontext, ensure you are compiling on a Linux-compatible environment.


Usage

Run the simulator using:

./scheduler <num_tasks> <quantum_ms> <algo_id>

Arguments

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

Scheduling Algorithms

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

Examples

1. Round Robin with 5 tasks and 50ms time slice

./scheduler 5 50 2

2. Shortest Job First with 10 tasks

Quantum is ignored for SJF but must be provided as a placeholder.

./scheduler 10 10 1

3. Multilevel Feedback Queue with 8 tasks

./scheduler 8 20 4

Sample Output

--- 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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages