A simple CPU scheduler written in Python for COSCA365, Operating Systems. This simulation implements a FCFS scheduling algorithm.
To run simply input the following into your cli:
$ python scheduler.py 10
10 here is your inputed
time_quantum for the program. You can input any
time_quantum you like as long as its an
Dictionary of Variables
Note that because Python does not inherently declare variable types, we will list the type of value the variables will be used according to other languages. Additionally, other variables may be necessary, such as
temp variables, which may not be reflected in the dictionary of variables. These will be made clear inside of the code.
Basic housekeeping variables
sysclock- this variable will take the
inttype and represent the system clock, beginning from 0.
time_quantum- time quantum for the program. Givin by a command line argument to the scheduler, ex.
python scheduler.py 10where the number
10is the program
number_of_processes- total number of processes for the scheduler. Hard-coded into the pogram and is decremented whenever a process is terminated; once
number_of_processes == 0the main loop breaks and the program is halted.
total_processes- set equal to the
number_of_processesin the beginning of the pogram. Since
total_processesis used make runtime calculations after main loop is broken.
switch_counter- used to count number of loops during a
context_switch. Initially it is set to
switch_counter > 5the
num_context- this variable will take the
inttype and it will hold the total number of context switches performed.
cpu_util- this variable will take the
floattype and it will display the percentage of total time the CPU is running.
ArgumentParserobject imported from Python’s
argparsemodule. Allows the use of command line arguments, so the user can input
args- arguments parsed by the
In this program, we will be creating and using a
process- process dictionary that holds a
i = pid; the dictionary contains
Processobjects. Used to iterate through while creating the processes from the text files provided.
pid- this will be the Process ID, an
int, assigned to each process uniquely.
burst_times- deque containing individual
burst_timevariables. Used to pop and append
burst_timefor running or waiting process.
wait_times- deque containing individual
wait_timevariables. Used to pop and append
wait_timefor running or waiting process.
arrival_time- time at which the process arrives in the program. Checked in the beginning of the program with a loop testing
inputfile- file to create the process from.
ready- this queue will be the “ready” queue for
processobjects that are ready to be run by the CPU.
wait- this queue will be the “wait” queue for
processobjects that are waiting for their wait time to decrease.