A small Python simulator for Priority CPU Scheduling supporting both:
- Non-preemptive priority scheduling: once a process starts, it runs until it finishes.
- Preemptive priority scheduling: a newly-arrived higher-priority process can interrupt the currently running process.
Priority rule: lower number = higher priority (e.g., priority 1 runs before 2).
Tie-breaker: FCFS (first-come, first-served) for equal priority.
Priority-Scheduling/
src/
__init__.py
process.py
simulator.py
src/process.pydefines theProcessandTimelineSegmentdata models.src/simulator.pycontains thesimulate(...)function and simulation config/result types.
- Python 3.10+
Download the project from this github reposiotory.
Open the project, and then open a terminal in the root folder.
navigate to
cd srcexecute:
python -m src.simulatorNote: using
-mmatters becausesrc/simulator.pyuses relative imports (e.g.,from .process import Process).
Each process has:
pid: process id (string)arrival_time: when the process becomes ready (int time units)burst_time: CPU time needed (int time units)priority: smaller value means higher priority
Example:
Process(pid="P2", arrival_time=1, burst_time=4, priority=1)The simulator returns a SimulationResult:
TimelineSegment(start, end, label)labelis usually a PID like"P1"or"IDLE"(or"CS"if context switching cost is enabled)
Example segment meaning:
TimelineSegment(start=1, end=5, label='P2')means P2 ran from time 1 up to (not including) time 5.