A comprehensive, feature-rich simulation of time-sharing operating system concepts implemented in C using POSIX user contexts (ucontext). This project demonstrates various CPU scheduling algorithms, resource management, and process state management.
- [Features]
- [Project Structure]
- [Installation]
- [Usage]
- [Scheduling Algorithms]
- [Task Types]
- [System Architecture]
- [Statistics and Metrics]
- [Examples]
- [Technical Details]
- [Contributing]
- 5 Scheduling Algorithms: Round Robin, Priority, Multilevel Feedback Queue (MLFQ), Lottery, and Completely Fair Scheduler (CFS)
- Multiple Task Types: CPU-bound, I/O-bound, Mixed, and Interactive workloads
- Resource Management: Mutex-style locks with wait queues and deadlock potential
- Preemptive Multitasking: Timer-based context switching with configurable quantum
- Comprehensive Statistics: Per-task and system-wide performance metrics
- Context switching with POSIX
ucontextAPI - Priority-based scheduling with dynamic priority adjustment
- Virtual runtime tracking (CFS implementation)
- Multilevel feedback queue with promotion/demotion
- Lottery scheduling with ticket-based fairness
- Resource contention simulation
- Voluntary yielding and sleeping
- I/O operation simulation with blocking
timeshare/
βββ scheduler.h # Header file with all declarations
βββ scheduler.c # Scheduler implementation and algorithms
βββ task.c # Task management and workload functions
βββ main.c # Main program entry point
βββ Makefile # Build configuration
βββ README.md # This file
- scheduler.h: Contains all type definitions, structs, enums, and function prototypes
- scheduler.c: Implements scheduling algorithms, timer management, context switching, and resource management
- task.c: Handles task creation, workload execution, and task lifecycle operations
- main.c: Parses arguments, initializes the system, and starts the simulation
- GCC compiler (with C11 support)
- Linux/Unix system (uses POSIX APIs)
- Make utility
# Clone or download the project
cd timeshare
# Build the project
make
# Clean build artifacts
make clean
# Rebuild from scratch
make clean && make./timeshare <num_tasks> <quantum_ms> <algorithm> <verbose>| Parameter | Description | Valid Range |
|---|---|---|
num_tasks |
Number of tasks to create | 1-64 |
quantum_ms |
Time quantum in milliseconds | β₯10 |
algorithm |
Scheduling algorithm to use | 0-4 (see below) |
verbose |
Enable verbose output | 0 or 1 |
0- Round Robin (RR)1- Priority Scheduling2- Multilevel Feedback Queue (MLFQ)3- Lottery Scheduling4- Completely Fair Scheduler (CFS)
./timeshare 8 50 2 0This runs 8 tasks with 50ms quantum using MLFQ without verbose output.
- Description: Classic time-sharing algorithm
- Behavior: Each task gets equal time quantum in circular order
- Best For: Fair distribution of CPU time
- Quantum Impact: High
- Description: Tasks scheduled by priority level (0-9)
- Behavior: Higher priority tasks always run first
- Best For: Systems with critical/background task separation
- Risk: Starvation of low-priority tasks
- Description: Adaptive scheduler with 3 priority queues
- Behavior:
- Tasks start in highest queue
- Demoted on preemption (CPU-intensive)
- Promoted on voluntary yield (I/O-intensive)
- Best For: Mixed workloads (interactive + batch)
- Advantage: Automatically identifies and prioritizes I/O-bound tasks
- Description: Probabilistic fair-share scheduling
- Behavior: Tasks receive tickets; winner selected randomly
- Tickets: Base 10 + (priority Γ 5)
- Best For: Proportional CPU sharing
- Advantage: Stochastic fairness
- Description: Linux-inspired virtual runtime scheduler
- Behavior: Tracks virtual runtime; schedules task with smallest vruntime
- Best For: General-purpose fair scheduling
- Advantage: Excellent fairness with low overhead
The simulator creates a diverse mix of tasks with different behaviors:
- Heavy computation (square root calculations)
- Minimal I/O operations
- Long time slices before yielding
- Frequent I/O operations (simulated)
- Short CPU bursts between I/O
- Voluntary blocking and sleeping
- Balanced CPU and I/O work
- Resource acquisition/release
- Periodic yielding
- Short CPU bursts
- Frequent voluntary yields
- Simulated user interaction delays
- Higher initial priority
Tasks transition through multiple states:
READY β RUNNING β [BLOCKED/SLEEPING/FINISHED]
β β
ββββββββββ
- READY: Waiting in run queue
- RUNNING: Currently executing
- BLOCKED: Waiting for resource
- SLEEPING: Voluntary sleep
- FINISHED: Completed execution
- Timer interrupt (SIGALRM) triggers scheduler
- Save current task context
- Select next task via scheduling algorithm
- Update statistics (CPU time, vruntime, etc.)
- Restore next task context
- 3 system resources (configurable)
- Mutex-style locking
- FIFO wait queues
- Automatic release on task exit
- Deadlock potential (for demonstration)
The simulator tracks comprehensive statistics:
- Total context switches
- Number of preemptions
- Voluntary yields
- Average turnaround time
- Average CPU time
- Completed tasks
- Task ID and type
- Priority level
- Final state
- Total CPU time (ms)
- Turnaround time (ms)
- Number of preemptions
- Number of yields
- Number of blocks
- Queue level (MLFQ) or Virtual runtime (CFS)
================================================================================
SIMULATION STATISTICS
================================================================================
Scheduling Algorithm: Multilevel Feedback Queue
Quantum: 50 ms
Total Tasks: 8
Completed Tasks: 8
Context Switches: 156
Preemptions: 142
Voluntary Yields: 89
Avg Turnaround Time: 2847.32 ms
Avg CPU Time: 1523.45 ms
Per-Task Statistics:
--------------------------------------------------------------------------------
ID Type Pri State CPU(ms) Turn(ms) Preempt Yield Block Queue/VRT
--------------------------------------------------------------------------------
0 CPU 3 DONE 1847.23 2956.78 45 12 0 Q2
1 I/O 7 DONE 456.12 2134.56 15 28 8 Q0
2 MIX 5 DONE 1234.56 2789.34 38 24 3 Q1
...
# Round Robin - fair but may not optimize for I/O
./timeshare 12 50 0 0
# MLFQ - adapts to workload patterns
./timeshare 12 50 2 0
# CFS - Linux-style fairness
./timeshare 12 50 4 0# Small quantum (high overhead, better interactivity)
./timeshare 10 10 0 0
# Large quantum (lower overhead, worse interactivity)
./timeshare 10 200 0 0# Watch detailed task execution
./timeshare 6 30 2 1# Maximum tasks
./timeshare 64 25 2 0# See how priority affects execution
./timeshare 15 50 1 0- POSIX ucontext API: User-level context switching
- POSIX Signals: Timer interrupts (SIGALRM)
- setitimer(): Periodic timer for quantum enforcement
- makecontext()/swapcontext(): Context creation and switching
- State Pattern: Task state management
- Strategy Pattern: Pluggable scheduling algorithms
- Observer Pattern: Statistics collection
- Each task has 128KB stack (configurable)
- Dynamic allocation with proper cleanup
- No memory leaks (verified with valgrind)
- Async-signal-safe operations in handler
- Context saving during preemption
- Re-entrant safe statistics updates
- Supports up to 64 concurrent tasks
- O(n) scheduling for most algorithms
- O(1) resource lookups
- Bounded history buffer (1000 entries)
#define STACK_SIZE (1024 * 128) // Per-task stack
#define MAX_TASKS 64 // Maximum tasks
#define MAX_RESOURCES 10 // System resources
#define HISTORY_SIZE 1000 // Event history bufferContributions are welcome! Areas for contribution:
- New scheduling algorithms
- Additional task types
- Performance optimizations
- Bug fixes
- Documentation improvements
- Test cases