先进的CPU调度程序模拟turbo c ++

时间:2017-02-11 04:20:40

标签: c++ algorithm borland-c++

我是turbo c ++的新手,我无法理解这里的代码块以及它们如何协同工作以执行抢占式CPU调度。这段代码正常工作我在一些网站上发现了这段代码,我想知道的是每个代码块的描述。请帮助我提高我对turbo c ++的了解。谢谢。

   #include<stdio.h>
struct sjf
{
       int bt,at,wt,st,pno,tt,cbt;
};

int get(struct sjf arr[],int t,int n)
{
       int imin,min=9999,i;
       for(i=0;i<n;i++)
       {
              if(arr[i].at<=t&&arr[i].st==0)
                     if(min>arr[i].bt)
                     {
                           min=arr[i].bt;
                           imin=i;
                     }
       }
       return imin;
}

void gantt_chart(struct sjf arr[],int p[],int n,int nop)
{
       int i,a[100],s=0;
       float avgtt=0,avgwt=0;
       printf("***************************************\n");
       printf("GANTT CHART\n");
       printf("0");
       for(i=0;i<n-1;i++)
       {
              while(i<n-1&&p[i]==p[i+1])
              {
                     s++;
                     i++;
              }

              s++;
              printf(" -> [P%d] <- %d",arr[p[i]].pno,s);
              arr[p[i]].wt=s-arr[p[i]].at-arr[p[i]].tt;
       }
       for(i=0;i<nop;i++)
       {
              arr[i].tt+=arr[i].wt;
              avgwt+=arr[i].wt;
              avgtt+=arr[i].tt;
       }
       printf("\n***************************************\n");
       printf("Pro\tArTi\tBuTi\tTaTi\tWtTi\n");
       printf("***************************************\n");
       for(i=0;i<nop;i++)
       {
              printf("[P%d]\t%d\t%d\t%d\t%d\n",arr[i].pno,arr[i].at,arr[i].cbt,arr[i].tt,arr[i].wt);
       }
       printf("***************************************\n");
       avgwt = avgwt/nop;
       avgtt = avgtt/nop;
       printf("Average Waiting Time : %.2f\n",avgwt);
       printf("Average Turnaround Time : %.2f\n",avgtt);
       return;
}

int iscomplite(struct sjf arr[],int n)
{
       int i;
       for(i=0;i<n;i++)
              if(arr[i].st==0)
                     return 0;
       return 1;
}
int main()
{
       int n,i,a,t=0;
       int p[100];
       float avgwt=0,avgtt=0;
       struct sjf arr[100];
       printf("SJF (Shortest Job First) - Preemptive\n");
       printf("Note -\n1. Arrival Time of at least on process should be 0\n2. CPU should never be idle\n");
       printf("Enter Number of Processes\n");
       scanf("%d",&n);
       for(i=0;i<n;i++)
       {
              printf("Enter Arrival Time & Burst Time for Process [P%d]\n",i);
              scanf("%d%d",&arr[i].at,&arr[i].bt);
              arr[i].pno = i;
              arr[i].cbt = arr[i].bt;
              arr[i].st=0;
              arr[i].tt=arr[i].bt;
              arr[i].wt=0;
       }
       i=0;
       while(1)
       {
              if(iscomplite(arr,n))
                     break;
              a=get(arr,t,n);
              p[i]=a;
              arr[a].bt-=1;
              if(arr[a].bt==0)
                     arr[a].st=1;
              t=t+1;
              i++;
       }
       gantt_chart(arr,p,i,n);
       return 0;
}

/*
SJF (Shortest Job First) - Preemptive
Note -
1. Arrival Time of at least on process should be 0
2. CPU should never be idle
Enter Number of Processes
4
Enter Arrival Time & Burst Time for Process [P0]
0 8
Enter Arrival Time & Burst Time for Process [P1]
1 4
Enter Arrival Time & Burst Time for Process [P2]
2 9
Enter Arrival Time & Burst Time for Process [P3]
3 5
***************************************
GANTT CHART
0 -> [P0] <- 1 -> [P1] <- 5 -> [P3] <- 10 -> [P0] <- 17 -> [P2] <- 26
***************************************
Pro     ArTi    BuTi    TaTi    WtTi
***************************************
[P0]    0       8       17      9
[P1]    1       4       4       0
[P2]    2       9       24      15
[P3]    3       5       7       2
***************************************
Average Waiting Time : 6.50
Average Turnaround Time : 13.00

*/

0 个答案:

没有答案