cround-robin

How to print a table of timeline for Round-Robin scheduling problem


My problem requires that all information of each process be printed out based on the given quantum time intervals. Whichever process is being processed is placed at the top, otherwise it is placed at the bottom.If the process has finished processing, it will not be printed anymore.

My Input:

char  name[50][10] = {"A","B","C","D","E"}; // process names
int  btime[] = {9,7,8,5,3}; // process burst times
int n = 5;

Expected Output:

Round robin scheduling with time quantum = 2
0: (A,9,9) (B,7,7) (C,8,8) (D,5,5) (E,3,3)
2: (B,7,7) (C,8,8) (D,5,5) (E,3,3) (A,9,7)
4: (C,8,8) (D,5,5) (E,3,3) (A,9,7) (B,7,5)
6: (D,5,5) (E,3,3) (A,9,7) (B,7,5) (C,8,6)
8: (E,3,3) (A,9,7) (B,7,5) (C,8,6) (D,5,3)
10: (A,9,7) (B,7,5) (C,8,6) (D,5,3) (E,3,1)
12: (B,7,5) (C.8.6) (D.5.3) (E,3,1) (A,9,5)
14: (C,8,6) (D,5,3) (E,3,1) (A,9,5) (B,7,3)
16: (D,5,3) (E,3,1) (A,9,5) (B.7,3) (C,8,4)
18: (E,3,1) (A,9,5) (B,7,3) (C,8,4) (D,5,1)
19: (A,9,5) (B,7,3) (C,8,4) (D,5,1)
21: (B,7,3) (C,8,4) (D,5,1) (A,9,3)
23: (C,8,4) (D,5,1) (A,9,3) (B,7,1)
25: (D,5,1) (A,9,3) (B,7,1) (C,8,2)
26: (A,9,3) (B,7,1) (C,8,2)
28: (B.7.1) (C,8,2) (A,9,1)
29: (C,8,2) (A,9,1)
31: (A,9,1)

My Code:

#include <stdio.h>

int main()
{
    char name[50][10] = {"A", "B", "C", "D", "E"}; // process names
    int btime[] = {9, 7, 8, 5, 3};                // process burst times
    int n = sizeof btime / sizeof btime[0];
    int quantum = 2;
    int wt[n], i;
    int rem_bt[n];
    for (i = 0; i < n; i++)
        rem_bt[i] = btime[i];

    printf("Round robin scheduling with time quantum = %d\n", quantum);
    int t = 0;
    while (1)
    {
        int done = 1;
        for (i = 0; i < n; i++)
        {
            if (rem_bt[i] > 0)
            {
                done = 0;
                if (rem_bt[i] > quantum)
                {
                    printf("%d: (%s,%d,%d) \n", t, name[i], btime[i], rem_bt[i]);
                    rem_bt[i] -= quantum;
                    t += quantum;
                }
                else
                {
                    printf("%d: (%s,%d,%d) \n", t, name[i], btime[i], rem_bt[i]);
                    t += rem_bt[i];
                    wt[i] = t - btime[i] - rem_bt[i];
                    rem_bt[i] = 0;
                }
            }
        }
        if (done == 1)
        {
            break;
        }
        printf("\n");
    }

    return 0;
}

I'm stuck at step that print all the timeline, I can only print out information about running processes, but cannot print out information about other stopped processes.

My output:

0: (A,9,9) 
2: (B,7,7) 
4: (C,8,8)
6: (D,5,5)
8: (E,3,3)
10: (A,9,7)
12: (B,7,5) 
14: (C,8,6) 
16: (D,5,3) 
18: (E,3,1)
19: (A,9,5)
21: (B,7,3) 
23: (C,8,4)
25: (D,5,1) 
26: (A,9,3) 
28: (B.7.1) 
29: (C,8,2) 
31: (A,9,1)

I would like to know your suggestion, thank you very much!


