当前位置: 首页 > 知识库问答 >
问题:

SJF 非抢占式调度算法

夹谷成仁
2023-03-14

我对这些调度算法很熟悉。我已经习惯了SJF的非抢占性,我从纸笔甘特图的角度理解它,但从编程的角度却不太理解。我的代码在下面,虽然它运行成功,但我的数学不正确。我该如何修复它?

测试案例过程突发到达P1 20 0 P2 3 3 P3 2 5

预期输出 平均等待时间为 11.3 平均周转时间为 19.6 平均响应时间为 10.6

实际输出平均等待时间为 8.3 平均周转时间为 6.6 平均响应时间为 14

    #include <iostream>
    using namespace std;

    void SJF_NP(int n, int burst[], int arrival[], int throughput)
    {
      cout << "Output for SJF_Non_Preemptive scheduling algorithm" << endl;

      int i, j, temp, temp2;
      double tot, avgwait, avgturnaround, avgresponse, tp;

      //array instantiations                                                                                          
      int start[n], end[n], wait[n];

      //calculations                                                                                                  
      for(i=1;i<=n;i++)
        {
          for(j=i+1;j<=n;j++)
            {
              if (i>=2 && burst[i-1]>burst[j-1])
                {
                  temp = burst[i-1];
                  temp2 = arrival[i-1];
                  burst[i-1]=burst[j-1];
                  arrival[i-1]=arrival[j-1];
                  burst[j-1]=temp;
                  arrival[j-1]=temp2;
                }
            }
          if(i==1)
            {
              start[0]=0;
              end[0]=burst[0];
              wait[0]=0;
            }
          else
            {
              start[i-1]=end[i-2];
              end[i-1]=start[i-1]+burst[i-1];
              wait[i-1]=start[i-1]+arrival[i-1];
            }
          //throughput                                                                                                
          if (start[i+1] <= throughput)
            tp = i+1;
        }

      //output                                                                                                        
      cout << "\n\nPROCESS \t BURST TIME\tARRIVAL TIME\tWAIT TIME\tSTART TIME\tEND TIME\n";
      for (i=0;i<n;i++){
        cout << "\nP[" << i + 1 << "]" << "\t\t" << burst[i] << "\t\t" << arrival[i] << "\t\t" << wait[i] << "\t\t" << start[i] << "\t\t" << end[i];
      }
      //avg wait time                                                                                                 
      for(i=1,tot=0;i<n;i++){
        tot+=wait[i-1];
        avgwait=tot/n;
      }
      //avg turnaround time                                                                                           
      for(i=1,tot=0;i<n;i++){
        tot+=end[i-1];
        avgturnaround=tot/n;
      }
      //avg response time                                                                                             
      for(i=1,tot=0;i<n;i++){
        tot+=start[i-1];
        avgresponse=tot/n;
      }
      cout << "\n\nAverage Wait Time: " << avgwait;
      cout << "\nAverage Response Time: " << avgturnaround;
      cout << "\nAverage Turnaround Time: " << avgresponse;
      cout << "\nThroughput for (" << throughput << "): " << tp << endl;
    }

共有1个答案

詹夕
2023-03-14
#include<iostream>
using namespace std;
struct Process
{
    int pid,at,bt,wt,tt,ct; //at=arrival time;bt=burst time
                                 //wt=waiting time; tt=turnaround time
                                //ct=completion time
};

//to swap processes
void swap(struct Process *t ,struct Process *w )
{
struct Process v;
v=*t;
*t=*w;
*w=v;
}
 //sort according to arrival time
 void sort1(struct Process *t,int p)
{
int i,j;
struct Process *q,s;

for(i=0;i<p;i++,t++)
{
for(j=i+1,q=t+1;j<p;j++,q++)
{
 if((t->at)>(q->at))
 {
 s=*t;
 *t=*q;
 *q=s;
 }
 }
}
}

//sort according to burst time
void sort2(struct Process *t,int p)
{
int i,j;
struct Process *q,s;
if(t->pid==p)
{
    return;
}
for(i=1;i<p;i++,t++)
{
for(j=i+1,q=t+1;j<p;j++,q++)
{
 if((t->bt)>(q->bt))
 {
 s=*t;
 *t=*q;
 *q=s;
 }
 }
}
}

