📜  使用优先队列实现非抢占式最短作业优先

📅  最后修改于: 2021-10-28 02:02:30             🧑  作者: Mango

阅读此处了解相同到达时间的最短作业优先调度算法。
最短作业优先(SJF)或最短作业优先,是一种调度策略,它选择执行时间最短的等待进程进行下一步执行。
在本文中,我们将使用优先级队列实现最短作业优先调度算法(SJF),以便我们可以处理不同到达时间的进程。
例子:

Input:  The processes are
Process id    Arrival time    Burst time    
    p1         4                  3        
    p2         0                  8        
    p3         5                  4        
    p4         9                  2    

Output: Process scheduling according to SJF is
Process id    Arrival time    Burst time    
    p2         0                  8        
    p1         4                  3        
    p4         9                  2        
    p3         5                  4    


Input: The processes are
Process id    Arrival time    Burst time    
    p1         0                  3        
    p2         0                  8        
    p3         5                  4        
    p4         9                  2

Output: Process scheduling according to SJF is
Process id    Arrival time    Burst time    
    p1         0                  3        
    p2         0                  8        
    p4         9                  2        
    p3         5                  4    

在这个程序中,任务是按照SJF调度算法对进程进行调度,该算法规定突发时间最小的进程优先,可以简单地通过将进程的突发时间按升序排序来实现。当我们必须处理不同到达时间的进程时,问题就出现了,那么我们根本无法根据突发时间对进程进行排序,因为我们需要考虑进程的到达时间,以便处理器不会闲置。
例子:
如果突发时间较长的进程在突发时间较短的进程之前到达,那么我们必须让处理器有时间到最先到达的进程,以便处理器不会闲置。
方法:
在 SJF 调度的情况下处理具有不同到达时间的进程:

  • 首先,根据到达时间对流程进行排序。
  • 维护一个等待队列,使进程以最短的突发时间保持在顶部。
  • 维护当前运行时间,即已执行进程的突发时间总和。
    • 如果新进程的到达时间小于
      等于当前运行时间,它被推入等待队列。
    • 进程在执行时从等待队列中弹出。它的突发时间被添加到当前运行时间,它的到达时间被更新为-1,这样它就不会再次进入等待队列。

下面是上述方法的实现:

CPP
// C++ implementation of SJF
#include 
using namespace std;
 
// number of process
#define SIZE 4
 
// Structure to store the
// process information
typedef struct proinfo {
    string pname; // process name
    int atime; // arrival time
    int btime; // burst time
 
} proinfo;
 
// This structure maintains the
// wait queue, using burst
// time to compare.
typedef struct cmpBtime {
    int operator()(const proinfo& a,
                   const proinfo& b)
    {
        return a.btime > b.btime;
    }
} cmpBtime;
 
// This function schedules the
// process according to the SJF
// scheduling algorithm.
void sjfNonpremetive(proinfo* arr)
{
    // Used to sort the processes
    // according to arrival time
    int index = 0;
    for (int i = 0; i < SIZE - 1; i++) {
        index = i;
        for (int j = i + 1; j < SIZE; j++) {
            if (arr[j].atime
                < arr[index].atime) {
                index = j;
            }
        }
        swap(arr[i], arr[index]);
    }
 
    // ctime stores the current run time
    int ctime = arr[0].atime;
 
    // priority queue, wait, is used
    // to store all the processes that
    // arrive <= ctime (current run time)
    // this is a minimum priority queue
    // that arranges values according to
    // the burst time of the processes.
    priority_queue,
                   cmpBtime>
        wait;
 
    int temp = arr[0].atime;
 
    // The first process is
    // pushed in the wait queue.
    wait.push(arr[0]);
    arr[0].atime = -1;
 
    cout << "Process id"
         << "\t";
    cout << "Arrival time"
         << "\t";
    cout << "Burst time"
         << "\t";
 
    cout << endl;
 
    while (!wait.empty()) {
 
        cout << "\t";
        cout << wait.top().pname << "\t\t";
        cout << wait.top().atime << "\t\t";
        cout << wait.top().btime << "\t\t";
        cout << endl;
 
        // ctime is increased with
        // the burst time of the
        // currently executed process.
        ctime += wait.top().btime;
 
        // The executed process is
        // removed from the wait queue.
        wait.pop();
 
        for (int i = 0; i < SIZE; i++) {
            if (arr[i].atime <= ctime
                && arr[i].atime != -1) {
                wait.push(arr[i]);
 
                // When the process once
                // enters the wait queue
                // its arrival time is
                // assigned to -1 so that
                // it doesn't enter again
                // int the wait queue.
                arr[i].atime = -1;
            }
        }
    }
}
 
// Driver Code
int main()
{
    // an array of process info structures.
    proinfo arr[SIZE];
 
    arr[0] = { "p1", 4, 3 };
    arr[1] = { "p2", 0, 8 };
    arr[2] = { "p3", 5, 4 };
    arr[3] = { "p4", 9, 2 };
 
    cout << "Process scheduling ";
    cout << "according to SJF is: \n"
         << endl;
 
    sjfNonpremetive(arr);
}


输出:
Process scheduling according to SJF is: 

Process id    Arrival time    Burst time    
    p2         0                8        
    p1         4                3        
    p4         9                2        
    p3         5                4

时间复杂度: O(N^2)。
辅助空间:O(N)。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。