Question: implement the following variation that gives priority to processes that are further from being finished. Processes that have more than 50% of their execution time
implement the following variation that gives priority to processes that are further from being finished.
Processes that have more than 50% of their execution time left to compute will be allowed two time slices, while processes that have 50% or less of their execution time left will be allowed one time slice.
Use the data following data file:
prio-procs.txt
the contents are as follows:
3 P1,11 P2,15 P3,13 P4,8 P5,10 P6,9
to test your algorithm.
Note: The data file has two items in it, Process ID and Execution Time. In order to compute the percentage of a process that is left to execute, you will have to store both the total time needed for the process and the remaining time needed by the process. You can do that by duplicating the execution time when you store the process in the queue. That is, each process is stored in the data file as follows:
P1,12
where P1 is the process ID and 12 is the execution time. When you read the data from the data file, you can duplicate the execution time and store the following in the queue:
P1,12,12
Where the first 12 is the remaining time and the second 12 is the total time.
This needs to be written in python and some skeleton code will be written down below. Basically it just needs to have a time slice of 6 for each process
skeleton code:
import queue
import random
# Function to open the file using exception handling
def openFile():
goodFile = False
while goodFile == False:
fname = input("Enter name of data file: ")
try:
inFile = open(fname, 'r')
goodFile = True
except IOError:
print("Invalid filename, please try again ... ")
return inFile
# Function to get the time slice value and the processes from the file into the queue
# Queue will contain a string with process ID and exec time separated by a comma
def getProcs(cpuQ):
infile = openFile()
# Get the first line in the file containing the time slice value
line = infile.readline()
# Strip the from the line and convert to an integer
tslice = int(line.strip())
# Loop through the file inserting processes into the queue
for line in infile:
proc = line.strip()
cpuQ.put(proc)
infile.close()
return tslice, cpuQ
# Function to print the contents of the queue
def printQueue(tslice, cpuQ):
print("The time slice is ",tslice, " The contents of the queue are: ")
for i in range(cpuQ.qsize()):
proc = cpuQ.get()
cpuQ.put(proc)
print(proc)
# Function to execute the processes in the queue
def scheduleProcs(tslice, cpuQ):
# While the queue is not empty
while (cpuQ.empty() != True):
# Get next process from queue
proc = cpuQ.get()
# Separate the process ID and the execution time from the process info
PID, exectime = proc.split(",")
# Convert exectime to an integer
exectime = int(exectime)
print("Getting next process - Process ", PID," has ", exectime," instructions to execute")
# Initialize the timer
timer = 0
# While proc still has time in slice and still has code to execute
while (timer < tslice) and (exectime > 0):
# In a real computer, the OS would take an instruction from process out of memory and execute it
# Execute an instruction of process
exectime = exectime - 1
# Count one tick of the timer
timer = timer + 1
print("Executing instruction ", exectime," of process ", PID,". Timer = ", timer)
# If proc still has instructions to execute put it back in the queue
if (exectime > 0):
# Create string with new exec time and process ID
proc = PID + "," + str(exectime)
# Put the process back in the queue
cpuQ.put(proc)
print("Put process ", PID," back in queue with ", exectime," instructions left to execute")
else:
print("*** Process ", PID, " Complete ***")
return
# Main function
def main():
# Create the scheduling queue
cpuQ = queue.Queue()
# Get the processes from the data file
tslice, cpuQ = getProcs(cpuQ)
# Print the queue
printQueue(tslice, cpuQ)
# Schedule the processes
scheduleProcs(tslice, cpuQ)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
