📜  门| GATE-CS-2017(Set 2)|第59章(1)

📅  最后修改于: 2023-12-03 15:28:45.297000             🧑  作者: Mango

GATE-CS-2017 (Set 2) Question 59

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.

Question Description

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.

Solution
Algorithm Explanation

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.

Understanding the Question

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.

Solution Steps
  1. Create a sorted list of processes based on their arrival time.
  2. Initialize a variable current_time to 0.
  3. Initialize an empty list completed_processes.
  4. Initialize a dictionary remaining_time with process IDs as keys and their burst times as values.
  5. Repeat until all processes are completed:
    1. Get the process with the shortest remaining burst time from the remaining_time dictionary.
    2. If its arrival time is greater than the current_time, update current_time to the process's arrival time.
    3. Append a context switch to the completed_processes list.
    4. Remove the process from the remaining_time dictionary.
    5. Repeat until the next shortest remaining burst time process is found or the process is completed:
      1. Increment current_time.
      2. Decrement the remaining burst time of the current process.
      3. If the remaining burst time of the process is 0, append the process to the completed_processes list and break the loop.
      4. If a new process arrives while the current process is executing, update remaining_time dictionary and break the loop.
  6. Finally, count the number of context switches in the completed_processes list.
Code Implementation
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
Results

Using the provided input, the function srtf returns 2 as the number of context switches required when the SRTF scheduling algorithm is implemented.

Conclusion

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.