osw1
osw1
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])
completion_time = 0
total_waiting_time = 0
total_turnaround_time = 0
total_waiting_time += waiting_time
total_turnaround_time += turnaround_time
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:
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
| 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
1 0 5 5 5 0
2 1 3 8 7 4
3 2 2 10 8 6
| P1 | P2 | P3 |
0 5 8 10
RESULT: Hence python program to simulate cpu scheduling algorithm first come first
serve executed successfully.
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)
wt.append(0)
tat.append(bt[0])
wtavg = 0
tatavg = tat[0]
current_time = at[0] + bt[0]
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()
P0 0 5 0 5
P1 0 3 5 8
P2 0 2 8 10
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
P0 0 5 0 5
P1 1 3 4 7
P2 2 2 6 8
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)
wt.append(0)
tat.append(bt[0])
wtavg = 0
tatavg = tat[0]
current_time = at[0] + bt[0]
print(f"{p[i] + 1} \t {at[i]} \t\t {pri[i]} \t\t {bt[i]} \t\t {wt[i]} \t\t {tat[i]}")
# 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
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
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)
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("\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
| 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