循环调度计划

时间:2013-02-16 17:02:47

标签: c scheduling round-robin

我一直致力于循环调度计划。我的意见是:

Process     Arrival Time    Burst Time
   1            0               4
   2            2               2
   3            4               3
   4            6               5
   5            7               1

时间片是3个单位! 我的输出必须是:

Process     AT      BT      WT      TT      FT
   1        0       4       9       13      13
   2        2       2       1       3       5
   3        4       3       1       4       8
   4        6       5       4       9       15
   5        7       1       4       5       12

但是我没有得到过程1,4和&的正确结果(WT& FT)。 5.这是我的代码,任何人都可以帮我修复并获得上述结果吗?

#include<stdio.h>
#include<conio.h>
struct proc
{
    int id;
    int arrival;
    int burst;
    int rem;
    int wait;
    int finish;
    int ti;
    int turnaround;
    float ratio;
}process[10];

int no,k;
int chkprocess(int);

void main()
{
 int i,j,t,time = 0,n;
 struct proc temp;
 int nextprocess(int);
 clrscr();
 printf("\n \n Enter the number of processes: ");
 scanf("%d", &n);
 printf("\n \n Enter the time slice of the CPU: ");
 scanf("%d", &t);

 for(i = 1; i <= n; i++)
 {
  process[i].id = i;
  printf("\n\nEnter the arrival time for process %d: ", i);
  scanf("%d", &(process[i].arrival));
  printf("\nEnter the burst time for process %d: ", i);
  scanf("%d", &(process[i].burst));
  process[i].rem = process[i].burst;
  process[i].ti=0;
  process[i].wait=0;
  process[i].finish=0;
 }

 for(i = 1; i <= n; i++)
 {
  for(j = i + 1; j <= n; j++)
  {
   if(process[i].arrival > process[j].arrival)
   {
    temp = process[i];
    process[i] = process[j];
    process[j] = temp;
   }
  }
 }

 no = 0;
 j = 1;

 while(chkprocess(n) == 1)
 {
  if(process[no + 1].arrival == time)
   no++;
  if((process[j].ti<=t)&&(process[j].rem !=0))
  {
   process[j].rem--;
   process[j].ti++;
   for(i = 1; i <= no; i++)
   {
    if((i!=j) && (process[i].rem != 0))
     process[i].wait++;
   }
  }
  if(process[j].rem==0)
   process[j].finish=time;
  if((process[j].ti >= t)||(process[j].rem==0))
  {
   process[j].ti = 0;
   j=nextprocess(j);
  }
  time++;
 }
 process[n].finish = time;
 printf("\n\n Process  Arrival  Burst   Waiting  Finishing turnaround  Tr/Tb \n");
 printf("%5s %9s %7s %10s %8s %9s\n\n", "id", "time", "time", "time", "time", "time");
 for(i = 1; i <= n; i++)
 {
  process[i].turnaround = process[i].wait + process[i].burst;
  process[i].ratio = (float)process[i].turnaround / (float)process[i].burst;
  printf("%5d %8d %7d  %8d %10d %9d %10.1f ", process[i].id, process[i].arrival,
                          process[i].burst,
                          process[i].wait, process[i].finish,
                          process[i].turnaround, process[i].ratio);

  printf("\n\n");
 }
 getch();
}

int chkprocess(int s)
{
 int i;
 for(i = 1; i <= s; i++)
 {
  if(process[i].rem != 0)
   return 1;
 }
 return 0;
}

int nextprocess(int k)
{
 int i;
 i=k+1;
 while(chkprocess(i) && i!=k)
 {
  if(process[i].rem != 0)
   return i;
  else
   i=(i+1)%no;
 }
}

谢谢

4 个答案:

答案 0 :(得分:4)

我确信存在许多错误(如果用户想要输入11个或更多进程,即使您的进程数组限制为10个,也可以不关心。)