int main()
{
    int n;
    struct Process p[80];
    int at,bt;
    cout<<"Enter number of processes";
    cin>>n;
    for (int i=0;i<n;i++)
      {
        cout<<"for process"<<i+1<<"\n";
        cout<<"pid";
        cin>>p[i].pid;
        cout<<"at";
        cin>>p[i].at;
        cout<<"bt";
        cin>>p[i].bt;
      } 


    sort1(p,n);
    p[0].wt=0;
    p[0].ct=p[0].bt;
    p[0].tt=p[0].ct;



    cout<<"ProcessId\t"<<"Arrival time\t"<<"Burst time\t";
    for(int i=0;i<n;i++)
    {
        cout<<p[i].pid<<"\t\t"<<p[i].at<<"\t\t"<<p[i].bt<<"\n";
    }

    int t=1;
    do{

    int diff=p[t].at-p[t-1].ct;
    if(diff<=0)
    {
      sort2(&p[t],n);   
      for( int i=t+1;i<n;i++)
      {
        if(p[t].bt==p[i].bt&&p[i].at<p[t].at)
        {
            swap(&p[t],&p[i]);
            break;
        }

      }

      p[t].ct=p[t-1].ct+p[t].bt;
      p[t].tt=p[t].ct-p[t].at;
      p[t].wt=p[t].tt-p[t].bt;
    }

    else
    {
        p[t].ct=p[t].at+p[t].bt;
        p[t].tt=p[t].ct-p[t].at;
        p[t].wt=0;
        sort2(&p[t+1],n);
    }
    t++;

    }   
    while(t < n);
    //Output
    cout<<"ProcessId\t"<<"Arrival time\t"<<"Burst time\t"<<"Waiting Time\t"      
    <<"Turnaround Time\n";
    for(int i=0;i<n;i++)
    {
        cout<<p[i].pid<<"\t\t"<<p[i].at<<"\t\t"<<p[i].bt<<"\t\t"
        <<p[i].wt<<"\t\t"<<p[i].tt<<"\n";
    }

    return 0;
}
 类似资料:
  • 我拿到这张桌子是为了抢先做最短工作 在G之前,它执行前有2秒,我需要包括它吗? 我在回答中用甘特图给出的表格是 我的问题是,是否可以包括F到达之前的等待时间?

  • 在非先占优先级调度中,进程根据分配给它们的优先级编号进行调度。 一旦进程被安排好了,它就会运行直到完成。 通常,优先级数越低,进程的优先级越高。 人们可能会对优先级数字感到困惑,因此在GATE中,明确提到哪一个是最高优先级,哪一个是最低优先级。 示例 在例子中,有7个进程:,,,,,和。 它们的优先级,到达时间和爆发时间在表中给出。 进程ID 优先级 到达时间 爆发时间 1 2 0 3 2 6 2

  • goroutine本来是设计为协程形式,但是随着调度器的实现越来越成熟,Go在1.2版中开始引入比较初级的抢占式调度。 从一个bug说起 Go在设计之初并没考虑将goroutine设计成抢占式的。用户负责让各个goroutine交互合作完成任务。一个goroutine只有在涉及到加锁,读写通道或者主动让出CPU等操作时才会触发切换。 垃圾回收器是需要stop the world的。如果垃圾回收器想

  • SJF =最短的工作第一,标题不会让我适合它 抢占式SJF调度是否会使进程的平均等待时间大于在非抢占式SJF调度算法中简单执行的进程?毕竟,您不断地切换上下文并迫使进程等待更长时间才能完成。 我似乎不明白为什么是先发制人的SJF(又名。最短剩余时间优先,或STRF)优于非抢占式SJF(就进程的平均等待时间而言)。 有人能给我解释一下吗? 非常感谢。

  • 嗨,伙计们。我们被分配了一个关于抢占优先调度的任务,我真的不知道如何做到这一点,因为两个或多个进程具有相同的优先级编号。 我必须做一个甘特图,计算周转时间和平均等待时间。 如果可能的话,你们能否发布一个关于如何做到这一点的分步解决方案,以便我可以研究它是如何完成的。 谢谢你们的帮助。

  • 最短作业优先算法如下图所示: 如果首先是最短的作业/其次是最短的流程,那么顺序不应该是:P1 → P5 → P3 → P4 → P2吗?因为这是服务时间从低到高的顺序。< br >为什么进程2排在第二位? 我知道如果我们改用突发时间,那将是顺序,但我不知道服务时间和突发时间有什么区别。 任何有助于解释该图形的帮助都将不胜感激。