implemented SRTF
This commit is contained in:
parent
aee7447c21
commit
6cc65667de
10
executionSRTF.csv
Normal file
10
executionSRTF.csv
Normal file
@ -0,0 +1,10 @@
|
||||
1,10,0,0
|
||||
2,72,32,2
|
||||
3,21,1,0
|
||||
4,11,1,0
|
||||
5,62,32,0
|
||||
6,22,2,0
|
||||
7,133,83,1
|
||||
8,42,12,0
|
||||
9,11,1,0
|
||||
10,93,33,0
|
|
1
performanceSRTF.csv
Normal file
1
performanceSRTF.csv
Normal file
@ -0,0 +1 @@
|
||||
293,3,3
|
|
203
simulator.c
203
simulator.c
@ -184,6 +184,81 @@ void prio_list_print(struct prio_list * list) {
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
struct remtime_list {
|
||||
struct pinfo ** proc;
|
||||
int length;
|
||||
};
|
||||
|
||||
/**
|
||||
* Pops and returns the last element (shortest remaining time) of the given remtime list
|
||||
*
|
||||
* Returns NULL if the list is empty
|
||||
*/
|
||||
struct pinfo * remtime_list_pop(struct remtime_list * list) {
|
||||
if (list->length == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list->length--;
|
||||
return *(list->proc + list->length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given process in the remtime list at
|
||||
* the appropriate place (according to its remaining time)
|
||||
*/
|
||||
void remtime_list_add(struct remtime_list * list, struct pinfo * proc) {
|
||||
struct pinfo * proc2;
|
||||
int idx = 0;
|
||||
|
||||
// Find first element (from the right) with longer remaining time
|
||||
for (int i=list->length-1; i>=0; i--) {
|
||||
proc2 = *(list->proc + i);
|
||||
if (proc2->remaining_time > proc->remaining_time) {
|
||||
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 remtime list
|
||||
*/
|
||||
struct remtime_list * create_remtime_list(int max_size) {
|
||||
struct pinfo ** processes = (struct pinfo **) malloc(sizeof(struct pinfo *) * max_size);
|
||||
struct remtime_list * list = (struct remtime_list *) malloc(sizeof(struct remtime_list));
|
||||
list->length = 0;
|
||||
list->proc = processes;
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last element (shortest remaining time) of the given remtime list
|
||||
*/
|
||||
struct pinfo * remtime_list_last(struct remtime_list * list) {
|
||||
return *(list->proc + list->length - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints the pids of the processes in the given remtime list
|
||||
*/
|
||||
void remtime_list_print(struct remtime_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};
|
||||
|
||||
@ -363,9 +438,128 @@ struct perf_info schedule_RR(struct pinfo *processes) { // Déclaration de la fo
|
||||
return perf; // Renvoie la structure de performance
|
||||
}
|
||||
|
||||
struct perf_info schedule_SRTF(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 remtime_list * queue = create_remtime_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;
|
||||
remtime_list_print(queue);
|
||||
printf("Processing next process (%d)\n", next->id);
|
||||
int delta = next->arrival_time - current_time;
|
||||
|
||||
// If current finished before next
|
||||
while (current != NULL && current->remaining_time <= delta) {
|
||||
printf(" (%d) Process %d finished before next\n", current_time, current->id);
|
||||
current->state = FINISHED;
|
||||
current_time += current->remaining_time;
|
||||
delta = next->arrival_time - current_time;
|
||||
current->turnaround_time = current_time - current->arrival_time;
|
||||
current->remaining_time = 0;
|
||||
finished++;
|
||||
current = remtime_list_pop(queue);
|
||||
}
|
||||
|
||||
if (current != NULL) {
|
||||
printf("Removing time from current process (%d)\n", current->id);
|
||||
current->remaining_time -= delta;
|
||||
current->state = RUNNING;
|
||||
printf(" New remaining time %d\n", current->remaining_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->remaining_time < current->remaining_time) {
|
||||
// Preempt current process
|
||||
printf("Next process (%d) has shorter remaining time\n", next->id);
|
||||
if (current->state == RUNNING) {
|
||||
printf(" Preempting current process (%d)\n", current->id);
|
||||
current->nb_time_pre_empted++;
|
||||
current->remaining_time -= next->arrival_time - current_time;
|
||||
current_time += CNTXT_SWITCH;
|
||||
perf.total_nr_ctxt_switch++;
|
||||
}
|
||||
current->state = READY;
|
||||
remtime_list_add(queue, current);
|
||||
|
||||
// Run process with shortest remaining time
|
||||
current = next;
|
||||
current->state = RUNNING;
|
||||
next = next->next_pinfo;
|
||||
} else {
|
||||
printf("Adding next process (%d) to list\n", next->id);
|
||||
remtime_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 = remtime_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 write_file(struct pinfo * process, struct perf_info * perf) {
|
||||
FILE *myStream_execution = fopen("executionPr.csv", "w");
|
||||
FILE *myStream_performance = fopen("performancePr.csv", "w");
|
||||
FILE *myStream_execution = fopen("executionSRTF.csv", "w");
|
||||
FILE *myStream_performance = fopen("performanceSRTF.csv", "w");
|
||||
|
||||
if (myStream_execution == NULL || myStream_performance == NULL) {
|
||||
perror("Erreur à l'ouverture des fichiers");
|
||||
@ -450,11 +644,14 @@ int main() {
|
||||
|
||||
//struct perf_info perf = schedule_FCFS(processes);
|
||||
//struct perf_info perf = schedule_RR(processes);
|
||||
struct perf_info perf = schedule_Pr(processes);
|
||||
//struct perf_info perf = schedule_Pr(processes);
|
||||
struct perf_info perf = schedule_SRTF(processes);
|
||||
|
||||
compute_waiting_time(processes);
|
||||
|
||||
write_file(processes, &perf);
|
||||
print_processes(processes);
|
||||
print_perf(&perf);
|
||||
|
||||
free_processes(processes);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user