from os import system
import sys
def clear_print():
system('clear')
def printer():
global set_ctr
clear_print()
print("SET {0} ALLOCATION".format(set_ctr))
print("\nMEMORY\t\t\tPARTITION SIZE\tALLOC\\DEALLOC\n""OS Partition\t{0:g}\t\t\t\tOS".format(OS_size))
ctr = 1
for item in partitions:
if ctr >= 10:
if allocations.get(item) is None:
print("{0}\t{1:g}".format(item, partitions[item]))
else:
print("{0}\t{1:g}\t\t\t\t{2}".format(item, partitions[item], partition_job.get(item)))
else:
ctr += 1
if allocations.get(item) is None:
print("{0}\t\t{1:g}".format(item, partitions[item]))
else:
print("{0}\t\t{1:g}\t\t\t\t{2}".format(item, partitions[item], partition_job.get(item)))
def allocation():
global set_ctr
available_storage = memory_size - sum(partitions.values(), OS_size)
partition_counter = 1
partition_number = "Partition {0}".format(partition_counter)
for i in list(jobs.keys()):
if jobs[i] <= available_storage and i not in currently:
partitions[partition_number] = jobs[i]
allocations[partition_number] = jobs[i]
partition_job[partition_number] = i
currently.append(i)
partition_counter += 1
partition_number = "Partition {0}".format(partition_counter)
available_storage = memory_size - sum(partitions.values(), OS_size)
if available_storage != 0:
partitions[partition_number] = available_storage
if len(allocations) != 0:
printer()
input("\nPress any key to continue...")
set_ctr += 1
currently.clear()
deallocation()
def deallocation():
for i in list(partition_job.values()):
for j in list(partition_job.keys()):
if partition_job[j] == i:
tat[i] = tat[i] - 1
if tat[i] == 0:
allocations.pop(j)
partition_job.pop(j)
jobs.pop(i)
partitions.clear()
partition_job.clear()
allocations.clear()
allocation()
def validator(string, num):
if is_number(num):
while float(num) <= 0:
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
num = validator(string, input(string))
else:
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
num = validator(string, input(string))
return float(num)
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
clear_print()
memory_size = validator("Enter memory size in KB: ", input("Enter memory size in KB: "))
OS_size = validator("Enter OS size in KB: ", input("Enter OS size in KB: "))
partitions = dict()
jobs = dict()
tat = dict()
allocations = dict()
partition_job = dict()
currently = []
set_ctr = 1
exit_ctr = 0
# Validates OS and memory size
while OS_size >= memory_size:
print("OS size cannot be greater than or equal to memory size.")
input("\nPress any key to continue...")
clear_print()
memory_size = validator("Enter memory size in KB: ", input("Enter memory size in KB: "))
OS_size = validator("Enter OS size in KB: ", input("Enter OS size in KB: "))
# Gets jobs
clear_print()
for x in range(10):
jobs["Job {0}".format(x + 1)] = validator("Enter Job {0}: ".format(x + 1), input("Enter Job {0}: ".format(x + 1)))
tat["Job {0}".format(x + 1)] = validator("Enter TAT for Job {0}: ".format(x + 1), input("Enter TAT for Job {0}: ".format(x + 1)))
print()
input("\nPress any key to continue...")
# Prints jobs list
clear_print()
print("JOB LIST\n")
for x in range(len(jobs)):
job = "Job {0}".format(x + 1)
print("{0}: {1:g}\tTAT:{2:g}".format(job, jobs[job], tat[job]))
input("\nPress any key to continue...")
allocation()
deallocation()
# Prints the conclusion
clear_print()
print("There were {0} set(s) of allocation.".format(set_ctr - 1))
if len(jobs) == 0:
input("All jobs were served/executed.\n\nPress any key to continue...")
else:
if len(jobs) == 1:
print("{0}".format(list(jobs.keys())[0]), end=" ")
else:
for jobs_item in jobs:
if jobs_item == list(jobs.keys())[-1]:
print("and {0}".format(jobs_item), end=" ")
else:
print(jobs_item, end=", ")
input("were not served/executed.\n\nPress any key to continue...")
What I have tried:
Have difficulty to understand and convert it. Please help me