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

📅  最后修改于: 2021-05-04 12:47:42             🧑  作者: 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,这样它就不会再次进入等待队列。

下面是上述方法的实现:

// 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