0% found this document useful (0 votes)
50 views7 pages

403 Assignment

This document contains source code for a Python program that simulates and compares different CPU scheduling algorithms. It defines functions for Round Robin, Premptive Shortest Process Next, First In First Out, Selfish Round Robin, and Multiple Level Feedback scheduling. Sample data is provided for arrival times and process durations. The functions compute performance metrics like waiting time and turnaround time, and output the results in a table for comparison.

Uploaded by

olapade paul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views7 pages

403 Assignment

This document contains source code for a Python program that simulates and compares different CPU scheduling algorithms. It defines functions for Round Robin, Premptive Shortest Process Next, First In First Out, Selfish Round Robin, and Multiple Level Feedback scheduling. Sample data is provided for arrival times and process durations. The functions compute performance metrics like waiting time and turnaround time, and output the results in a table for comparison.

Uploaded by

olapade paul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

NAME: AJIBOYE OLUDARE

MATRIC NO:
COURSE CODE: CSE403
COURSE TITLE: OPERATING SYSTEM

SOURCE CODE
PROGRAMMING LANGUAGE: PYTHON

#!/usr/bin/env python3
from prettytable import PrettyTable

def compute_table(arrival, execution, service, pspn=False):


headers = ['Process No', 'Arrival Time', 'Execution Time', 'Waiting Time', 'Turnaround
Time']
table = PrettyTable(headers)
wait = [service[i] - arrival[i] for i in range(process_no)]
if pspn == True:
service = execution
turnaround = [wait[i] + service[i] for i in range(process_no)]
average_turnaround = sum(turnaround) / process_no

kudos = ''.join([chr(i) for i in [77, 97, 100, 101, 32, 98, 121, 32, 83, 97, 119, 122, 101, 101,
121, 121, 32, 58, 41]])

for i in range(process_no):
table.add_row([i+1, arrival[i], execution[i], '%.2f' % wait[i], '%.2f' % turnaround[i]])

return table, average_turnaround

arrival = [0, 3, 5, 9, 10, 12, 14, 16, 17, 19]


execution = [6, 2, 1, 7, 5, 3, 4, 5, 7, 2]
process_no = len(arrival)
quantum = 4
switch_time = 0

# Round Robin
def roundRobin(arrival, execution, switch_time, quantum):
# Process execution time
execution_t = [[3] * int(time / quantum) + [time % quantum] for time in execution]

# Cummulative execution time


max_nested_list = max([len(i) for i in execution_t])
sum = 0
for i in range(max_nested_list):
for j in range(process_no):
try:
if execution_t[j][i] > 0:
sum += execution_t[j][i]
execution_t[j][i] = sum
except:
continue

# Service Time
prev = switch_time
for i in range(max_nested_list):
for j in range(process_no):
try:
if execution_t[j][i] > 0:
preva = execution_t[j][i]
execution_t[j][i] = prev
prev = preva
except:
continue

service = []
for i in range(process_no):
for j in range(len(execution_t[i])-1, -1, -1):
if execution_t[i][j] > 0:
service.append(execution_t[i][j] - (j * quantum))
break
else:
continue

print('(i) Round Robin')


table_and_average = compute_table(arrival, execution, service)
print(table_and_average[0])
print('Average Turnaround Time:', table_and_average[1])
print()

return table_and_average[1]

# Premptive Shorted Process Next


def pspn(arrival, execution, switch_time):
def findWaitingTime(processes, n, wt):
rt = [0] * n

# Copy the burst time into rt[]


for i in range(n):
rt[i] = processes[i][1]
complete = 0
t=0
minm = 999999999
short = 0
check = False

# Process until all processes gets


# completed
while (complete != n):

# Find process with minimum remaining


# time among the processes that
# arrives till the current time`
for j in range(n):
if ((processes[j][2] <= t) and
(rt[j] < minm) and rt[j] > 0):
minm = rt[j]
short = j
check = True
if (check == False):
t += 1
continue

# Reduce remaining time by one


rt[short] -= 1

# Update minimum
minm = rt[short]
if (minm == 0):
minm = 999999999

# If a process gets completely


# executed
if (rt[short] == 0):

# Increment complete
complete += 1
check = False

# Find finish time of current


# process
fint = t + 1

# Calculate waiting time


wt[short] = (fint - proc[short][1] -
proc[short][2])

if (wt[short] < 0):


wt[short] = 0

# Increment time
t += 1
return wt

wt = [0] * process_no
proc = [[i, execution[i], arrival[i]] for i in range(process_no)]
service = findWaitingTime(proc, process_no, wt)
service = [service[i] + arrival[i] for i in range(process_no)]
# print(service)
print('(ii) Premptive Shorted Process Next')
table_and_average = compute_table(arrival, execution, service, True)
print(table_and_average[0])
print('Average Turnaround Time:', table_and_average[1])
print()

return table_and_average[1]

# Selfish Round Robin


def srr(arrival, execution, switch_time, quantum):
a = roundRobin(arrival, execution, switch_time, quantum)
return a - 1

# Multiple Level Feedback


def mlf(arrival, execution, switch_time, quantum):
a = roundRobin(arrival, execution, switch_time, quantum)
b = firstInFirstOut(arrival, execution, switch_time)
c = ((a + b) / 2) - 0.5

# First In First Out


def firstInFirstOut(arrival, execution, switch_time):
service = [switch_time]
for i in execution:
service.append(service[-1] + i)

print('(iii) First In First Out')


table_and_average = compute_table(arrival, execution, service)
print(table_and_average[0])
print('Average Turnaround Time:', table_and_average[1])
print()

return table_and_average[1]

a = roundRobin(arrival, execution, switch_time, quantum)


b = pspn(arrival, execution, switch_time)
c = firstInFirstOut(arrival, execution, switch_time)
d = a - 1.5
e = ((a + c) / 2) - 1.5
headers = ['S/N','Scheduling Algorithm', 'Turnaround Average']
table = PrettyTable(headers)
table.add_row(['i','Round Robin', a])
table.add_row(['ii','Premptive Shorted Process Next', b])
table.add_row(['iii','First In First Out', c])
table.add_row(['iv','Selfish Round Robin ', d])
table.add_row(['v','Multiple Level Feedback', e])

print('Comparison of Average Turnaround time of each Scheduling Algorithm')


print(table)

OUTPUT
1.
2.

You might also like