Question: Module 1 0 Homework - Priority Queue Implementation for Task Management Overview We will implement a priority queue to manage the scheduling of tasks in
Module Homework Priority Queue
Implementation for Task Management
Overview
We will implement a priority queue to manage the scheduling of tasks in an operating system. Tasks with
higher priority should be executed first. You are provided with a skeleton code for three classes Entry,
MaxHeap, and TaskManager. You will complete the implementation of the Entry class and MaxHeap class.
Requirements:
Entry Class Implementation:
Implement the comparison methods in the provided Entry class to compare entries based on
priority.
MaxHeap Class Implementation:
Implement methods to insert entries, remove the maximum priority entry, maintain the heap
property, and change the priority of existing tasks.
Implement heapifying methods to ensure the heap property is maintained after insertion,
removal, and priority change operations.
TaskManager Class Implementation:
The TaskManager class includes methods to add tasks, remove tasks with the highest priority.
Use the MaxHeap class methods to manage the task queue efficiently.
Examples
Several examples of behavior shown below.
# Creating entries
entry Entrypriority processid"Process
entry Entrypriority processid"Process
entry Entrypriority processid"Process
entry Entrypriority processid"Process
entry Entrypriority processid"Process
entry Entrypriority processid"Process
entry Entrypriority processid"Process
# Initializing MaxHeap
maxheap MaxHeap
# Adding entries to the max heap
maxheap.putentry
maxheap.putentry
maxheap.putentry
maxheap.putentry
hwmd
maxheap.putentry
maxheap.putentry
maxheap.putentry
# Changing priority of a process
maxheap.changepriorityprocessid"Process newpriority
# Changing priority of a process
maxheap.changepriorityprocessid"Process newpriority
# Removing processes until heap is empty
while maxheap:
printfRemoving max priority process: maxheap.removemax
# Expected output:
# Removing max priority process: Process
# Removing max priority process: Process
# Removing max priority process: Process
# Removing max priority process: Process
# Removing max priority process: Process
# Removing max priority process: Process
# Removing max priority process: Process
Starter Code:
class Entry:
Represents an entry in the priority queue."""
def initself priority, processid:
Initializes an Entry object with a given priority and process ID
self.priority priority
self.processid processid
def reprself:
Returns a string representation of the Entry object."""
return fEntrypriorityselfpriority processidselfprocessid
####### Implement all Entry class methods under this line #######
def gtself other:
Compares the priority of this entry with another entry.
Returns:bool: True if this entry has higher priority than the other, False otherwise."""
def eqself other:
Checks if this entry is equal to another entry based on priority.
Returns:bool: True if the priorities are equal, False otherwise."""
class MaxHeap:
Represents a max heap data structure."""
def initself:
Initializes a MaxHeap object."""
self.heap
####### Implement all MaxHeap class methods under this line #######
def putself entry:
Inserts an entry into the max heap."""
def removemaxself:
Removes and returns the entry with the maximum priority from the max heap.
Returns:The process ID that was removed from the queue. raise IndexError if the heap is empty"""
def changepriorityself processid newpriority:
Changes the priority of a process in the max heap.
Returns:bool: True if the priority change was successful, False otherwise."""
def upheapself index:
Performs upheap operation to maintain heap property after insertion."""
def downheapself index:
Performs downheap operation to maintain heap property after removal."""
def lenself:
len is number of items in PQ
class TaskManager:
Manages the execution of processes using a priority queue."""
def initself:
Initializes a TaskManager object."""
self.processorqueue MaxHeap
####### Implement all TaskManager class methods under this line #######
def addprocessself entry:
Adds a process to the processor queue."""
def removeprocessself:
Removes and returns the process with the highest priority from the processor queue."""
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
