Skip to content

Commit

Permalink
Added Operating system to store processes Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
hernandesmacedo committed Dec 2, 2021
1 parent 2327b30 commit c3765e4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
49 changes: 27 additions & 22 deletions classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Queue:
"""
def __init__(self, quantum):
"""
Initiate a new empty Queue with quantum value provided from Scheduler based
Initiate a new empty Queue with quantum value based
on priority of processes that will be in this queue.
"""
self.first = None
Expand All @@ -66,23 +66,21 @@ def end_queue(self):
"""Frees Queue memory space when all PCB processes in it have been finished."""
del self

class Scheduler:
class OS:
"""
Scheduler is responsible for sorting all Queues and processes in it.
It uses priority rule to arrange the Queues and then, uses Round Robin to
organize how processes will use CPU.
Operating system that manages processes Queues.
Creates an object containing:
Creates an Operating system object containing:
`sorted_queues`: A list with all Queues with different priorities. This list is sorted by priority,
so in first list index will be the Queue of processes with highest priority,
and in the last list index will be the Queue of processes with lowest priority.
"""
def __init__(self):
"""Initiate a new empty Scheduler with no Queue."""
"""Initiate a new OS with no processes Queue."""
self.sorted_queues = []

def __repr__(self):
"""Formats the way we want to see a Scheduler and its Queues."""
"""Formats the way we want to see a OS and its Queues."""
description = ""
for queue in self.sorted_queues:
priority_queue = queue.first.priority
Expand All @@ -97,15 +95,15 @@ def __repr__(self):

def queue_exists_in_sorted_queues(self, priority):
"""
Verifies if a Queue already exists in Scheduler by comparing priority value.
Verifies if a Queue already exists in OS by comparing priority value.
Args:
`self`: Scheduler
`priority`: priority to be searched in the Scheduler Queues
`self`: OS
`priority`: priority to be searched in the OS Queues
Returns:
None if there is not a Queue of processes with this `priority` value in `Scheduler`,
or returns the position of this Queue in the `Scheduler` sorted list of Queues.
None if there is not a Queue of processes with this `priority` value in `OS`,
or returns the position of this Queue in the `OS` sorted list of Queues.
"""
for queue in self.sorted_queues:
if(queue.first.priority == priority):
Expand All @@ -114,12 +112,12 @@ def queue_exists_in_sorted_queues(self, priority):

def new_position_in_sorted_queues(self, priority):
"""
If there is not a Queue of processes with a `priority` value in `Scheduler`,
If there is not a Queue of processes with a `priority` value in `OS`,
this method verifies where a new processes Queue must be inserted in order to
maintain the priority rule.
Args:
`self`: Scheduler
`self`: OS
`priority`: priority of the new Queue
Returns:
Expand All @@ -131,15 +129,15 @@ def new_position_in_sorted_queues(self, priority):
return self.sorted_queues.index(queue)
return len(self.sorted_queues)

def schedule_process(self, process):
def add_process(self, process):
"""
Receives a new PCB process and verifies if there is a Queue with same priority in Scheduler.
Receives a new PCB process and verifies if there is a Queue with same priority in OS.
If not, creates a new Queue and insert it in a position based in process
`priority` value, to maintain the priority rule.
If already exists a Queue with same priority in Scheduler, then just adds it to the end of this Queue.
If already exists a Queue with same priority in OS, then just adds it to the end of this Queue.
Args:
`self`: Scheduler
`self`: OS
`process`: PCB process object.
"""
queue_position = self.queue_exists_in_sorted_queues(process.priority)
Expand All @@ -151,6 +149,12 @@ def schedule_process(self, process):
new_position = self.new_position_in_sorted_queues(process.priority)
self.sorted_queues.insert(new_position, new_queue)

class Scheduler:
"""
Scheduler is responsible for managing which process will use CPU.
For it, Scheduler uses priority order and Round Robin rules.
"""

def context_switch(self, queue, running_process, time_unit):
"""
This method executes the Context Switch.
Expand Down Expand Up @@ -200,7 +204,7 @@ def context_switch(self, queue, running_process, time_unit):
del aux
return queue.first

def round_robin(self, time_unit):
def round_robin(self, time_unit, operating_system):
"""
This method simulates the Round Robin execution of processes.
Because the processes Queue are executed respecting their priority values,
Expand All @@ -225,9 +229,10 @@ def round_robin(self, time_unit):
Args:
`self`: Scheduler
`time_unit`: the time unit that the Round Robin is happening.
`operating_system`: OS with processes Queues.
"""
logging.info("ROUND ROBIN EXECUTION:")
for queue in self.sorted_queues:
logging.info("ROUND ROBIN SCHEDULER EXECUTION:")
for queue in operating_system.sorted_queues:
running_process = queue.first
while running_process is not None:
runtime = queue.quantum
Expand Down
19 changes: 10 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
import random
from datetime import datetime, timedelta
from classes import PCB, Scheduler
from classes import PCB, Scheduler, OS
import logging
logging.basicConfig(filename = 'output.log', level=logging.DEBUG, format='%(message)s')

def processes_creator(scheduler):
def processes_creator(operating_system):
"""Generates random processes until there are 4 different priorities and at least 18 different processes created.
Args:
`scheduler`: Scheduler receiving and organizing order processes according to their priority and creation time.
`operating_system`: operating_system receiving and managing processes.
"""
end_address = 9999
date_time = datetime.now().replace(microsecond=0)
number_of_processes = 0

while len(scheduler.sorted_queues) != 4 or number_of_processes < 18:
while len(operating_system.sorted_queues) != 4 or number_of_processes < 18:
name = 'PROCESS {:02d}'.format(number_of_processes + 1)
priority = random.randint(1, 4)
date_time += timedelta(minutes=1)
init_address = end_address + 1
end_address = init_address + random.randint(1, 20)
remaining_time = random.randint(2, 18)
process = PCB(name, priority, date_time, init_address, end_address, remaining_time)
scheduler.schedule_process(process)
operating_system.add_process(process)
number_of_processes += 1

def main():
operating_system = OS()
scheduler = Scheduler()

time_unit = 0

processes_creator(scheduler)
processes_creator(operating_system)

logging.info("SCHEDULER:")
logging.info(scheduler)
logging.info("OPERATING SYSTEM:")
logging.info(operating_system)

scheduler.round_robin(time_unit)
scheduler.round_robin(time_unit, operating_system)

main()

0 comments on commit c3765e4

Please sign in to comment.