然而;我花了10分钟试图破译你的代码,但仍然不知道它认为它在做什么 - 根本没有评论,变量名和函数名没有帮助(例如no不是布尔值“是/否“变量,checkprocess()不检查一个进程,但检查所有进程以查看是否所有进程都已完成,等等)。大多数情况下,如果我得到报酬修复此代码,我会简单地将其丢弃并从头开始重写以节省时间。我想过从头开始重写它,只是发布结果代码;但这不会帮助你完成你的作业。

我的建议是,从头开始重写,而不是修复它。

它应该有一个全局currently_running_process变量,一个全局current_time变量,一个用于增加当前时间的函数,以及一个用于调度程序本身的函数。

增加当前时间的功能是:

  • 对于调度程序链表上的每个进程,增加等待时间
  • 执行current_time++
  • 找到应该启动的任何进程(current_time == arrival_time)并将任何已启动的进程附加到调度程序链接列表的末尾

调度程序功能应该:

  • 从调度程序的链接列表中删除第一个进程
  • 确定该进程应使用多长时间(时间片长度或进程'剩余时间,以较低者为准)
  • 从流程的剩余时间
  • 中减去该时间量
  • 循环调用increase_time()函数,直到经过了这段时间
  • 如果过程'剩余时间不为零;将流程重新放回链表的末尾
  • 如果进程剩余时间为零,检查调度程序的链表是否为空,如果是
  • 则退出程序

注意:我从current_time = -1;开始并在调用调度程序函数之前调用该函数以增加当前时间;这样在调度程序开始工作之前,任何带有arrival_time == 0的进程都会被添加到调度程序的链表中(并且调度程序函数一旦启动就不会看到空列表)。

答案 1 :(得分:2)

/* The following code doesn't take the arrival time of the processes in account.
                              HAPPY CODING */
#include<stdio.h>
void main()
{
int b[10],br[10],wo[10];
int n,i,bt,q,count;
float awt=0,att=0;
for (i=0;i<10;i++)
     wo[i]=0;
printf("Input the nmbr of processes running....");
scanf("%d",&n);
printf("\n Input their burst tym in order..");
for(i=0;i<n;i++)
    scanf("%d",&b[i]);
printf("\n Input the quantum time for the algorithm..");
scanf("%d",&q);
for(i=0;i<n;i++)
    br[i]=b[i];
bt=0;
for(i=0;i<n;i++)
    bt=bt+b[i];
count=0;
printf("\nThe Gantt Chart is as follows:\n");
printf("\n 0");
do
{
for(i=0;i<n;i++)
{
  if(br[i]==0)
   {}
  else
  {
   if(br[i]>=q)
   {
     br[i]=br[i]-q;
     if(br[i]==0)
        wo[i]=count;
     count=count+q;
     printf("\t(P%d)",i);
     printf("\t%d",count);
   }
   else
   {
     if(br[i]<q)
    {
       count=count+br[i];
       br[i]=0;
       wo[i]=count;
       printf("\t(P%d)",i);
       printf("\t%d",count);
     }
   }
 }
}
}while(count<bt);
for(i=0;i<n;i++)
    awt=awt+(wo[i]-b[i]);
awt=awt/n;
printf("\n The average waiting time is....%f",awt);
for(i=0;i<n;i++)
    att=att+wo[i];
att=att/n;
printf("\n The average turnaround time is....%f",att);
}

答案 2 :(得分:1)

你可以使用队列做同样的事情,我粘贴一个用ANSI CPP编写的链接 您可以查看此链接以获取更多信息。我遇到了和你一样的问题,但链接上的代码对我帮助很大,它还包含许多其他调度程序,但我只提取了循环法。 click here to see the code for round robin Scheduling

答案 3 :(得分:0)

C

中的循环调度程序
#include<stdio.h>
#include<conio.h>

main(){

    int i, j, k, n, so, tq, sob, sum, swt, stat, tata, temp, count;
    int bt[10], bth[10], wt[10], tat[10];
    float awt=0.0, atat=0.0;
    char new;

    // i = loop controller
    // j = loop controller
    // k = loop controller
    // n = number of process
    // so = (burst time holder divided by time quantum) and added by one
    // tq = time quantum
    // awt =average waiting time
    // new = hold the value of start command
    // sob = gantt chart size from so
    // swt = summation of waiting time              l
    // bt[] = burst time
    // wt[] = waiting time
    // atat = average turn around time
    // gcps = gantt chart process sequence
    // stat = summation of turn around time
    // tata = accumulator of turn around time
    // temp = time quantum holder
    // count = counter
    // bth[] = burst time holder
    // tat[] = turn around time



    printf("\n\n\n\n   To start round robin scheduling press any key: ");

    k = 0;
    new = getche();
    system("cls");

    while(k < 7){

        j = 0; sob = 0; count = 0; sum = 0; swt = 0; stat = 0; tata = 0;

        printf("\n\n\n\t\t\t      ROUND-ROBIN SCHEDULING");
        printf("\n\t\t\t      ======================");
        printf("\n\n\n\n\n   Enter number of processes: ");
        scanf("%d", &n);
        printf("\n");

        for(i = 0; i < n; i++){

            printf("\n   Enter burst time for Process P%d: ", i+1);
            scanf("%d", &bt[i]);
            bth[i] = bt[i];
        }

        printf("\n\n   Enter time quantum: ");
        scanf("%d", &tq);
        system("cls");
        printf("\n\n\n\t\t\t      ROUND-ROBIN SCHEDULING");
        printf("\n\t\t\t      ======================");
        printf("\n\n\n\n\n   Time quantum: %d", tq);

        for(i = 0; i < n; i++){

            if(bth[i] % tq == 0){

                so = bth[i] / tq;
            }
            else{so = (bth[i] / tq) +1;}
            sob = sob + so;
        }

        int gc[sob], gcps[sob];

        while(1){

            for(i = 0,count = 0; i < n; i++){

                temp = tq;
                if(bth[i] == 0){

                    count++;
                    continue;
                }

                if(bth[i] > tq){

                    gc[j] = tq;
                    gcps[j] = i+1; j++;
                    bth[i] = bth[i] - tq;
                }

                else if(bth[i] >= 0){

                    if(bth[i] == tq){gc[j] = tq; gcps[j] = i+1; j++;}
                    else{gc[j] = bth[i]; gcps[j] = i+1; j++;}
                    temp = bth[i];
                    bth[i] = 0;
                }

                tata = tata + temp;
                tat[i ]= tata;
            }

            if(n==count){

                break;
            }
        }

        for(i = 0; i < n; i++){

            wt[i] = tat[i] - bt[i];
            swt = swt + wt[i];
            stat = stat + tat[i];
        }

        awt = (float)swt/n;
        atat = (float)stat/n;

        printf("\n\n   Process   Burst time   Waiting time   Turn around time\n");
        printf("   -------   ----------   ------------   ----------------\n");

        for(i = 0; i < n; i++){

            printf("\n\n      P%d\t %d\t       %d \t        %d", i+1, bt[i], wt[i], tat[i]);
        }

        printf("\n\n\n\n   Gantt Chart:\n");
        printf("   ------------\n\n");
        for(j = 0; j < sob; j++){

            printf("\tP%d", gcps[j]);
        }
        printf("\n   0");
        for(j = 0; j < sob; j++){

            sum = sum + gc[j];
            if(j == 0){printf("        %d", sum);}
            else{printf("\t    %d", sum);}
        }
        printf("\n\n\n\n   Average waiting time: %.2f \n\n   Average turn around time: %.2f",awt,atat);
        printf("\n\n\n\n   To start again press S and to exit press any key: ");

        new = getche();
        system("cls");

        if(new == 'S'|| new == 's'){k++;}
        else{printf("\n\n\n   Program was terminated successfully\n\n   Thank you\n\n\n"); break;}

    }

}
相关问题