📜  门| GATE-CS-2002 |问题24(1)

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

GATE-CS-2002 Problem 24

This is a problem from the year 2002 Computer Science Graduate Aptitude Test in Engineering (GATE-CS-2002). The problem statement is as follows:


Consider the following set of processes, with the arrival time and the CPU burst time given in milliseconds:

| Process | Arrival Time | CPU Burst Time | |-|-|-| | P1 | 0 | 2 | | P2 | 1 | 3 | | P3 | 2 | 1 | | P4 | 3 | 2 |

The scheduling algorithm used is preemptive shortest remaining time first (SRTF) with a time quantum of 1 millisecond. The preemption is based on strict priority.

What is the average turnaround time?


To solve this problem, we will use the following steps:

  1. Calculate the waiting time for each process using the SRTF algorithm.
  2. Use the waiting time to calculate the turnaround time for each process.
  3. Use the turnaround time to calculate the average turnaround time.
SRTF Algorithm

SRTF is a preemptive scheduling algorithm where the process with the shortest remaining burst time is scheduled first. If a process with a shorter burst time arrives during the execution of a process, the current process is preempted and the shorter process is scheduled.

To implement the SRTF algorithm, we will use a priority queue to keep track of the remaining time of each process. The process with the shortest remaining time will have the highest priority in the queue.

Code

Here is an implementation of the SRTF algorithm in Python:

from queue import PriorityQueue

def srtf(processes):
    n = len(processes)
    remaining_time = [processes[i][2] for i in range(n)]
    waiting_time = [0 for i in range(n)]
    pq = PriorityQueue()
    current_time = 0
    completed = 0
    pq.put((processes[0][1], 0))
    while completed != n:
        if pq.empty():
            current_time = processes[completed][1]
            pq.put((processes[completed][2], completed))
        else:
            time, process_index = pq.get()
            waiting_time[process_index] += current_time - processes[process_index][1]
            current_time += time
            remaining_time[process_index] = 0
            completed += 1
            while completed < n and processes[completed][1] <= current_time:
                pq.put((processes[completed][2], completed))
                completed += 1
            for i in range(n):
                if remaining_time[i] > 0 and processes[i][1] <= current_time:
                    pq.put((remaining_time[i], i))
                    break
    return waiting_time

The processes parameter is a list of tuples representing each process. Each tuple contains three elements: the process ID, arrival time, and CPU burst time.

The srtf function returns a list of waiting times for each process.

Calculation of waiting time and turnaround time

Using the SRTF algorithm, we can calculate the waiting time for each process. We can then use the waiting time to calculate the turnaround time.

Code

Here is the implementation of the calculation of waiting time and turnaround time in Python:

def calculate_turnaround_time(processes, waiting_time):
    n = len(processes)
    turnaround_time = [0 for i in range(n)]
    for i in range(n):
        turnaround_time[i] = processes[i][2] + waiting_time[i]
    return turnaround_time

processes = [('P1', 0, 2), ('P2', 1, 3), ('P3', 2, 1), ('P4', 3, 2)]
waiting_time = srtf(processes)
turnaround_time = calculate_turnaround_time(processes, waiting_time)

The calculate_turnaround_time function takes the processes list and the waiting_time list as parameters and returns a list of turnaround times for each process.

Average turnaround time

The average turnaround time is the total turnaround time of all processes divided by the number of processes.

Code

Here is the implementation of the calculation of the average turnaround time in Python:

def calculate_average_turnaround_time(turnaround_time):
    n = len(turnaround_time)
    return sum(turnaround_time) / n

average_turnaround_time = calculate_average_turnaround_time(turnaround_time)

The calculate_average_turnaround_time function takes the turnaround_time list as a parameter and returns the average turnaround time.

Conclusion

In this problem, we used the SRTF algorithm to schedule processes and calculated the waiting time and turnaround time for each process. We then used the turnaround time to calculate the average turnaround time.