Solution

  • For starters, you are always printing the current clock and a newline character for every non-exhausted process. You need to separate this information into different prints, only printing the clock and a newline once for every set of processes.

    Additionally, you are always iterating from the start of the array to the end of the array, meaning your list of processes will always start with the lowest-indexed, non-exhausted process, instead of the next non-exhausted process.


    One approach would be to use the remainder operator (%) to clamp indexes to an appropriate range, and in turn, to manage the current global position in the array. This lets you cycle through the positions in the array; meaning you can start in the middle, iterate to the end, wrap back around to the start of the array, and iterate back to the middle by separately keeping track of the total number of iterations.

    A basic algorithm would be that, at every processing point:

    A more aggressive algorithm might shift the array elements over exhausted ones, or use a different data structure that can be manipulated more freely (e.g., using two linked lists for pending and finished processes).


    Some extra advice would be to represent your processes as an array of structures, instead of three separate arrays of fragmented data. It may also be easier to think about this problem in terms of unsigned types, since time is (almost) always advancing. In the example below we deal with elapsed time, instead of remaining time.


    A fairly complete example:

    #include <stdio.h>
    
    struct entry {
        const char *name;
        const unsigned target_time;
        unsigned elapsed_time;
    };
    
    static void print_entry(const struct entry *e)
    {
        printf(" (%s,%u,%u)", e->name, e->target_time, e->target_time - e->elapsed_time);
    }
    
    int main(void)
    {
        struct entry entries[] = {
            { "A", 9, 0 }, { "B", 7, 0 }, { "C", 8, 0 }, { "D", 5, 0 }, { "E", 3, 0 }
        };
        const size_t n_entries = sizeof entries / sizeof *entries;
        const unsigned processing_time = 2;
    
        unsigned tick = 0;
        size_t index = 0;
    
        printf("Round robin scheduling with time quantum = %u\n", processing_time);
    
        while (1) {
            size_t n = n_entries;
            struct entry *entry;
    
            /* find next */
            do {
                entry = entries + index;
                index = (index + 1) % n_entries;
            } while (entry->elapsed_time >= entry->target_time && --n);
    
            /* none found, we're done */
            if (!n)
                break;
    
            printf("%u:", tick);
            /* print entry before it is processed */
            print_entry(entry);
    
            /* process time slice */
            entry->elapsed_time += processing_time;
    
            /* clamp elapsed time, and adjust tick */
            if (entry->elapsed_time > entry->target_time) {
                tick += entry->elapsed_time - entry->target_time;
                entry->elapsed_time = entry->target_time;
            } else {
                tick += processing_time;
            }
    
            /* print any remaining processes */
            for (size_t i = index; --n; i = (i + 1) % n_entries) {
                entry = entries + i;
    
                if (entry->elapsed_time < entry->target_time)
                    print_entry(entry);
            }
    
            putchar('\n');
        }
    }
    

    Output:

    Round robin scheduling with time quantum = 2
    0: (A,9,9) (B,7,7) (C,8,8) (D,5,5) (E,3,3)
    2: (B,7,7) (C,8,8) (D,5,5) (E,3,3) (A,9,7)
    4: (C,8,8) (D,5,5) (E,3,3) (A,9,7) (B,7,5)
    6: (D,5,5) (E,3,3) (A,9,7) (B,7,5) (C,8,6)
    8: (E,3,3) (A,9,7) (B,7,5) (C,8,6) (D,5,3)
    10: (A,9,7) (B,7,5) (C,8,6) (D,5,3) (E,3,1)
    12: (B,7,5) (C,8,6) (D,5,3) (E,3,1) (A,9,5)
    14: (C,8,6) (D,5,3) (E,3,1) (A,9,5) (B,7,3)
    16: (D,5,3) (E,3,1) (A,9,5) (B,7,3) (C,8,4)
    18: (E,3,1) (A,9,5) (B,7,3) (C,8,4) (D,5,1)
    19: (A,9,5) (B,7,3) (C,8,4) (D,5,1)
    21: (B,7,3) (C,8,4) (D,5,1) (A,9,3)
    23: (C,8,4) (D,5,1) (A,9,3) (B,7,1)
    25: (D,5,1) (A,9,3) (B,7,1) (C,8,2)
    26: (A,9,3) (B,7,1) (C,8,2)
    28: (B,7,1) (C,8,2) (A,9,1)
    29: (C,8,2) (A,9,1)
    31: (A,9,1)