0% found this document useful (0 votes)
3 views

osw1

The document outlines Python programs simulating various CPU scheduling algorithms including First Come First Serve, Shortest Job First, Priority Scheduling, and Round Robin. Each program collects process details, calculates waiting and turnaround times, and displays results along with Gantt charts. The document includes sample outputs demonstrating the execution of each algorithm successfully.

Uploaded by

Praveena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

osw1

The document outlines Python programs simulating various CPU scheduling algorithms including First Come First Serve, Shortest Job First, Priority Scheduling, and Round Robin. Each program collects process details, calculates waiting and turnaround times, and displays results along with Gantt charts. The document includes sample outputs demonstrating the execution of each algorithm successfully.

Uploaded by

Praveena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

DATE: EXP NO: PAGE NO:

AIM: To write a python program to simulate page replacement algorithm first come first
serve
PROGRAM:
import matplotlib.pyplot as plt

def fcfs_scheduling():
num_processes = int(input("Enter the number of processes: "))

processes = []
for i in range(num_processes):
arrival_time = int(input(f"Enter arrival time for process {i+1}: "))
burst_time = int(input(f"Enter burst time for process {i+1}: "))
processes.append([arrival_time, burst_time])

# Sort processes based on arrival time


processes.sort()

completion_time = 0
total_waiting_time = 0
total_turnaround_time = 0

