📜  门| GATE-CS-2016(Set 1)|问题19(1)

📅  最后修改于: 2023-12-03 14:58:30.950000             🧑  作者: Mango

GATE-CS-2016(Set 1) Problem 19

This question pertains to the field of computer science, more specifically to the GATE-CS-2016 (Set 1) exam. Problem 19 is about designing a scheduling algorithm for a processor.

Problem Statement

The problem statement asks to design a CPU scheduling algorithm that minimizes the average waiting time for a set of processes. The algorithm must ensure that each process is given equal CPU time, and that no process is starved. The problem also provides a set of constraints and inputs.

Solution

The solution to this problem involves designing an algorithm that satisfies the given constraints and inputs. One possible algorithm involves using Round Robin Scheduling, which is a pre-emptive algorithm that allocates a fixed time slice to each process in a circular manner. This algorithm satisfies the constraint of equal CPU time allocation, and also ensures that no process is starved.

In order to minimize the average waiting time, the time slice for each process must be carefully chosen. A shorter time slice may result in more context switches and overheads, while a longer time slice may result in process starvation. A common approach is to set the time slice to a multiple of the time required for a context switch.

The solution can be implemented in a programming language such as C++, Java or Python. Here is an example implementation in C++:

#include <iostream>
#include <queue>
using namespace std;

struct process {
    int pid; // process ID
    int arrival_time; // arrival time of the process
    int burst_time; // CPU burst time of the process
    int remaining_time; // remaining CPU burst time of the process
    int waiting_time; // waiting time of the process
};

int main() {
    // Input variables
    int n; // number of processes
    int time_slice; // time slice for each process
    int quantum; // the quantum of time slice for each process
    
    // Input from user
    cout << "Enter number of processes: ";
    cin >> n;
    cout << "Enter time slice for each process: ";
    cin >> time_slice;
    cout << "Enter quantum of time slice for each process: ";
    cin >> quantum;
    
    // Create queue of processes
    queue<process> q;
    
    // Input process details
    for (int i = 0; i < n; i++) {
        process p;
        cout << "Enter arrival time of process " << i+1 << ": ";
        cin >> p.arrival_time;
        cout << "Enter burst time of process " << i+1 << ": ";
        cin >> p.burst_time;
        p.remaining_time = p.burst_time;
        p.pid = i+1;
        p.waiting_time = 0;
        q.push(p);
    }
    
    // Scheduling algorithm
    int current_time = q.front().arrival_time;
    while (!q.empty()) {
        process p = q.front();
        q.pop();
        if (p.remaining_time <= time_slice) {
            current_time += p.remaining_time;
            p.waiting_time = current_time - p.arrival_time - p.burst_time;
            cout << "Process " << p.pid << " completed at time " << current_time << endl;
        } else {
            current_time += time_slice;
            p.remaining_time -= time_slice;
            q.push(p);
        }
        if ((current_time - q.front().arrival_time) >= quantum && !q.empty()) {
            q.push(q.front());
            q.pop();
        }
    }
    
    // Calculate average waiting time
    float avg_waiting_time = 0;
    for (int i = 0; i < n; i++) {
        avg_waiting_time += q.front().waiting_time;
        q.pop();
    }
    avg_waiting_time /= n;
    cout << "Average waiting time: " << avg_waiting_time << endl;
    
    return 0;
}
Conclusion

In conclusion, GATE-CS-2016 (Set 1) Problem 19 is a challenging problem that requires the design of a CPU scheduling algorithm that minimizes the average waiting time for a set of processes, while ensuring that each process is given equal CPU time and that no process is starved. A possible solution involves using Round Robin Scheduling with carefully chosen time slice and quantum values. The solution can be implemented in a programming language such as C++, Java or Python.