Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Exercise 3 : Using High and Low Priority Queues: Background information: Computing devices like laptops and smartphones may have multiple computing cores, which allow us
Exercise : Using High and Low Priority Queues:
Background information:
Computing devices like laptops and smartphones may have multiple computing cores, which allow us to run multiple programs at once. But what happens when the number of programs that we want to run is more than the number of cores in our device? For example, what if we want to run programs on a device with only CPU cores? Job scheduling helps the device to run the most important programs first. Imagine that each program we want to run submits a job request to the operating system before it is actually run. Those jobs are stored in either a highpriority queue or a lowpriority queue, depending on how important the program is For example, processes that are fundamental to how the device operates eg displaying things to the screen, dealing with system input and output have a higher priority than userinstalled applications eg web browsers, music playing app, calculator app At any given time, jobs waiting in the highpriority queue will be executed first, in the order that they were requested in If there are no highpriority jobs waiting to be executed, then the jobs in the lowpriority queue can be executed.
Problem:
Download labpriority.py from eClass, and save in the same folder as your other lab files. This file contains a Job class definition, as well as two functions that have already been completed for you getjob and processcomplete Read the comments for this code to understand what it does Add code to the main function in this file so that every time a new job is created ie every time getjob is called that job object is enqueued to the appropriate queue:
highpriorityqueue or lowpriorityqueue.
Complete the main function so that whenever a process has finished indicated when
processcomplete returns True a new process is started by dequeuing a job from the
appropriate queue. ie If there is at least one job in the highpriority queue, it should be dequeued and assigned to the currentjob variable. However, if the highpriority queue is empty and there is at least one job in the lowpriority queue, then it should be dequeued and assigned to the currentjob variable. If a job has been successfully dequeued from either queue, the processrunning variable should be set to True.
labpriority.py
import random
from queues import CircularQueue
class Job:
def initself priority processname None:
Object for job description of various types
:param priority: for low and for high priority
:param processname: Description of the process optional
self.id random.randint
self.priority priority
if processname is None:
if self.highpriority:
self.processname random.choiceOS File Write', OS File Read', OS Display'
else:
self.processname random.choiceUSER Browser', USER Music', USER Calculator'
def highpriorityself:
Priority of the job
:return: True if process of high priority
return self.priority
def processnameself:
Process name of the job
:return: returns the process name for the job
return self.processname
def strself:
return : : :
: : :
: : :formatIDself.id 'Process Name',self.processname, 'Priority','HIGH' if self.priority else 'LOW'
def getjob:
Return a job type trying to simulate a queueing process for the CPU
:return: Returns the Job Object or none
if random.random:
return Jobpriority
if random.random:
return Jobpriority
return None # the no job
def processcomplete:
Returns a boolean value telling if the current process is complete executing or not
:return: TrueFalse depending the process is complete or not
if random.random:
return True
return False
def main:
processrunning False # tells the state of the processor True if a process is running
currentjob None
highpriorityqueue CircularQueue
lowpriorityqueue CircularQueue
# we will run our computer for time steps
timesteps
for t in rangetimesteps:
print######## RUN : ########
formatt
job getjob # get a job
if job:
printJob generated
formatjobprocessname
# Put the job in the appropriate queue
########################
### ENTER YOUR CODE ####
########################
########################
#### END OF CODE ######
########################printJobs waiting in High Priority Queue :formathighpriorityqueue.size
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started