2024-10-08 12:14:31 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2024-10-08 12:54:19 +00:00
|
|
|
#include <string.h>
|
2024-10-08 12:14:31 +00:00
|
|
|
|
2024-10-08 11:20:49 +00:00
|
|
|
#define RR_QUANTUM 2
|
|
|
|
#define CNTXT_SWITCH 1
|
2024-10-15 08:27:35 +00:00
|
|
|
#define MAX_PROCESSES 10
|
2024-10-08 11:20:49 +00:00
|
|
|
|
2024-10-08 12:14:31 +00:00
|
|
|
enum pstate {
|
|
|
|
WAITING,
|
|
|
|
READY,
|
2024-10-15 08:46:25 +00:00
|
|
|
RUNNING,
|
2024-10-08 12:14:31 +00:00
|
|
|
FINISHED
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pinfo {
|
|
|
|
int id;
|
|
|
|
int arrival_time;
|
|
|
|
int execution_time;
|
|
|
|
int priority;
|
|
|
|
|
|
|
|
int wait_time;
|
2024-10-15 08:46:25 +00:00
|
|
|
int turnaround_time;
|
2024-10-08 13:24:55 +00:00
|
|
|
int remaining_time;
|
2024-10-15 08:27:35 +00:00
|
|
|
int nb_time_pre_empted; // Ajouté pour compter les préemptions
|
2024-10-08 12:14:31 +00:00
|
|
|
enum pstate state;
|
|
|
|
|
|
|
|
struct pinfo * next_pinfo;
|
|
|
|
};
|
|
|
|
|
2024-10-15 08:46:25 +00:00
|
|
|
int n_processes(struct pinfo * head) {
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
while (head != NULL) {
|
|
|
|
n++;
|
|
|
|
head = head->next_pinfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2024-10-08 12:14:31 +00:00
|
|
|
struct perf_info {
|
|
|
|
int total_time;
|
|
|
|
int total_nr_ctxt_switch;
|
|
|
|
int total_time_ctxt_switch;
|
|
|
|
};
|
|
|
|
|
2024-10-08 12:54:19 +00:00
|
|
|
void print_perf(struct perf_info * perf) {
|
|
|
|
printf("Total time: %d\n", perf->total_time);
|
|
|
|
printf("Total number of context switches: %d\n", perf->total_nr_ctxt_switch);
|
|
|
|
printf("Total time spent on context switching: %d\n", perf->total_time_ctxt_switch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_pinfo(struct pinfo * info) {
|
|
|
|
printf("<Process {\n");
|
|
|
|
printf(" PID: %d\n", info->id);
|
|
|
|
printf(" Arrival time: %d\n", info->arrival_time);
|
|
|
|
printf(" Execution time: %d\n", info->execution_time);
|
|
|
|
printf(" Priority: %d\n", info->priority);
|
2024-10-08 13:21:47 +00:00
|
|
|
printf(" Wait time: %d\n", info->wait_time);
|
2024-10-15 08:46:25 +00:00
|
|
|
printf(" Turnaround time: %d\n", info->turnaround_time);
|
2024-10-08 13:24:55 +00:00
|
|
|
printf(" Remaining time: %d\n", info->remaining_time);
|
2024-10-08 13:16:41 +00:00
|
|
|
printf(" NEXT -> %p\n", info->next_pinfo);
|
2024-10-08 12:54:19 +00:00
|
|
|
printf("}>\n");
|
|
|
|
}
|
|
|
|
|
2024-10-08 13:21:47 +00:00
|
|
|
void print_processes(struct pinfo * processes) {
|
|
|
|
while (processes != NULL) {
|
|
|
|
print_pinfo(processes);
|
|
|
|
processes = processes->next_pinfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-08 13:16:41 +00:00
|
|
|
struct pinfo * create_process(int id, int arrival_time, int execution_time, int priority) {
|
|
|
|
struct pinfo * info = malloc(sizeof(struct pinfo));
|
|
|
|
info->id = id;
|
|
|
|
info->arrival_time = arrival_time;
|
|
|
|
info->execution_time = execution_time;
|
|
|
|
info->priority = priority;
|
|
|
|
info->wait_time = 0;
|
2024-10-15 08:46:25 +00:00
|
|
|
info->turnaround_time = 0;
|
2024-10-08 13:24:55 +00:00
|
|
|
info->remaining_time = execution_time;
|
2024-10-15 08:27:35 +00:00
|
|
|
info->nb_time_pre_empted = 0; // Initialiser le nombre de préemptions
|
2024-10-08 13:16:41 +00:00
|
|
|
info->state = WAITING;
|
|
|
|
info->next_pinfo = NULL;
|
2024-10-08 12:14:31 +00:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2024-10-15 08:27:35 +00:00
|
|
|
// Méthode de planification FCFS
|
2024-10-08 12:54:19 +00:00
|
|
|
struct perf_info schedule_FCFS(struct pinfo * processes) {
|
2024-10-08 12:14:31 +00:00
|
|
|
int current_time = 0;
|
|
|
|
struct pinfo * process = processes;
|
|
|
|
|
2024-10-08 12:54:19 +00:00
|
|
|
struct perf_info perf = {0, 0, 0};
|
2024-10-08 12:14:31 +00:00
|
|
|
|
|
|
|
while (process != NULL) {
|
|
|
|
int wait_time = current_time - process->arrival_time;
|
|
|
|
if (wait_time < 0) {
|
|
|
|
wait_time = 0;
|
|
|
|
}
|
|
|
|
process->wait_time = wait_time;
|
2024-10-15 08:46:25 +00:00
|
|
|
process->turnaround_time = process->execution_time + process->wait_time;
|
|
|
|
current_time = process->arrival_time + process->turnaround_time;
|
2024-10-08 12:14:31 +00:00
|
|
|
|
|
|
|
process = process->next_pinfo;
|
|
|
|
}
|
2024-10-08 12:54:19 +00:00
|
|
|
perf.total_time = current_time;
|
2024-10-08 12:14:31 +00:00
|
|
|
|
|
|
|
return perf;
|
|
|
|
}
|
|
|
|
|
2024-10-15 08:46:25 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2024-10-15 11:54:44 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2024-10-15 08:46:25 +00:00
|
|
|
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);
|
2024-10-15 08:53:57 +00:00
|
|
|
current->nb_time_pre_empted++;
|
2024-10-15 08:46:25 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2024-10-08 12:14:31 +00:00
|
|
|
|
2024-10-15 08:27:35 +00:00
|
|
|
struct perf_info schedule_RR(struct pinfo *processes) { // Déclaration de la fonction schedule_RR qui prend un pointeur vers une liste de processus
|
|
|
|
struct perf_info perf = {0, 0, 0}; // Initialisation de la structure de performance avec des valeurs à zéro
|
|
|
|
int current_time = 0; // Variable pour suivre le temps actuel
|
|
|
|
int finished_processes = 0; // Compteur pour le nombre de processus terminés
|
|
|
|
|
|
|
|
while (finished_processes < MAX_PROCESSES) { // Boucle principale jusqu'à ce que tous les processus soient terminés
|
|
|
|
int process_found = 0; // Indicateur pour savoir si un processus prêt a été trouvé
|
|
|
|
struct pinfo *current_process = processes; // Pointeur pour parcourir la liste des processus
|
|
|
|
|
|
|
|
while (current_process != NULL) { // Boucle pour parcourir tous les processus
|
|
|
|
// Vérifiez si le processus est prêt à s'exécuter
|
|
|
|
if (current_process->state != FINISHED && current_process->arrival_time <= current_time) { // Vérifie si le processus n'est pas fini et est arrivé
|
|
|
|
process_found = 1; // Un processus prêt à s'exécuter a été trouvé
|
|
|
|
|
|
|
|
int time_slice = (current_process->remaining_time < RR_QUANTUM) ? current_process->remaining_time : RR_QUANTUM; // Calcule la tranche de temps à exécuter
|
|
|
|
|
|
|
|
// Simuler l'exécution
|
|
|
|
current_time += time_slice; // Incrémente le temps actuel par la tranche de temps
|
|
|
|
current_process->remaining_time -= time_slice; // Diminue le temps restant du processus
|
|
|
|
|
|
|
|
// Calculer les temps d'attente pour les autres processus
|
|
|
|
struct pinfo *other_process = processes; // Pointeur pour parcourir à nouveau la liste des processus
|
|
|
|
while (other_process != NULL) { // Boucle pour parcourir tous les autres processus
|
|
|
|
if (other_process->state != FINISHED && other_process != current_process && other_process->arrival_time <= current_time) { // Vérifie si l'autre processus est prêt
|
|
|
|
other_process->wait_time += time_slice; // Augmente le temps d'attente des autres processus
|
|
|
|
}
|
|
|
|
other_process = other_process->next_pinfo; // Passe au processus suivant
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gérer les statistiques de préemption
|
|
|
|
if (current_process->remaining_time == 0) { // Vérifie si le processus est terminé
|
|
|
|
current_process->state = FINISHED; // Met à jour l'état du processus à fini
|
|
|
|
finished_processes++; // Incrémente le compteur de processus terminés
|
|
|
|
current_process->turnaround_time = current_time - current_process->arrival_time; // Calcule le temps de turnaround
|
|
|
|
} else {
|
|
|
|
// Incrémenter le nombre de préemptions
|
|
|
|
current_process->nb_time_pre_empted++; // Incrémente le compteur de préemptions pour le processus actuel
|
|
|
|
perf.total_nr_ctxt_switch++; // Incrémente le nombre total de commutations de contexte
|
|
|
|
}
|
|
|
|
|
|
|
|
// Débogage : Afficher les informations du processus
|
|
|
|
printf("Processus %d: remaining_time=%d, nb_time_pre_empted=%d\n", // Affiche les informations de débogage pour le processus actuel
|
|
|
|
current_process->id, current_process->remaining_time, current_process->nb_time_pre_empted);
|
|
|
|
}
|
|
|
|
|
|
|
|
current_process = current_process->next_pinfo; // Passe au processus suivant dans la liste
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!process_found) { // Vérifie si aucun processus prêt n'a été trouvé
|
|
|
|
// Aucun processus prêt, avancer le temps
|
|
|
|
current_time++; // Incrémente le temps actuel si aucun processus n'est prêt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
perf.total_time = current_time; // Enregistre le temps total écoulé dans la structure de performance
|
|
|
|
return perf; // Renvoie la structure de performance
|
|
|
|
}
|
|
|
|
|
2024-10-15 11:54:44 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-10-15 08:27:35 +00:00
|
|
|
void write_file(struct pinfo * process, struct perf_info * perf) {
|
2024-10-15 11:54:44 +00:00
|
|
|
FILE *myStream_execution = fopen("executionSRTF.csv", "w");
|
|
|
|
FILE *myStream_performance = fopen("performanceSRTF.csv", "w");
|
2024-10-15 08:27:35 +00:00
|
|
|
|
|
|
|
if (myStream_execution == NULL || myStream_performance == NULL) {
|
|
|
|
perror("Erreur à l'ouverture des fichiers");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (process != NULL) {
|
|
|
|
fprintf(myStream_execution, "%d,%d,%d,%d\n",
|
|
|
|
process->id,
|
|
|
|
process->turnaround_time,
|
|
|
|
process->wait_time,
|
|
|
|
process->nb_time_pre_empted);
|
|
|
|
process = process->next_pinfo;
|
|
|
|
}
|
|
|
|
fclose(myStream_execution);
|
|
|
|
|
|
|
|
fprintf(myStream_performance, "%d,%d,%d\n",
|
|
|
|
perf->total_time,
|
|
|
|
perf->total_nr_ctxt_switch,
|
|
|
|
perf->total_time_ctxt_switch);
|
|
|
|
|
|
|
|
fclose(myStream_performance);
|
|
|
|
}
|
|
|
|
|
2024-10-08 12:14:31 +00:00
|
|
|
struct pinfo * read_file() {
|
2024-10-08 12:54:19 +00:00
|
|
|
FILE * file = fopen("tasks.csv", "r");
|
|
|
|
unsigned long buf_size = sizeof(char) * 64;
|
|
|
|
char * line = (char *) malloc(buf_size);
|
|
|
|
char * pid_str;
|
|
|
|
char * arrival_str;
|
|
|
|
char * execution_str;
|
|
|
|
char * prio_str;
|
|
|
|
|
|
|
|
struct pinfo * first = NULL;
|
|
|
|
struct pinfo * last = NULL;
|
2024-10-08 13:16:41 +00:00
|
|
|
struct pinfo * process;
|
2024-10-08 12:54:19 +00:00
|
|
|
|
|
|
|
while (fgets(line, buf_size, file)) {
|
|
|
|
pid_str = strtok(line, " ");
|
|
|
|
arrival_str = strtok(NULL, " ");
|
|
|
|
execution_str = strtok(NULL, " ");
|
|
|
|
prio_str = strtok(NULL, " ");
|
|
|
|
|
|
|
|
process = create_process(
|
|
|
|
atoi(pid_str),
|
|
|
|
atoi(arrival_str),
|
|
|
|
atoi(execution_str),
|
|
|
|
atoi(prio_str)
|
|
|
|
);
|
|
|
|
|
2024-10-15 08:27:35 +00:00
|
|
|
// Si la liste n'est pas initialisée
|
2024-10-08 12:54:19 +00:00
|
|
|
if (first == NULL) {
|
2024-10-08 13:16:41 +00:00
|
|
|
first = process;
|
2024-10-08 12:54:19 +00:00
|
|
|
}
|
2024-10-08 12:14:31 +00:00
|
|
|
|
2024-10-15 08:27:35 +00:00
|
|
|
// Si un élément est déjà dans la liste
|
2024-10-08 12:54:19 +00:00
|
|
|
if (last != NULL) {
|
2024-10-08 13:16:41 +00:00
|
|
|
last->next_pinfo = process;
|
2024-10-08 12:54:19 +00:00
|
|
|
}
|
2024-10-08 13:16:41 +00:00
|
|
|
last = process;
|
2024-10-08 12:54:19 +00:00
|
|
|
|
2024-10-08 13:16:41 +00:00
|
|
|
print_pinfo(process);
|
2024-10-08 12:54:19 +00:00
|
|
|
}
|
2024-10-08 13:16:41 +00:00
|
|
|
free(line);
|
2024-10-08 12:54:19 +00:00
|
|
|
fclose(file);
|
|
|
|
return first;
|
2024-10-08 12:14:31 +00:00
|
|
|
}
|
|
|
|
|
2024-10-08 13:16:41 +00:00
|
|
|
void free_processes(struct pinfo * next) {
|
|
|
|
struct pinfo * cur;
|
|
|
|
while (next != NULL) {
|
|
|
|
cur = next;
|
|
|
|
next = cur->next_pinfo;
|
|
|
|
free(cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-14 15:20:29 +00:00
|
|
|
|
|
|
|
|
2024-10-08 11:20:49 +00:00
|
|
|
int main() {
|
2024-10-08 12:14:31 +00:00
|
|
|
struct pinfo * processes = read_file();
|
|
|
|
|
2024-10-15 08:46:25 +00:00
|
|
|
//struct perf_info perf = schedule_FCFS(processes);
|
2024-10-15 08:53:57 +00:00
|
|
|
//struct perf_info perf = schedule_RR(processes);
|
2024-10-15 11:54:44 +00:00
|
|
|
//struct perf_info perf = schedule_Pr(processes);
|
|
|
|
struct perf_info perf = schedule_SRTF(processes);
|
2024-10-15 08:46:25 +00:00
|
|
|
|
|
|
|
compute_waiting_time(processes);
|
2024-10-08 13:16:41 +00:00
|
|
|
|
2024-10-08 14:44:55 +00:00
|
|
|
write_file(processes, &perf);
|
2024-10-15 11:54:44 +00:00
|
|
|
print_processes(processes);
|
|
|
|
print_perf(&perf);
|
2024-10-08 12:14:31 +00:00
|
|
|
|
2024-10-08 13:16:41 +00:00
|
|
|
free_processes(processes);
|
2024-10-08 11:20:49 +00:00
|
|
|
|
|
|
|
return 0;
|
2024-10-15 08:27:35 +00:00
|
|
|
}
|