1.Banker’s Algorithm in Operating System def is_safe(processes, available, max_need, allocated): n = len(processes) work = available.copy() finish = [False] * n safe_sequence = [] while True: found = False for i in range(n): if not finish[i]: can_allocate = True for j in range(len(available)): if max_need[i][j] - allocated[i][j] > work[j]; can_allocate = False break if can_allocate: for j in range(len(available)): work[j] += allocated[i][j] finish[i] = True safe_sequence.append(processes[i]) found = True break if not found: break return all(finish), safe_sequence processes = ['P1', 'P2', 'P3', 'P4', 'P5'] available = [3, 3, 2] max_need = [ [7, 5, 3], [3, 2, 2], [9, 0, 2], [2, 2, 2], [4, 3, 3] ] allocated = [ [0, 1, 0], [2, 0, 0], [3, 0, 2], [2, 1, 1], [0, 0, 2] ] safe, sequence = is_safe(processes, available, max_need, allocated) if safe: print("System is in a safe state.") print("Safe sequence:", sequence) else: print("System is in an unsafe state. Deadlock may occur.") 2. Priority Scheduling function priority_scheduling(processes): sort processes based on priority total_time = 0 waiting_time = 0 for each process in processes: total_time += process.burst_time waiting_time += total_time - process.arrival_time average_waiting_time = waiting_time / number_of_processes return average_waiting_time processes = [ {id: 1, burst_time: 10, priority: 3, arrival_time: 0}, {id: 2, burst_time: 6, priority: 1, arrival_time: 1}, {id: 3, burst_time: 8, priority: 2, arrival_time: 2} ] average_waiting_time = priority_scheduling(processes) print("Average Waiting Time:", average_waiting_time) 3.Round_robin_scheduling function round_robin_scheduling(processes, time_quantum): remaining_time = burst time of each process waiting_time = array of zeros of size number_of_processes current_time = 0 while True: all_processes_completed = True for i = 0 to number_of_processes - 1: if remaining_time[i] > 0: all_processes_completed = False execute process[i] for min(remaining_time[i], time_quantum) current_time += min(remaining_time[i], time_quantum) remaining_time[i] -= min(remaining_time[i], time_quantum) waiting_time[i] = current_time - burst time of process[i] if all_processes_completed: break return average_waiting_time = sum(waiting_time) / number_of_processes 4.Shortest Job First scheduling sudo code function shortest_job_first(processes): sort processes based on burst_time total_time = 0 waiting_time = 0 for each process in processes: total_time += process.burst_time waiting_time += total_time - process.arrival_time return waiting_time / number_of_processes processes = [{id: 1, burst_time: 10, arrival_time: 0}, {id: 2, burst_time: 5, arrival_time: 1}, {id: 3, burst_time: 8, arrival_time: 2}] average_waiting_time = shortest_job_first(processes) print("Average Waiting Time:", average_waiting_time) 5.First_come_first_serve function first_come_first_serve(processes): total_time = 0 waiting_time = 0 for each process in processes: total_time += process.burst_time waiting_time += total_time - process.arrival_time return waiting_time / number_of_processes processes = [{id: 1, burst_time: 10, arrival_time: 0}, {id: 2, burst_time: 5, arrival_time: 1}, {id: 3, burst_time: 8, arrival_time: 2}] average_waiting_time = first_come_first_serve(processes) print("Average Waiting Time:", average_waiting_time)