Os Codes
Os Codes
FCFS(CPU)
CODE
def calculate_waiting_time(n, bt, wt):
wt[0] = 0
for i in range(1, n):
wt[i] = bt[i - 1] + wt[i - 1]
if __name__ == "__main__":
n = int(input("Enter the number of processes: "))
burst_time = []
find_average_time(n, burst_time)
2. SJF
CODE
# Initialize waiting time and turnaround time arrays
waiting_time = [0] * n
turnaround_time = [0] * n
print(f"{processes[i][0]}\t{processes[i][1]}\t\t{processes[i][2]}\t\t{waiting_time[i]}\t\t{turnaround_tim
e[i]}")
def main():
# Taking user input for number of processes
n = int(input("Enter the number of processes: "))
processes = []
for i in range(n):
process_id = input(f"Enter Process ID for process {i + 1}: ")
burst_time = int(input(f"Enter Burst Time for process {i + 1}: "))
arrival_time = int(input(f"Enter Arrival Time for process {i + 1}: "))
processes.append((process_id, burst_time, arrival_time))
sjf_scheduling(processes)
if __name__ == "__main__":
main()
3. ROUND
CODE
while True:
done = True
for i in range(n):
if remaining_burst_time[i] > 0:
done = False
if remaining_burst_time[i] > time_quantum:
total_time += time_quantum
remaining_burst_time[i] -= time_quantum
else:
total_time += remaining_burst_time[i]
waiting_time[i] = total_time - processes[i][1]
remaining_burst_time[i] = 0
if done:
break
for i in range(n):
turnaround_time[i] = waiting_time[i] + processes[i][1]
avg_waiting_time = sum(waiting_time) / n
avg_turnaround_time = sum(turnaround_time) / n
def main():
# Taking user input for number of processes
n = int(input("Enter the number of processes: "))
processes = []
for i in range(n):
process_id = input(f"Enter Process ID for process {i + 1}: ")
burst_time = int(input(f"Enter Burst Time for process {i + 1}: "))
processes.append((process_id, burst_time))
round_robin_scheduling(processes, quantum)
if __name__ == "__main__":
main()
4. BANKER
ccode
while True:
found = False
for i in range(n):
if not finish[i] and all(need[i][j] <= work[j] for j in range(m)):
safe_sequence.append(i)
finish[i] = True
work = [work[j] + alloc[i][j] for j in range(m)]
found = True
break
if not found:
break
def main():
num_processes = int(input("Enter the number of processes: "))
num_resources = int(input("Enter the number of resources: "))
print("\nNeed Matrix:")
for i in range(num_processes):
need = [max_resources[i][j] - allocation[i][j] for j in range(num_resources)]
print(" ".join(map(str, need)))
if safe_sequence:
print("\nSafe sequence:", safe_sequence)
else:
print("\nNo safe sequence exists!")
if __name__ == "__main__":
main()
5. FIFO
ccode
return page_faults
def main():
# Taking user input for number of frames
num_frames = int(input("Enter the number of frames: "))
if __name__ == "__main__":
main()
6. OPT
ccode
return page_faults
def main():
# Taking user input for number of frames
num_frames = int(input("Enter the number of frames: "))
return page_faults
def main():
# Taking user input for number of frames
num_frames = int(input("Enter the number of frames: "))
if __name__ == "__main__":
main()
8. FCFS(I/O)
Code
return total_movement
def main():
# Taking user input for initial position of disk head
initial_position = int(input("Enter the initial position of the disk head: "))
if __name__ == "__main__":
main()
9. SSCAN
code
while True:
if direction == "left":
nearest_request = max([req for req in requests if req <= current_position] + [0])
if nearest_request == 0:
total_movement += current_position
current_position = 0
direction = "right"
else:
total_movement += abs(nearest_request - current_position)
current_position = nearest_request
sequence.append(current_position)
requests.remove(current_position)
elif direction == "right":
nearest_request = min([req for req in requests if req >= current_position] + [total_tracks])
if nearest_request == total_tracks:
total_movement += total_tracks - current_position
current_position = total_tracks
direction = "left"
else:
total_movement += abs(nearest_request - current_position)
current_position = nearest_request
sequence.append(current_position)
requests.remove(current_position)
if not requests:
break
def main():
# Taking user input for initial position of disk head
initial_position = int(input("Enter the initial position of the disk head: "))
if __name__ == "__main__":
main()
10. SSIF
Code
while requests:
nearest_request = min(requests, key=lambda x: abs(x - current_position))
total_movement += abs(nearest_request - current_position)
current_position = nearest_request
sequence.append(current_position)
requests.remove(current_position)
def main():
# Taking user input for initial position of disk head
initial_position = int(input("Enter the initial position of the disk head: "))
if __name__ == "__main__":
main()
11. FIRST
Code
for i in range(len(process_sizes)):
for j in range(len(memory_blocks)):
if memory_blocks[j] >= process_sizes[i]:
allocation[i] = j
memory_blocks[j] -= process_sizes[i]
break
return allocation
def main():
# Taking user input for the number of memory blocks and their sizes
num_blocks = int(input("Enter the number of memory blocks: "))
memory_sizes = list(map(int, input("Enter the sizes of memory blocks separated by spaces:
").split()))
# Taking user input for the number of processes and their sizes
num_processes = int(input("Enter the number of processes: "))
process_sizes = list(map(int, input("Enter the sizes of processes separated by spaces:
").split()))
if __name__ == "__main__":
main()
12. BEST
Code
for i in range(len(process_sizes)):
best_idx = -1
for j in range(len(memory_blocks)):
if memory_blocks[j] >= process_sizes[i]:
if best_idx == -1 or memory_blocks[j] < memory_blocks[best_idx]:
best_idx = j
if best_idx != -1:
allocation[i] = best_idx
memory_blocks[best_idx] -= process_sizes[i]
return allocation
def main():
# Taking user input for the number of memory blocks and their sizes
num_blocks = int(input("Enter the number of memory blocks: "))
memory_sizes = list(map(int, input("Enter the sizes of memory blocks separated by spaces:
").split()))
# Taking user input for the number of processes and their sizes
num_processes = int(input("Enter the number of processes: "))
process_sizes = list(map(int, input("Enter the sizes of processes separated by spaces:
").split()))
if __name__ == "__main__":
main()
13. WORST
Code
for i in range(len(process_sizes)):
worst_idx = -1
for j in range(len(memory_blocks)):
if memory_blocks[j] >= process_sizes[i]:
if worst_idx == -1 or memory_blocks[j] > memory_blocks[worst_idx]:
worst_idx = j
if worst_idx != -1:
allocation[i] = worst_idx
memory_blocks[worst_idx] -= process_sizes[i]
return allocation
def main():
# Taking user input for the number of memory blocks and their sizes
num_blocks = int(input("Enter the number of memory blocks: "))
memory_sizes = list(map(int, input("Enter the sizes of memory blocks separated by spaces:
").split()))
# Taking user input for the number of processes and their sizes
num_processes = int(input("Enter the number of processes: "))
process_sizes = list(map(int, input("Enter the sizes of processes separated by spaces:
").split()))