A Python implementation of a priority-based CPU scheduler using a time-slice model and process blocking intervals.
- Simulates a CPU scheduling algorithm with priority-based process handling.
- Supports process preemption, blocking, and termination.
- Outputs the simulation timeline, including process states.
- Calculates average turnaround time (TAT) for all processes.
- Handles process arrivals dynamically based on arrival times.
- Configurable
time_sliceandblock_durationvalues. - Debug mode (
DEBUGflag) for detailed simulation logs.
The scheduler manages three key queues:
-
Arrival Queue
- Stores processes waiting to enter the ready queue based on their arrival times.
-
Ready Queue
- Holds processes ready for execution, sorted by priority.
- Processes are preempted at next quantum if a higher-priority process arrives.
-
Blocked Queue
- Contains processes temporarily halted due to reaching their block intervals.
- These processes are moved back to the ready queue after their block duration ends.
The program requires an input file containing process definitions, and it accepts additional parameters for the time slice and block duration:
Each line in the file represents a process with the following space-separated fields:
Name: a sequence of non-blank characters representing the name of the process.Priority: the priority level for the process, from 1 to 9. A higher number indicates a process should take precedence over lower-numbered priority processes.Arrival Time: the time at which the process arrives in the system.Total Time: the total amount of CPU time that will be used by the process.Block Interval: the interval at which the process will block for I/O. When a process blocks, it is unavailable to run for the time specified by the command line argumentblock_duration.
# ProcessName Priority ArrivalTime TotalTime BlockInterval
A 1 0 10 3
B 2 2 5 2
C 3 4 7 4
python3 scheduler.py <input_file> <time_slice> <block_duration>
python3 scheduler.py joblist2.txt 2 1
input_file: Path to the input file containing process details.time_slice: the decimal integer length of the time slice for the Round-Robin scheduler.block_duration: the decimal integer time length that a process is unavailable to run after it blocks.
The scheduler produces a timeline of process events in the following format:
- Start Time: When the event begins.
- Process Name: Identifier for the process or "(IDLE)" if the CPU is idle.
- Duration: Length of the event in time units.
- Status:
T: Process terminated (completed all required CPU time).B: Process blocked (reached block interval).P: Process preempted by the time slice.I: CPU idle.
timeSlice: 2 blockDuration: 1
0 A 2 P
2 B 2 P
4 C 2 P
6 (IDLE) 1 I
...
Average Turnaround Time: 8.333