Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

LAB6_PRIORITY.PY Exercise 3: Using High and Low Priority Queues: Background information: Computing devices like laptops and smartphones may have multiple computing cores, which allow us

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

LAB6_PRIORITY.PYimage text in transcribedimage text in transcribedimage text in transcribed

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 & 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: 1. 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. 2. 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. Hint: use the Job's high_priority() method to check if a job is high-priority (True) or low- priority (False). Notice that get_job() may also return no job (None) which will NOT go into either queue, so that must be checked for as well. 3. 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. Sample Output (your output may differ since the jobs are generated randomly): ## ### ### RUN : 1 # # # # # # # # Job [USER] Music generated [PROCESSOR] Busy Jobs waiting in High Priority Queue :0 Jobs waiting in Low Priority Queue : 0 ## ### ### RUN : 2 ######## Job [OS] Display generated [PROCESSOR] Busy Jobs waiting in High Priority Queue : 1 Jobs waiting in Low Priority Queue : 0 #### #### RUN : 3 ######## Job [USER) Browser generated JOB COMPLETED ID Process Name Priority : 142 : [USER] Music : LOW [PROCESSOR] Busy Jobs waiting in High Priority Queue :0 Jobs waiting in Low Priority Queue : 1 ### ### ## RUN : 4 ######## Job [USER] Browser generated [PROCESSOR] Busy Jobs waiting in High Priority Queue : 0 Jobs waiting in Low Priority Queue : 2 ## ### ### RUN : 5 ### ### ## Job [OS] File Read generated JOB COMPLETED ID : 329 Process Name Priority : [OS] Display HIGH [PROCESSOR] Busy Jobs waiting in High Priority Queue :0 Jobs waiting in Low Priority Queue : 2 ## # # # # # # RUN : 6 # # # # # # # # Job [USER) Calculator generated JOB COMPLETED ID : 167 Process Name : [OS] File Read Priority : HIGH [PROCESSOR] Busy Jobs waiting in High Priority Queue : 0 Jobs waiting in Low Priority Queue : 2 # # # # # # # # RUN : 7 # # # # # # # # Job [USER] Music generated [PROCESSOR] Busy Jobs waiting in High Priority Queue : 0 Jobs waiting in Low Priority Queue :3 ### ## # # # RUN : 8 #### #### Job [OS] File Read generated JOB COMPLETED ID Process Name Priority : 486 : [USER] Browser LOW [PROCESSOR] Busy Jobs waiting in High Priority Queue :0 Jobs waiting in Low Priority Queue :3 ######## RUN : 9 # # # # # # # # Job [USER) Browser generated [PROCESSOR] Busy Jobs waiting in High Priority Queue : 0 Jobs waiting in Low Priority Queue : 4 # # # # # # # # RUN : 10 ### ### ## Job [USER) Calculator generated [PROCESSOR] Busy Jobs waiting in High Priority Queue : 0 Jobs waiting in Low Priority Queue : 5 import random from queues import Circular Queue class Job: definit__(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._procesa_nane - random.choice (I'IOSPile Write', 'TOS] File Read', '[OS] Display']) else: self._process_name = random.choice(ICUSER) Browser', 'LUSER] sic', 'LUSER] Calculator']) def high priority (self): Priority of the job :return: True ir process of high priority return self.__priority == 1 def process name (self): Process name of the ob return; returns the process name for the job return selb._process_name def str return (self): '(:

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

Step: 3

blur-text-image

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

Spatial Databases A Tour

Authors: Shashi Shekhar, Sanjay Chawla

1st Edition

0130174807, 978-0130174802

Students also viewed these Databases questions

Question

What are some of the possible scenes from our future?

Answered: 1 week ago