-
Notifications
You must be signed in to change notification settings - Fork 0
/
modified_rr.c
116 lines (98 loc) · 3.75 KB
/
modified_rr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
#include <stdlib.h>
#include "scheduler.h"
#include "policy.h"
#define SCHED_NAME "Modified Round Robin Scheduler"
void schedule_task_mrr(tcb_t **ready_queue, tcb_t **running_task, Interval **schedule,
int *numReady, int *round_count, int currentTime, int quantum_number) {
if(*running_task != NULL) {
(*running_task)->params.runTime += 1;
if((*running_task)->params.runTime == (*running_task)->params.burstTime) {
terminate_task((*running_task)->pid);
insertInterval(schedule, (*running_task)->pid, currentTime);
*running_task = NULL;
(*round_count)++;
} else if(*numReady > 0 && (*running_task)->params.runTime%quantum_number == 0) {
suspend_task((*running_task)->pid);
insertInterval(schedule, (*running_task)->pid, currentTime);
*running_task = NULL;
(*round_count)++;
}
}
if(*running_task == NULL && *numReady > 0) {
tcb_t *task = popQueue(ready_queue, numReady);
if(task->params.responseTime == -1) {
task->params.responseTime = currentTime - task->params.arrivalTime;
}
start_task(task->pid);
*running_task = task;
if(*schedule == NULL) insertInterval(schedule, -1, currentTime);
}
if(*running_task == NULL) {
printf("No task running - current time = %d\n", currentTime);
} else {
printf("running process %s - current time = %d \n", (*running_task)->pname, currentTime);
}
}
void mrrScheduler(tcb_t **task_list, int num_tasks, int quantum_number) {
int totalWaitingtime = 0, totalBursttime = 0,
totalResponsetime = 0, currentTime = 0,
contextSwitches = 0, numProcesses = 0, numReady = 0;
int round_count = 0;
int process_left = num_tasks;
tcb_t **ready_queue = calloc(MAX_TASKS, sizeof(tcb_t *));
tcb_t *running_task = NULL;
Interval *schedule = NULL;
printf("\n<---------------------------------->\n");
printf("\t%s\n\n", SCHED_NAME);
do {
for(int i = 0; i < num_tasks; i++) {
if(task_list[i]->state == STATE_WAITING) {
ready_task(i);
insertQueue(ready_queue, task_list[i], &numReady);
task_list[i]->params.waitingTime += 1;
} else if (task_list[i]->state == STATE_INACTIVE &&
task_list[i]->params.arrivalTime == currentTime) {
ready_task(i);
insertQueue(ready_queue, task_list[i], &numReady);
prioritySort(ready_queue, numReady);
++numProcesses;
} else if (task_list[i]->state == STATE_READY) {
task_list[i]->params.waitingTime += 1;
}
}
if (round_count == process_left) {
round_count = 0;
process_left = numReady;
burstSort(ready_queue, numReady);
}
schedule_task_mrr(ready_queue, &running_task, &schedule, &numReady, &round_count,
currentTime, quantum_number);
//delay(500);
++currentTime;
} while(numProcesses != num_tasks || numReady != 0 || running_task != NULL);
for (int i = 0; i < num_tasks; i++) {
totalResponsetime += task_list[i]->params.responseTime;
totalWaitingtime += task_list[i]->params.waitingTime;
totalBursttime += task_list[i]->params.burstTime;
}
Interval *s = schedule;
while(s != NULL) {
++contextSwitches;
s = s->nextInterval;
}
printSchedulingInfo();
printf("Average waiting time = %.4f\n",
((float)totalWaitingtime / (float)num_tasks));
printf("Average response time = %.4f\n",
((float)totalResponsetime / (float)num_tasks));
printf("Average turn around time = %.4f\n",
((float)(totalWaitingtime + totalBursttime) / (float)num_tasks));
printf("Throughput = %.4f\n",
((float)num_tasks) / (float)(totalWaitingtime + totalBursttime));
printf("# of Context Switches = %d\n", contextSwitches > 0 ? contextSwitches - 2 : 0);
printGanttChart(SCHED_NAME, schedule);
printf("\n<---------------------------------->\n");
freeSchedule(&schedule);
free(ready_queue);
}