0% found this document useful (0 votes)
12 views12 pages

Os Lab

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)
12 views12 pages

Os Lab

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/ 12

Write a program to implement FCFS CPU scheduling algorithm.

class Process:
def __init__(self, pid, arrival_time, burst_time):
self.pid = pid
self.arrival_time = arrival_time
self.burst_time = burst_time
self.waiting_time = 0
self.turnaround_time = 0

def find_waiting_time(processes):
n = len(processes)
waiting_time = [0] * n

# First process always has 0 waiting time


waiting_time[0] = 0

# Calculate waiting time for each process


for i in range(1, n):
waiting_time[i] = (processes[i-1].burst_time + waiting_time[i-1])

return waiting_time

def find_turnaround_time(processes, waiting_time):


n = len(processes)
turnaround_time = [0] * n

# Calculate turnaround time for each process


for i in range(n):
turnaround_time[i] = processes[i].burst_time + waiting_time[i]

return turnaround_time
def find_average_time(processes):
waiting_time = find_waiting_time(processes)
turnaround_time = find_turnaround_time(processes, waiting_time)

total_waiting_time = sum(waiting_time)
total_turnaround_time = sum(turnaround_time)

n = len(processes)

print("Process ID | Arrival Time | Burst Time | Waiting Time | Turnaround Time")


print("--------------------------------------------------------------")
for i in range(n):
print(f"{processes[i].pid}\t\t{processes[i].arrival_time}\t\
t{processes[i].burst_time}\t\t{waiting_time[i]}\t\t{turnaround_time[i]}")

print(f"\nAverage Waiting Time: {total_waiting_time / n:.2f}")


print(f"Average Turnaround Time: {total_turnaround_time / n:.2f}")

def main():
# Sample input: List of processes with (Process ID, Arrival Time, Burst Time)
processes = [
Process(1, 0, 5),
Process(2, 1, 3),
Process(3, 2, 8),
Process(4, 3, 6)
]

# Sort processes based on arrival time


processes.sort(key=lambda x: x.arrival_time)

find_average_time(processes)
if __name__ == "__main__":
main()

OUTPUT
--------------------
1 0 5 05
2 1 3 58
3 2 8 816
4 3 6 16 22

Average Waiting Time: 7.25


Average Turnaround Time: 12.75
PS D:\OS>

class Process:
2 def __init__(self, pid, arrival_time, burst_time):
3 self.pid = pid
4 self.arrival_time = arrival_time
5 self.burst_time = burst_time
6 self.waiting_time = 0
7 self.turnaround_time = 0
8
9def find_waiting_time(processes):
10 n = len(processes)
11 waiting_time = [0] * n
12 remaining_processes = processes.copy()
13 current_time = 0
14
15 while remaining_processes:
16 # Filter processes that have arrived
17 available_processes = [p for p in remaining_processes if p.arrival_time
<= current_time]
18
19 if available_processes:
20 # Select the process with the shortest burst time
21 shortest_process = min(available_processes, key=lambda x:
x.burst_time)
22 waiting_time[shortest_process.pid - 1] = current_time -
shortest_process.arrival_time
23 current_time += shortest_process.burst_time
24
25 # Remove the selected process from the list
26 remaining_processes.remove(shortest_process)
27 else:
28 # If no process has arrived, move time forward
29 current_time += 1
30
31 return waiting_time
32
33def find_turnaround_time(processes, waiting_time):
34 n = len(processes)
35 turnaround_time = [0] * n
36
37 # Calculate turnaround time for each process
38 for i in range(n):
39 turnaround_time[i] = processes[i].burst_time + waiting_time[i]
40
41 return turnaround_time
42
43def find_average_time(processes):
44 waiting_time = find_waiting_time(processes)
45 turnaround_time = find_turnaround_time(processes, waiting_time)
46
47 total_waiting_time = sum(waiting_time)
48 total_turnaround_time = sum(turnaround_time)
49
50 n = len(processes)
51
52 print("Process ID | Arrival Time | Burst Time | Waiting Time | Turnaround
Time")
53 print("--------------------------------------------------------------")
54 for i in range(n):
55 print(f"{processes[i].pid}\t\t{processes[i].arrival_time}\t\
t{processes[i].burst_time}\t\t{waiting_time[i]}\t\t{turnaround_time[i]}")
56
57 print(f"\nAverage Waiting Time: {total_waiting_time / n:.2f}")
58 print(f"Average Turnaround Time: {total_turnaround_time / n:.2f}")
59
60def main():
61 # Sample input: List of processes with (Process ID, Arrival Time, Burst Time)
62 processes = [
63 Process(1, 0, 8),
64 Process(2, 1, 4),
65 Process(3, 2, 9),
66 Process(4, 3, 5)
67 ]
68
69 # Sort processes based on arrival time
70 processes.sort(key=lambda x: x.arrival_time)
71
72 find_average_time(processes)
73
74if __name__ == "__main__":
75 main()

Write a program to implement SJF CPU scheduling algorithm.


class Process:
2 def __init__(self, pid, arrival_time, burst_time):
3 self.pid = pid
4 self.arrival_time = arrival_time
5 self.burst_time = burst_time
6 self.waiting_time = 0
7 self.turnaround_time = 0
8
9def find_waiting_time(processes):
10 n = len(processes)
11 waiting_time = [0] * n
12 remaining_processes = processes.copy()
13 current_time = 0
14
15 while remaining_processes:
16 # Filter processes that have arrived
17 available_processes = [p for p in remaining_processes if p.arrival_time
<= current_time]
18
19 if available_processes:
20 # Select the process with the shortest burst time
21 shortest_process = min(available_processes, key=lambda x:
x.burst_time)
22 waiting_time[shortest_process.pid - 1] = current_time -
shortest_process.arrival_time
23 current_time += shortest_process.burst_time
24
25 # Remove the selected process from the list
26 remaining_processes.remove(shortest_process)
27 else:
28 # If no process has arrived, move time forward
29 current_time += 1
30
31 return waiting_time
32
33def find_turnaround_time(processes, waiting_time):
34 n = len(processes)
35 turnaround_time = [0] * n
36
37 # Calculate turnaround time for each process
38 for i in range(n):
39 turnaround_time[i] = processes[i].burst_time + waiting_time[i]
40
41 return turnaround_time
42
43def find_average_time(processes):
44 waiting_time = find_waiting_time(processes)
45 turnaround_time = find_turnaround_time(processes, waiting_time)
46
47 total_waiting_time = sum(waiting_time)
48 total_turnaround_time = sum(turnaround_time)
49
50 n = len(processes)
51
52 print("Process ID | Arrival Time | Burst Time | Waiting Time | Turnaround
Time")
53 print("--------------------------------------------------------------")
54 for i in range(n):
55 print(f"{processes[i].pid}\t\t{processes[i].arrival_time}\t\
t{processes[i].burst_time}\t\t{waiting_time[i]}\t\t{turnaround_time[i]}")
56
57 print(f"\nAverage Waiting Time: {total_waiting_time / n:.2f}")
58 print(f"Average Turnaround Time: {total_turnaround_time / n:.2f}")
59
60def main():
61 # Sample input: List of processes with (Process ID, Arrival Time, Burst Time)
62 processes = [
63 Process(1, 0, 8),
64 Process(2, 1, 4),
65 Process(3, 2, 9),
66 Process(4, 3, 5)
67 ]
68
69 # Sort processes based on arrival time
70 processes.sort(key=lambda x: x.arrival_time)
71
72 find_average_time(processes)
73
74if __name__ == "__main__":
75 main()

OUTPUT
PS D:\OS> &
C:/Users/shivs/AppData/Local/Microsoft/WindowsApps/python3.12.exe
d:/OS/PROGRAM2.PY
Process ID | Arrival Time | Burst Time | Waiting Time | Turnaround Time
--------------------------------------------------------------
1 0 8 0 8
2 1 4 7 11
3 2 9 15 24
4 3 5 9 14

Average Waiting Time: 7.75


Average Turnaround Time: 14.25
PS D:\OS>

Write a program to implement Priority CPU scheduling algorithm.


class Process:
def __init__(self, pid, arrival_time, burst_time, priority):
self.pid = pid
self.arrival_time = arrival_time
self.burst_time = burst_time
self.priority = priority
self.waiting_time = 0
self.turnaround_time = 0

def find_waiting_time(processes):
n = len(processes)
waiting_time = [0] * n
remaining_processes = processes.copy()
current_time = 0

while remaining_processes:
# Filter processes that have arrived
available_processes = [p for p in remaining_processes if p.arrival_time <=
current_time]

if available_processes:
# Select the process with the highest priority (lowest priority number)
highest_priority_process = min(available_processes, key=lambda x:
x.priority)
waiting_time[highest_priority_process.pid - 1] = current_time -
highest_priority_process.arrival_time
current_time += highest_priority_process.burst_time

# Remove the selected process from the list


remaining_processes.remove(highest_priority_process)
else:
# If no process has arrived, move time forward
current_time += 1

return waiting_time

def find_turnaround_time(processes, waiting_time):


n = len(processes)
turnaround_time = [0] * n

# Calculate turnaround time for each process


for i in range(n):
turnaround_time[i] = processes[i].burst_time + waiting_time[i]

return turnaround_time

def find_average_time(processes):
waiting_time = find_waiting_time(processes)
turnaround_time = find_turnaround_time(processes, waiting_time)

total_waiting_time = sum(waiting_time)
total_turnaround_time = sum(turnaround_time)

n = len(processes)

print("Process ID | Arrival Time | Burst Time | Priority | Waiting Time |


Turnaround Time")
print("---------------------------------------------------------------------------------")
for i in range(n):
print(f"{processes[i].pid}\t\t{processes[i].arrival_time}\t\
t{processes[i].burst_time}\t\t{processes[i].priority}\t\t{waiting_time[i]}\t\
t{turnaround_time[i]}")

print(f"\nAverage Waiting Time: {total_waiting_time / n:.2f}")


print(f"Average Turnaround Time: {total_turnaround_time / n:.2f}")

def main():
# Sample input: List of processes with (Process ID, Arrival Time, Burst Time,
Priority)
processes = [
Process(1, 0, 10, 2),
Process(2, 1, 4, 1),
Process(3, 2, 6, 3),
Process(4, 3, 8, 2)
]

# Sort processes based on arrival time


processes.sort(key=lambda x: x.arrival_time)

find_average_time(processes)

if __name__ == "__main__":
main()

OUTPUT:-
PS D:\OS> &
C:/Users/shivs/AppData/Local/Microsoft/WindowsApps/python3.12.exe
d:/OS/PROGRAM3.PY
Process ID | Arrival Time | Burst Time | Priority | Waiting Time | Turnaround Time
---------------------------------------------------------------------------------
1 0 10 2 0 10
2 1 4 1 9 13
3 2 6 3 20 26
4 3 8 2 11 19

Average Waiting Time: 10.00


Average Turnaround Time: 17.00
PS D:\OS>

You might also like