implemented priority scheduling
This commit is contained in:
parent
4f7893b70e
commit
39b1a9ea22
224
simulator.c
224
simulator.c
@ -8,6 +8,7 @@
|
|||||||
enum pstate {
|
enum pstate {
|
||||||
WAITING,
|
WAITING,
|
||||||
READY,
|
READY,
|
||||||
|
RUNNING,
|
||||||
FINISHED
|
FINISHED
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -18,13 +19,25 @@ struct pinfo {
|
|||||||
int priority;
|
int priority;
|
||||||
|
|
||||||
int wait_time;
|
int wait_time;
|
||||||
int completion_time;
|
int turnaround_time;
|
||||||
int remaining_time;
|
int remaining_time;
|
||||||
|
int n_preempted;
|
||||||
enum pstate state;
|
enum pstate state;
|
||||||
|
|
||||||
struct pinfo * next_pinfo;
|
struct pinfo * next_pinfo;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int n_processes(struct pinfo * head) {
|
||||||
|
int n = 0;
|
||||||
|
|
||||||
|
while (head != NULL) {
|
||||||
|
n++;
|
||||||
|
head = head->next_pinfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
struct perf_info {
|
struct perf_info {
|
||||||
int total_time;
|
int total_time;
|
||||||
int total_nr_ctxt_switch;
|
int total_nr_ctxt_switch;
|
||||||
@ -44,7 +57,7 @@ void print_pinfo(struct pinfo * info) {
|
|||||||
printf(" Execution time: %d\n", info->execution_time);
|
printf(" Execution time: %d\n", info->execution_time);
|
||||||
printf(" Priority: %d\n", info->priority);
|
printf(" Priority: %d\n", info->priority);
|
||||||
printf(" Wait time: %d\n", info->wait_time);
|
printf(" Wait time: %d\n", info->wait_time);
|
||||||
printf(" Completion time: %d\n", info->completion_time);
|
printf(" Turnaround time: %d\n", info->turnaround_time);
|
||||||
printf(" Remaining time: %d\n", info->remaining_time);
|
printf(" Remaining time: %d\n", info->remaining_time);
|
||||||
printf(" NEXT -> %p\n", info->next_pinfo);
|
printf(" NEXT -> %p\n", info->next_pinfo);
|
||||||
printf("}>\n");
|
printf("}>\n");
|
||||||
@ -64,8 +77,9 @@ struct pinfo * create_process(int id, int arrival_time, int execution_time, int
|
|||||||
info->execution_time = execution_time;
|
info->execution_time = execution_time;
|
||||||
info->priority = priority;
|
info->priority = priority;
|
||||||
info->wait_time = 0;
|
info->wait_time = 0;
|
||||||
info->completion_time = 0;
|
info->turnaround_time = 0;
|
||||||
info->remaining_time = execution_time;
|
info->remaining_time = execution_time;
|
||||||
|
info->n_preempted = 0;
|
||||||
info->state = WAITING;
|
info->state = WAITING;
|
||||||
info->next_pinfo = NULL;
|
info->next_pinfo = NULL;
|
||||||
return info;
|
return info;
|
||||||
@ -83,8 +97,8 @@ struct perf_info schedule_FCFS(struct pinfo * processes) {
|
|||||||
wait_time = 0;
|
wait_time = 0;
|
||||||
}
|
}
|
||||||
process->wait_time = wait_time;
|
process->wait_time = wait_time;
|
||||||
process->completion_time = process->execution_time + process->wait_time;
|
process->turnaround_time = process->execution_time + process->wait_time;
|
||||||
current_time = process->arrival_time + process->completion_time;
|
current_time = process->arrival_time + process->turnaround_time;
|
||||||
|
|
||||||
process = process->next_pinfo;
|
process = process->next_pinfo;
|
||||||
}
|
}
|
||||||
@ -93,6 +107,201 @@ struct perf_info schedule_FCFS(struct pinfo * processes) {
|
|||||||
return perf;
|
return perf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct prio_list {
|
||||||
|
struct pinfo ** proc;
|
||||||
|
int length;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pops and returns the last element (highest priority) of the given priority list
|
||||||
|
*
|
||||||
|
* Returns NULL if the list is empty
|
||||||
|
*/
|
||||||
|
struct pinfo * prio_list_pop(struct prio_list * list) {
|
||||||
|
if (list->length == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
list->length--;
|
||||||
|
return *(list->proc + list->length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds the given process in the priority list at
|
||||||
|
* the appropriate place (according to its priority)
|
||||||
|
*/
|
||||||
|
void prio_list_add(struct prio_list * list, struct pinfo * proc) {
|
||||||
|
struct pinfo * proc2;
|
||||||
|
int idx = 0;
|
||||||
|
|
||||||
|
// Find first element (from the right) with lower priority
|
||||||
|
for (int i=list->length-1; i>=0; i--) {
|
||||||
|
proc2 = *(list->proc + i);
|
||||||
|
if (proc2->priority > proc->priority) {
|
||||||
|
idx = i+1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Shift elements
|
||||||
|
for (int j=list->length-1; j>=idx; j--) {
|
||||||
|
*(list->proc + j + 1) = *(list->proc + j);
|
||||||
|
}
|
||||||
|
*(list->proc + idx) = proc;
|
||||||
|
list->length++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new priority list
|
||||||
|
*/
|
||||||
|
struct prio_list * create_prio_list(int max_size) {
|
||||||
|
struct pinfo ** processes = (struct pinfo **) malloc(sizeof(struct pinfo *) * max_size);
|
||||||
|
struct prio_list * list = (struct prio_list *) malloc(sizeof(struct prio_list));
|
||||||
|
list->length = 0;
|
||||||
|
list->proc = processes;
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the last element (highest priority) of the given priority list
|
||||||
|
*/
|
||||||
|
struct pinfo * prio_list_last(struct prio_list * list) {
|
||||||
|
return *(list->proc + list->length - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints the pids of the processes in the given priority list
|
||||||
|
*/
|
||||||
|
void prio_list_print(struct prio_list * list) {
|
||||||
|
printf("queue: ");
|
||||||
|
for (int i=0; i < list->length; i++) {
|
||||||
|
if (i != 0) {
|
||||||
|
printf(", ");
|
||||||
|
}
|
||||||
|
printf("%d", (*(list->proc + i))->id);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
struct perf_info schedule_Pr(struct pinfo * processes) {
|
||||||
|
struct perf_info perf = {0, 0, 0};
|
||||||
|
|
||||||
|
int current_time = 0;
|
||||||
|
struct pinfo * current = NULL;
|
||||||
|
struct pinfo * next = processes;
|
||||||
|
|
||||||
|
int N = n_processes(processes);
|
||||||
|
printf("N = %d\n", N);
|
||||||
|
|
||||||
|
struct prio_list * queue = create_prio_list(N);
|
||||||
|
|
||||||
|
int finished = 0;
|
||||||
|
|
||||||
|
while (finished != N) {
|
||||||
|
printf("\nCurrent time: %d / ", current_time);
|
||||||
|
if (current != NULL) {
|
||||||
|
printf("Current: %d / ", current->id);
|
||||||
|
} else {
|
||||||
|
printf("Current: none / ");
|
||||||
|
}
|
||||||
|
if (next != NULL) {
|
||||||
|
printf("Next: %d\n", next->id);
|
||||||
|
} else {
|
||||||
|
printf("Next: none\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current == NULL) {
|
||||||
|
printf("No running process: running %d\n", next->id);
|
||||||
|
current = next;
|
||||||
|
current->state = RUNNING;
|
||||||
|
next = next->next_pinfo;
|
||||||
|
|
||||||
|
} else if (next != NULL) {
|
||||||
|
next->state = READY;
|
||||||
|
prio_list_print(queue);
|
||||||
|
printf("Processing next process (%d)\n", next->id);
|
||||||
|
|
||||||
|
// If current finished before next
|
||||||
|
while (current != NULL && current_time + current->remaining_time <= next->arrival_time) {
|
||||||
|
printf(" Process %d finished before next\n", current->id);
|
||||||
|
current->state = FINISHED;
|
||||||
|
current_time += current->remaining_time;
|
||||||
|
current->turnaround_time = current_time - current->arrival_time;
|
||||||
|
current->remaining_time = 0;
|
||||||
|
finished++;
|
||||||
|
current = prio_list_pop(queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current != NULL) {
|
||||||
|
printf("Removing time from current process\n");
|
||||||
|
current->remaining_time -= next->arrival_time - current_time;
|
||||||
|
current->turnaround_time += next->arrival_time - current_time;
|
||||||
|
}
|
||||||
|
if (next->arrival_time > current_time) {
|
||||||
|
current_time = next->arrival_time;
|
||||||
|
}
|
||||||
|
next->state = READY;
|
||||||
|
|
||||||
|
// If no running process, immediately run next process
|
||||||
|
if (current == NULL) {
|
||||||
|
printf("Queue is empty, running next process %d\n", next->id);
|
||||||
|
current = next;
|
||||||
|
current->state = RUNNING;
|
||||||
|
next = next->next_pinfo;
|
||||||
|
|
||||||
|
} else if (next->priority < current->priority) {
|
||||||
|
// Preempt current process
|
||||||
|
printf("Next process (%d) has higher priority\n", next->id);
|
||||||
|
if (current->state == RUNNING) {
|
||||||
|
printf(" Preempting current process (%d)\n", current->id);
|
||||||
|
current->n_preempted++;
|
||||||
|
current_time += CNTXT_SWITCH;
|
||||||
|
perf.total_nr_ctxt_switch++;
|
||||||
|
}
|
||||||
|
current->state = READY;
|
||||||
|
prio_list_add(queue, current);
|
||||||
|
|
||||||
|
// Run process with higher priority
|
||||||
|
current = next;
|
||||||
|
current->state = RUNNING;
|
||||||
|
next = next->next_pinfo;
|
||||||
|
} else {
|
||||||
|
printf("Adding next process (%d) to list\n", next->id);
|
||||||
|
prio_list_add(queue, next);
|
||||||
|
next = next->next_pinfo;
|
||||||
|
}
|
||||||
|
if (current != NULL) {
|
||||||
|
current->state = RUNNING;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} else {
|
||||||
|
printf("No new processes, emptying queue\n");
|
||||||
|
while (current != NULL) {
|
||||||
|
printf("Completing process %d\n", current->id);
|
||||||
|
current->state = FINISHED;
|
||||||
|
current_time += current->remaining_time;
|
||||||
|
current->turnaround_time = current_time - current->arrival_time;
|
||||||
|
current->remaining_time = 0;
|
||||||
|
finished++;
|
||||||
|
current = prio_list_pop(queue);
|
||||||
|
if (current != NULL) {
|
||||||
|
current->state = RUNNING;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
perf.total_time = current_time;
|
||||||
|
perf.total_time_ctxt_switch = perf.total_nr_ctxt_switch * CNTXT_SWITCH;
|
||||||
|
|
||||||
|
return perf;
|
||||||
|
}
|
||||||
|
|
||||||
|
void compute_waiting_time(struct pinfo * processes) {
|
||||||
|
while (processes != NULL) {
|
||||||
|
processes->wait_time = processes->turnaround_time - processes->execution_time;
|
||||||
|
processes = processes->next_pinfo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct pinfo * read_file() {
|
struct pinfo * read_file() {
|
||||||
FILE * file = fopen("tasks.csv", "r");
|
FILE * file = fopen("tasks.csv", "r");
|
||||||
@ -154,7 +363,10 @@ void free_processes(struct pinfo * next) {
|
|||||||
int main() {
|
int main() {
|
||||||
struct pinfo * processes = read_file();
|
struct pinfo * processes = read_file();
|
||||||
|
|
||||||
struct perf_info perf = schedule_FCFS(processes);
|
//struct perf_info perf = schedule_FCFS(processes);
|
||||||
|
struct perf_info perf = schedule_Pr(processes);
|
||||||
|
|
||||||
|
compute_waiting_time(processes);
|
||||||
|
|
||||||
//write_file(processes, &perf);
|
//write_file(processes, &perf);
|
||||||
print_processes(processes);
|
print_processes(processes);
|
||||||
|
Loading…
Reference in New Issue
Block a user