Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.0 kB
2
Indexable
Never
void prioritywithroundrobin(task* table, int quantum)
{
    printf("------------------- ROUND ROBIN WITH PRIORITY ------------------- \n");
    int i, j;
    int timeCounter = 0;
    int highest = highestPriority(table);
    int taskAmount = actualTaskAmount(table);
    queue** queues = createQueues(table, highest);

    for (i = 0; i < taskAmount; i++)
    {
        if (table[i].arrival == timeCounter)
        {
            queues[table[i].priority]->tasks[queues[table[i].priority]->rear] = table[i];
            queues[table[i].priority]->rear++;
        }
    }
    int tempQuan = quantum;
    int runFlag = 0;
    int flag = 0;
    while (runFlag == 0)
    {
        l1:
        for (i = highest; i >= 0; i--)
        {
            tempQuan = quantum;
            while (queues[i]->rear != 0)
            {
                // while tempQuan is not 0
                // reduce task burst by 1, reduce tempQuan by 1, increment timeCounter by 1
                while (tempQuan != 0)
                {
                    printf("Running <P%d,%d>\n", queues[i]->tasks[0].id, queues[i]->tasks[0].burst);
                    queues[i]->tasks[0].burst--;
                    timeCounter++;
                    if (queues[i]->tasks[0].burst == 0)
                    {
                        queues[i]->rear--;
                        for (j = 0; j < queues[i]->rear; j++)
                        {
                            queues[i]->tasks[j] = queues[i]->tasks[j + 1];
                        }
                        flag = 1;
                    }

                    // Check if any task had arrived and add it to proper queue
                    for (j = 0; j < taskAmount; j++)
                    {
                        if (table[j].arrival == timeCounter)
                        {
                            queues[table[j].priority]->tasks[queues[table[j].priority]->rear] = table[j];
                            queues[table[j].priority]->rear++;
                        }
                    }
                    tempQuan--;
                }
                // if flag is 1, then tempQuan is 0 and we need to move task from front of queue to end of queue
                if (!flag)
                {
                    queues[i]->tasks[queues[i]->rear] = queues[i]->tasks[0];
                    for (j = 0; j < queues[i]->rear; j++)
                    {
                        queues[i]->tasks[j] = queues[i]->tasks[j + 1];
                    }
                    flag = 0;
                    goto l1;
                }
                else
                    goto l1;
            }
        }
        for (i = 0; i < highest + 1; i++)
        {
            if (queues[i]->rear != 0)
            {
                runFlag = 0;
                break;
            }
            else
            {
                runFlag = 1;
            }
        }

    }
}