📅  最后修改于: 2023-12-03 15:28:45.297000             🧑  作者: Mango
This is a question from the GATE-CS-2017 (Set 2) exam paper, which tests the knowledge of programmers in the area of operating systems.
Consider three CPU-intensive processes, which require 10, 20 and 30 time units and arrive at times 0, 2 and 6, respectively. How many context switches are needed if the operating system implements a shortest remaining time first scheduling algorithm? Do not count the context switches in the initial time before the first process arrives.
Shortest remaining time first (SRTF) algorithm is a preemptive version of the SJF algorithm. In this algorithm, the process that has the shortest remaining burst time is given the highest priority. It is a preemptive algorithm because at any point of time, if a new job has a shorter remaining time than that of the job currently being executed, the current job gets preempted.
The question provides us with the arrival time and the burst time of three CPU-intensive processes, and we need to find out the number of context switches required if the SRTF scheduling algorithm is implemented. We do not need to count the context switches in the initial time before the first process arrives.
current_time
to 0.completed_processes
.remaining_time
with process IDs as keys and their burst times as values.remaining_time
dictionary.current_time
, update current_time
to the process's arrival time.completed_processes
list.remaining_time
dictionary.current_time
.completed_processes
list and break the loop.remaining_time
dictionary and break the loop.completed_processes
list.def srtf(process_list):
sorted_processes = sorted(process_list, key=lambda x: x[0])
current_time = sorted_processes[0][0]
completed_processes = []
remaining_time = {i[2]: i[1] for i in sorted_processes}
while remaining_time:
shortest_process = min(remaining_time, key=lambda x: remaining_time[x])
if remaining_time[shortest_process] + current_time > sorted_processes[-1][0]:
current_time = sorted_processes[-1][0]
if shortest_process != completed_processes[-1:]:
completed_processes.append('context switch')
current_time += 1
remaining_time[shortest_process] -= 1
if remaining_time[shortest_process] == 0:
completed_processes.append(shortest_process)
del remaining_time[shortest_process]
for process in sorted_processes:
if process[0] <= current_time and process[2] not in remaining_time:
remaining_time[process[2]] = process[1]
break
num_context_switches = completed_processes.count('context switch')
return num_context_switches
Using the provided input, the function srtf
returns 2 as the number of context switches required when the SRTF scheduling algorithm is implemented.
The SRTF scheduling algorithm is useful in minimizing the average waiting time for CPU-intensive processes. By preempting processes with shorter remaining burst times, the algorithm ensures that the higher priority processes are executed first. The implementation of the algorithm requires keeping track of the arrival time and burst time of each process, and dynamically updating the schedules based on the remaining time of the processes.