Untitled
unknown
c_cpp
4 years ago
3.4 kB
8
Indexable
queue **createQueues(task *table, int highest)
{
int i;
queue **queues = (queue **)malloc(sizeof(queue *) * (highest + 1));
for (i = 0; i < highest + 1; i++)
{
queues[i] = (queue *)malloc(sizeof(queue));
queues[i]->tasks = (task *)malloc(sizeof(task) * actualTaskAmount(table));
queues[i]->size = actualTaskAmount(table);
queues[i]->front = 0;
queues[i]->rear = 0;
}
return queues;
}
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)
{
for (i = highest; i >= 0; i--)
{
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];
queues[i]->rear++;
// remove task
queues[i]->rear--;
for (j = 0; j < queues[i]->rear; j++)
{
queues[i]->tasks[j] = queues[i]->tasks[j + 1];
}
tempQuan = quantum;
flag = 0;
}
}
}
runFlag = 1;
for (i = 0; i < highest + 1; i++)
{
if (queues[i]->rear != 0)
{
runFlag = 0;
}
}
}
}
Editor is loading...