简体   繁体   English

循环调度程序

[英]Round Robin Scheduling Program

I have been working on a Round Robin Scheduling Program. 我一直在研究循环调度程序。 My inputs are: 我的输入是:

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

Time Slice is 3 units! 时间片是3个单位! My output must be: 我的输出必须是:

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

But I am not getting the correct results(WT & FT) for Process 1, 4 & 5. Here's my code, can anyone please help me fix it and get the above results? 但是我没有获得过程1、4和5的正确结果(WT和FT)。这是我的代码,有人可以帮助我修复它并获得上述结果吗?

#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;
 }
}

Thank You 谢谢

I'm sure there are many bugs (starting with not caring if the user wants to enter 11 or more processes even though your array of processes is limited to 10). 我确定有很多错误(即使您的进程数组限制为10个,如果用户想输入11个或更多进程,从不关心也可以开始)。

However; 然而; I spent 10 minutes trying to decipher your code and still don't really know what it thinks it's doing - there's no comments at all and the variable names and function names don't help (eg no is not a boolean "yes/no" variable, checkprocess() doesn't check one process but checks all processes to see if all processes have finished, etc). 我花了10分钟的时间试图破译您的代码,但仍然不十分了解它的含义-根本没有注释,变量名和函数名也无济于事(例如, no不是布尔值“是/否”变量, checkprocess()不会检查一个进程,而是检查所有进程以查看是否所有进程都已完成,等等)。 Mostly, if I were being paid to fix this code I'd simple throw it out and rewrite it from scratch to save time. 通常,如果需要付费以修复此代码,我会简单地将其丢弃并从头开始重写以节省时间。 I thought about rewriting it from scratch and just posting the resulting code; 我考虑过从头开始重写它,然后发布结果代码。 but that's not going to help you with your homework. 但这不会帮助您完成功课。

My advice is, rewrite it from scratch instead of fixing it. 我的建议是,从头开始重写它,而不要修复它。

It should have a global currently_running_process variable, a global current_time variable, one function to increase the current time, and one function for the scheduler itself. 它应该有一个全球性的currently_running_process变量,全局current_time变量,一个函数来增加电流的时间,并为调度本身的一个功能。

The function to increase the current time would: 增加当前时间的功能是:

  • for each process on the scheduler's linked list, increase the waiting time 对于调度程序的链表上的每个进程,增加等待时间
  • do current_time++ current_time++
  • find any processes that should be started ( current_time == arrival_time ) and append any started processes to the end of the scheduler's linked list 查找应启动的任何进程( current_time == arrival_time ),并将任何已启动的进程附加到调度程序的链表的末尾

The scheduler function should: 调度程序功能应:

  • remove the first process from the scheduler's linked list 从调度程序的链接列表中删除第一个进程
  • determine how much time that process should use (the time slice length or the process' remaining time, whichever is lower) 确定该流程应使用多少时间(时间片长度或流程的剩余时间,以较低者为准)
  • subtract that amount of time from the process' remaining time 从进程的剩余时间中减去该时间
  • call the increase_time() function in a loop, until that amount of time has passed 循环调用increase_time()函数,直到经过该时间量
  • if the process' remaining time is not zero; 如果进程的剩余时间不为零; put the process back onto the end of the linked list 将流程放回链接列表的末尾
  • if the process` remaining time was zero, check if the scheduler's linked list is empty and exit the program if it is 如果进程的剩余时间为零,请检查调度程序的链表是否为空,如果是,则退出程序

Note: I'd start with current_time = -1; 注意:我将从current_time = -1;开始current_time = -1; and call the function to increase the current time once before calling the scheduler function; 并在调用调度程序函数之前调用该函数以增加当前时间一次; so that any processes with arrival_time == 0 would be added to the scheduler's linked list before the scheduler starts working (and so that the scheduler function doesn't see an empty list as soon as it's started). 因此,在到达调度程序开始工作之前,会将带有arrival_time == 0所有进程添加到调度程序的链接列表中(这样,调度程序功能就不会在启动后立即看到空列表)。

/* 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);
}

You can use queue for doing the same, i am pasting a link which is written in ANSI CPP You can check this link for more info. 您可以使用队列执行相同的操作,我正在粘贴以ANSI CPP编写的链接。您可以检查此链接以获取更多信息。 I was having same problem like you had but the code on the link helped me a lot it also contains many other Scheduling program but i extracted only round robin from it. 我遇到了和您一样的问题,但是链接上的代码对我有很大帮助,它还包含许多其他计划程序,但我仅从其中提取了循环代码。 click here to see the code for round robin Scheduling 单击此处查看循环调度的代码

Round Robin Scheduling program in C 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;}

    }

}

This code will read data from file whose format should have one process info in a single line, arrival time , burst time , spaced, and file should terminate with -1. 这段代码将从文件中读取数据,该文件的格式应在一行中包含一个过程信息, 到达时间突发时间 ,间隔和文件应以-1结尾。 File name and time slice must be passed in command arguments. 文件名和时间片必须在命令参数中传递。 Like: 喜欢:

0 3
1 2
2 1
-1

The code is in C and variable names are self-descriptive. 代码用C语言编写,变量名称是自描述的。

#include<stdio.h>

int main(int argc, char *argv[])
{
int flag = 0;

int timeSlice = atoi(argv[2]);

printf("%d\n\n", timeSlice);

int arrivalTime[10], burstTime[10], responseTime[10], finishTime[10];
int remainingProcesses, processCount = 0;

FILE * file = fopen(argv[1], "r");

if (!(file == NULL))
{
    while (fscanf(file, "%d", &arrivalTime[processCount]))
    {
        if (arrivalTime[processCount] == -1)
            break;

        fscanf(file, "%d", &burstTime[processCount]);

        responseTime[processCount] = burstTime[processCount];

        processCount++;
    }

    remainingProcesses = processCount;

    fclose(file);
}

printf("Process\t|  Arrival time\t|  Finish Time\t|     Burst\t|   Turnaround\t|\n");
printf("-------------------------------------------------------------------------\n");

int i = 0; int time = 0;

while (remainingProcesses != 0)
{
    if (responseTime[i] <= timeSlice && responseTime[i]>0)
    {
        time += responseTime[i];
        responseTime[i] = 0;
        flag = 1;
    }
    else if (responseTime[i] > 0)
    {
        responseTime[i] -= timeSlice;
        time += timeSlice;
    }

    if (responseTime[i] == 0 && flag == 1)
    {
        finishTime[i] = time;
        remainingProcesses--;

        printf("P[%d]\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\n", i + 1, arrivalTime[i], finishTime[i], burstTime[i], finishTime[i] - arrivalTime[i]);
        flag = 0;
    }


    if (i == processCount - 1) // If its the last process go back to slicing process 1
    {
        i = 0;
    }

    else if (arrivalTime[i + 1] <= time) // If the next process has kicked in
    {
        i++;
    }

    else // If the process haven't kicked in yet
    {
        time++;
        i = 0;
    }
}

return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM