Answered step by step
Verified Expert Solution
Link Copied!

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 3: 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 8 programs on a device with only 2 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 high-priority queue or a low-priority queue, depending on how important the program is. For example, processes that are fundamental to how the device operates (e.g. displaying things to the screen, dealing with system input and output) have a higher priority than user-installed applications (e.g. web browsers, music playing app, calculator app). At any given time, jobs waiting in the high-priority queue will be executed first, in the order that they were requested in. If there are no high-priority jobs waiting to be executed, then the jobs in the low-priority queue can be executed.
Problem:
Download lab6_priority.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 (get_job(), and process_complete()). 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 (i.e. every time get_job() is called), that job object is enqueued to the appropriate queue:
high_priority_queue or low_priority_queue.
Complete the main() function so that whenever a process has finished (indicated when
process_complete() returns True), a new process is started by dequeuing a job from the
appropriate queue. i.e. If there is at least one job in the high-priority queue, it should be dequeued and assigned to the current_job variable. However, if the high-priority queue is empty and there is at least one job in the low-priority queue, then it should be dequeued and assigned to the current_job variable. If a job has been successfully dequeued from either queue, the process_running variable should be set to True.
lab6_priority.py
import random
from queues import CircularQueue
class Job:
def __init__(self, priority =0, process_name = None):
'''
Object for job description of various types
:param priority: 0 for low and 1 for high priority
:param process_name: Description of the process (optional)
'''
self.__id = random.randint(1,1000)
self.__priority = priority
if process_name is None:
if self.high_priority():
self.__process_name = random.choice(['[OS] File Write', '[OS] File Read', '[OS] Display'])
else:
self.__process_name = random.choice(['[USER] Browser', '[USER] Music', '[USER] Calculator'])
def high_priority(self):
'''
Priority of the job
:return: True if process of high priority
'''
return self.__priority ==1
def process_name(self):
'''
Process name of the job
:return: returns the process name for the job
'''
return self.__process_name
def __str__(self):
return '{:15} : {:20}
{:15} : {:20}
{:15} : {:20}'.format('ID',self.__id, 'Process Name',self.__process_name, 'Priority','HIGH' if self.__priority ==1 else 'LOW' )
def get_job():
'''
Return a job type , trying to simulate a queueing process for the CPU
:return: Returns the Job Object or none
'''
if random.random().5:
return Job(priority=1)
if random.random().9:
return Job(priority=0)
return None # the no job
def process_complete():
'''
Returns a boolean value telling if the current process is complete executing or not
:return: True/False depending the process is complete or not
'''
if random.random()0.5:
return True
return False
def main():
process_running = False # tells the state of the processor True if a process is running
current_job = None
high_priority_queue = CircularQueue(1000)
low_priority_queue = CircularQueue(1000)
# we will run our computer for 10 time steps
time_steps =10
for t in range(time_steps):
print("######## RUN : {} ########
".format(t+1))
job = get_job() # get a job
if job:
print("Job {} generated
".format(job.process_name()))
# Put the job in the appropriate queue
########################
### ENTER YOUR CODE ####
########################
########################
#### END OF CODE ######
########################print("Jobs waiting in High Priority Queue :{}".format(high_priority_queue.size()))
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago