Round Robin Scheduling Issue

So I am writing a simple Round Robin Scheduler with a time slice of 1.

Here is the input (first number is arrival time, second is burst time)

0 3
2 6
4 4
6 5
8 2

And here is the expected output (cpu is what is being executed, ready queue is what is waiting. A letter of the alphabet is used to indicate what process is being run and the number after it is the remaining time):

time   cpu   ready queue (pid/rst)
----   ---   ---------------------
  0    A3    --
  1    A2    --
  2    B6    A1
  3    A1    B5
  4    B5    C4
  5    C4    B4
  6    B4    D5, C3
  7    D5    C3, B3
  8    C3    B3, E2, D4
  9    B3    E2, D4, C2
 10    E2    D4, C2, B2
 11    D4    C2, B2, E1
 12    C2    B2, E1, D3
 13    B2    E1, D3, C1
 14    E1    D3, C1, B1
 15    D3    C1, B1
 16    C1    B1, D2
 17    B1    D2
 18    D2    --
 19    D1    --

However I am having an error with the ready queue. Arrivals should be added to the rear of the ready queue before preemptions (see how D5 is added to the ready queue ahead of C3)

This is what I am getting:

time    cpu ready_queue (pid/rst)
----    --- ---------------------
  0     A3  --
  1     A2  --
  2     B6  --
  3     A1  --
  4     B5  --
  5     C4  --
  6     D5  --
  7     B4  --
  8     C3  --
  9     D4  --
  10    E2  --
  11    B3  --
  12    C2  --
  13    D3  --
  14    E1  --
  15    B2  --
  16    C1  --
  17    D2  --
  18    B1  --
  19    D1  --

Here is my code:

#include <stdio.h>
#include <stdlib.h>

#define TS 1

struct process
{
    char pid;
    int arrival_time;
    int service_time;
    int remaining_time;
    int departure_time;
    int turnaround_time;
    int wait_time;
};

struct process processes[26];
char pid_rst[2];
char alphabet[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
int num_processes = 0;
int total_time;

char* create_id(char c, int i)
{
    sprintf(pid_rst, "%c%d", c, i);

    return pid_rst;
}

void get_processes() 
{
    int a_t;
    int s_t;
    int i = 0;

    while((scanf("%d %d", &a_t, &s_t) == 2))
    {
        processes[i].pid = alphabet[i];
        processes[i].arrival_time = a_t;
        processes[i].service_time = s_t;
        processes[i].remaining_time = processes[i].service_time;

        i++;
        num_processes++;
        total_time += processes[i].service_time;
    }
}

int main()
{
    int time;
    int count;
    int remain;
    int flag = 0;
    int i;
    int w;

    get_processes();

    char* cpu_queue[1];
    char* wait_queue[num_processes];
    remain = num_processes;

    for(i = 0; i < num_processes; i++)
    {
        wait_queue[i] = "--";
    }

    printf("ntimetcputready_queue (pid/rst)n");
    printf("----t---t---------------------n");

    for(time = 0, i = 0; remain != 0;) 
    {
        w = 1;
        if(processes[i].remaining_time <= TS && processes[i].remaining_time > 0)
        {
            printf("  %dt%st%sn", time, create_id(processes[i].pid, processes[i].remaining_time), wait_queue[w-1]);
            time += processes[i].remaining_time;
            processes[i].remaining_time = 0;
            flag = 1;   
        }
        else if (processes[i].remaining_time > 0)
        {
            printf("  %dt%st%sn", time, create_id(processes[i].pid, processes[i].remaining_time), wait_queue[w-1]);
            processes[i].remaining_time -= TS;
            time += TS;
        }
        if(processes[i].remaining_time == 0 && flag == 1)
        {
            remain--;
            processes[i].turnaround_time += time - processes[i].arrival_time;
            processes[i].wait_time += time - processes[i].arrival_time - processes[i].service_time;
            processes[i].departure_time = time;
            flag = 0;
        }
        if(i == num_processes-1)
        {
            i = 0;
        }
        else if(processes[i+1].arrival_time <= time)
        {
            wait_queue[w] = create_id(processes[i].pid, processes[i].remaining_time);
            if(w < 4)
                w++;
            i++;

        }
        else
        {
            i = 0;
        }
    }

    return 0;
}
链接地址: http://www.djcxy.com/p/84836.html

上一篇: 多级反馈队列调度如何工作?

下一篇: 轮询调度问题