print("\nProcess\tArrival Time\tBurst Time\tCompletion Time\tTurnaround


Time\tWaiting Time")
print("-------------------------------------------------------------------------------------")

start_times = [] # To store start times for Gantt chart


for i in range(num_processes):
arrival_time = processes[i][0]
burst_time = processes[i][1]

start_time = max(completion_time, arrival_time)


completion_time = start_time + burst_time
turnaround_time = completion_time - arrival_time
waiting_time = start_time - arrival_time

total_waiting_time += waiting_time
total_turnaround_time += turnaround_time

start_times.append(start_time) # Store start time for Gantt chart

print(f"{i+1}\t\t{arrival_time}\t\t{burst_time}\t\t{completion_time}\t\t{turnaround_time
}\t\t{waiting_time}")
OPERATING SYSTEM LABORATORY CSE DEPARTMENT
DATE: EXP NO: PAGE NO:

average_waiting_time = total_waiting_time / num_processes


average_turnaround_time = total_turnaround_time / num_processes

print(f"\nAverage Waiting Time: {average_waiting_time:.2f}")


print(f"Average Turnaround Time: {average_turnaround_time:.2f}")

# Call the function to draw the Gantt chart


draw_gantt_chart(processes, start_times)

def draw_gantt_chart(processes, start_times):


fig, ax = plt.subplots(figsize=(10, 2))
for i in range(len(processes)):
ax.barh(0, processes[i][1], left=start_times[i], color='skyblue', edgecolor='black')
ax.text(start_times[i] + processes[i][1] / 2, 0, f'P{i+1}', ha='center', va='center',
fontsize=10, fontweight='bold')

ax.set_xlabel("Time")
ax.set_yticks([])
ax.set_xticks([start_times[i] + processes[i][1] for i in range(len(processes))])
ax.set_title("Gantt Chart for FCFS Scheduling")
plt.show()

if __name__ == "__main__":
fcfs_scheduling()
SAMPLE OUTPUT:
Enter the number of processes: 3
Enter arrival time for process 1: 0
Enter burst time for process 1: 5
Enter arrival time for process 2: 0
Enter burst time for process 2: 3
Enter arrival time for process 3: 0
Enter burst time for process 3: 2
Process Arrival Time Burst Time Completion Time Turnaround TimeWaitingTime

1 0 5 5 5 0
2 0 3 8 8 5
3 0 2 10 10 8

Average Waiting Time: 4.333333333333333


Average Turnaround Time: 7.666666666666667

OPERATING SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: PAGE NO:

| P1 | P2 | P3 |

0 5 8 10

EXECUTED OUTPUT:
Enter the number of processes: 3
Enter arrival time for process 1: 0
Enter burst time for process 1: 5
Enter arrival time for process 2: 1
Enter burst time for process 2: 3
Enter arrival time for process 3: 2
Enter burst time for process 3: 2
Process Arrival Time Burst Time Completion Time Turnaround TimeWaiting Time

1 0 5 5 5 0
2 1 3 8 7 4
3 2 2 10 8 6

Average Waiting Time: 3.3333333333333335


Average Turnaround Time: 6.0
Enter the number of processes: 3
Enter arrival time for process 1: 0
Enter burst time for process 1: 5
Enter arrival time for process 2: 1
Enter burst time for process 2: 3
Enter arrival time for process 3: 2
Enter burst time for process 3: 2
Process Arrival Time Burst Time Completion Time Turnaround TimeWaiting Time

1 0 5 5 5 0
2 1 3 8 7 4
3 2 2 10 8 6

Average Waiting Time: 3.3333333333333335


Average Turnaround Time: 6.0

| P1 | P2 | P3 |

0 5 8 10
RESULT: Hence python program to simulate cpu scheduling algorithm first come first
serve executed successfully.

OPERATING SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: PAGE NO:

AIM: To write a python program to simulate the cpu scheduling algorithm Shortes
jobFirst .
PROGRAM:
def main():
at = []
bt = []
wt = []
tat = []
n = int(input("Enter the number of processes: "))

for i in range(n):
arrival_time = int(input(f"Enter Arrival Time for Process {i}: "))
at.append(arrival_time)
burst_time = int(input(f"Enter Burst Time for Process {i}: "))
bt.append(burst_time)

processes = sorted(zip(range(n), at, bt), key=lambda x: x[1])


process_ids, at, bt = zip(*processes)

wt.append(0)
tat.append(bt[0])
wtavg = 0
tatavg = tat[0]
current_time = at[0] + bt[0]

for i in range(1, n):


if current_time < at[i]:
current_time = at[i]
wt.append(current_time - at[i])
tat.append(wt[i] + bt[i])
wtavg += wt[i]
tatavg += tat[i]
current_time += bt[i]

print("\n\tPROCESS\tARRIVAL TIME\tBURST TIME\tWAITING TIME\tTURNAROUND


TIME")
for i in range(n):
print(f"\n\tP{process_ids[i]} \t {at[i]} \t\t {bt[i]} \t\t {wt[i]} \t\t {tat[i]}")

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


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

print("\nGantt Chart:")
OPERATING SYSTEM LABORATORY CSE DEPARTMENT
DATE: EXP NO: PAGE NO:

for i in range(n):
print(f"P{process_ids[i]}", end=" ")
print()

current_time = 0
for i in range(n):
if current_time < at[i]:
current_time = at[i]
current_time += bt[i]
print(f"{current_time}", end=" ")
print()

if __name__ == "__ main__”;


SAMPLE OUTPUT:
Enter the number of processes: 3
Enter Arrival Time for Process 0: 0
Enter Burst Time for Process 0: 5
Enter Arrival Time for Process 1: 0
Enter Burst Time for Process 1: 3
Enter Arrival Time for Process 2: 0
Enter Burst Time for Process 2: 2
PROCESS ARRIVAL TIME BURST TIME WAITING TIME TURNAROUND TIME

P0 0 5 0 5
P1 0 3 5 8
P2 0 2 8 10

Average Waiting Time: 4.33


Average Turnaround Time: 7.67

Gantt Chart:
P2 P1 P0
2 5 10
EXECUTED OUTPUT:
Enter the number of processes: 3
Enter Arrival Time for Process 0: 0
Enter Burst Time for Process 0: 5
Enter Arrival Time for Process 1: 1
Enter Burst Time for Process 1: 3
Enter Arrival Time for Process 2: 2
Enter Burst Time for Process 2: 2
PROCESS ARRIVAL TIME BURST TIME WAITING TIME TURNAROUND TIME

OPERATING SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: PAGE NO:

P0 0 5 0 5
P1 1 3 4 7
P2 2 2 6 8

Average Waiting Time: 3.33


Average Turnaround Time: 6.00
Gantt Chart:
P0 P1 P2
0 5 8 10
RESULT: Hence python program to simulate cpu scheduling algorithm Shortest job First
executed successfully.

OPERATING SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: PAGE NO:

AIM: To write a python program to simulate the cpu scheduling priority algorithm.

Program:
def main():
p = []
at = []
bt = []
pri = []
wt = []
tat = []
n = int(input("Enter the number of processes: "))

for i in range(n):
arrival_time, burst_time, priority = map(
int, input(f"Enter the Arrival Time, Burst Time, & Priority of Process {i + 1}: ").split()
)
p.append(i)
at.append(arrival_time)
bt.append(burst_time)
pri.append(priority)

processes = sorted(zip(p, at, bt, pri), key=lambda x: (x[1], x[3]))


p, at, bt, pri = zip(*processes)

wt.append(0)
tat.append(bt[0])
wtavg = 0
tatavg = tat[0]
current_time = at[0] + bt[0]

for i in range(1, n):


if current_time < at[i]:
current_time = at[i]
wt.append(current_time - at[i])
tat.append(wt[i] + bt[i])
wtavg += wt[i]
tatavg += tat[i]
current_time += bt[i]

print("\nPROCESS\tARRIVAL TIME\tPRIORITY\tBURST TIME\tWAITING


TIME\tTURNAROUND TIME")
for i in range(n):
OPERATING SYSTEM LABORATORY CSE DEPARTMENT
DATE: EXP NO: PAGE NO:

print(f"{p[i] + 1} \t {at[i]} \t\t {pri[i]} \t\t {bt[i]} \t\t {wt[i]} \t\t {tat[i]}")

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


print(f"Average Turnaround Time is --- {tatavg / n:.2f}")

# Gantt Chart
print("\nGantt Chart:")
for i in range(n):
print(f"P{p[i] + 1}", end=" ")
print()

current_time = 0
for i in range(n):
if current_time < at[i]:
current_time = at[i]
current_time += bt[i]
print(f"{current_time}", end=" ")
print()

if __name__ == "__main__":
main()
SAMPLE OUTPUT:
Enter the number of processes: 3
Enter the Arrival Time, Burst Time, & Priority of Process 1: 0 5 2
Enter the Arrival Time, Burst Time, & Priority of Process 2: 0 3 1
Enter the Arrival Time, Burst Time, & Priority of Process 3: 0 2 3
PROCESS ARRIVAL TIME PRIORITY BURST TIME WAITING TIME TURNAROUNDTIME
2 0 1 3 0 3
1 0 2 5 3 8
3 0 3 2 8 10

Average Waiting Time is --- 3.67


Average Turnaround Time is --- 7.00

Gantt Chart:
P2 P1 P3
0 3 8 10
EXECUTED OUTPUT:
Enter the number of processes: 4
Enter the Arrival Time, Burst Time, & Priority of Process 1: 0 4 1
Enter the Arrival Time, Burst Time, & Priority of Process 2: 2 5 4
Enter the Arrival Time, Burst Time, & Priority of Process 3: 1 5 3
Enter the Arrival Time, Burst Time, & Priority of Process 4: 3 2 2
OPERATING SYSTEM LABORATORY CSE DEPARTMENT
DATE: EXP NO: PAGE NO:

PROCESS ARRIVAL TIME PRIORITY BURST TIME WAITING TIME TURNAROUND TIME
1 0 1 4 0 4
4 3 2 2 1 3
3 1 3 5 5 10
2 2 4 5 10 15

Average Waiting Time is --- 4.00


Average Turnaround Time is --- 8.00
Gantt Chart:
P1 P4 P3 P2
0 4 6 11 16
RESULT: Hence python program to simulate cpu scheduling priority algorithm is executed
successfully.

OPERATING SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: PAGE NO:

AIM:To write a python program to simulate the cpu scheduling Round-robin algorithm.
PROGRAM:
def main():
at = []
bu = []
wa = []
tat = []
ct = []
rt = []
n = int(input("Enter the number of processes: "))

for i in range(n):
arrival_time = int(input(f"Enter Arrival Time for process {i + 1}: "))
burst_time = int(input(f"Enter Burst Time for process {i + 1}: "))
at.append(arrival_time)
bu.append(burst_time)
ct.append(burst_time)
rt.append(burst_time)

t = int(input("Enter the size of time slice: "))


queue = []
current_time = 0
completed = 0
gantt_chart = []

while completed < n:


for i in range(n):
if at[i] <= current_time and rt[i] > 0 and i not in queue:
queue.append(i)

if queue:
current = queue.pop(0)
gantt_chart.append((current + 1, current_time))

if rt[current] <= t:
current_time += rt[current]
tat.append(current_time - at[current])
rt[current] = 0
completed += 1
else:
current_time += t
rt[current] -= t
queue.append(current)
OPERATING SYSTEM LABORATORY CSE DEPARTMENT
DATE: EXP NO: PAGE NO:

else:
current_time += 1

for i in range(n):
ct.append(current_time)
tat.append(ct[i] - at[i])
wa.append(tat[i] - bu[i])

att = sum(tat) / n
awt = sum(wa) / n

print(f"\nThe Average Turnaround time is -- {att:.2f}")


print(f"The Average Waiting time is -- {awt:.2f}")
print("\n\tPROCESS\t ARRIVAL TIME \tBURST TIME \t WAITING TIME\t TURNAROUND
TIME")
for i in range(n):
print(f"\t{i + 1} \t {at[i]} \t\t {bu[i]} \t\t {wa[i]} \t\t {tat[i]}")

print("\nGantt Chart:")
for i in range(len(gantt_chart)):
if i == 0:
print(f"0", end=" ")
print(f"| P{gantt_chart[i][0]} ", end="")
print(f"| {current_time}")

if __name__ == "__main__":
main()
SAMPLE OUTPUT:
Enter the number of processes: 3
Enter Arrival Time for process 1: 0
Enter Burst Time for process 1: 6
Enter Arrival Time for process 2: 2
Enter Burst Time for process 2: 8
Enter Arrival Time for process 3: 4
Enter Burst Time for process 3: 5
Enter the size of time slice: 2
PROCES ARRIVAL TIME BURST TIME WAITING TIME TURNAROUND TIME
1 0 6 8 14
2 2 8 9 17
3 4 5 14 19
The Average Turnaround time is -- 16.67
The Average Waiting time is -- 10.33

OPERATING SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: PAGE NO:

| P1 | P2 | P3 | P1 | P2 | P3 | P1 | P2 | P2 | P3 |
0 2 4 6 8 10 12 14 16 18 19
EXECUTED OUTPUT:
Enter the number of processes: 3
Enter Arrival Time for process 1: 0
Enter Burst Time for process 1: 5
Enter Arrival Time for process 2: 0
Enter Burst Time for process 2: 3
Enter Arrival Time for process 3: 0
Enter Burst Time for process 3: 2
Enter the size of time slice: 2
PROCESS ARRIVAL TIME BURST TIME WAITING TIME TURNAROUND TIME
1 0 5 3.00 5.00
2 0 3 3.00 6.00
3 0 2 0.00 2.00
The Average Turnaround time is -- 5.67
The Average Waiting time is -- 3.33
| P1 | P2 | P3 | P1 | P2 | P1 |
0 2 4 6 8 9 10

RESULT: Hence python program to simulate round-robin algorithm executed successfully.

OPERATING SYSTEM LABORATORY CSE DEPARTMENT

You might also like