RR fait
This commit is contained in:
parent
dfab397122
commit
5509743101
10
executionRR.csv
Normal file
10
executionRR.csv
Normal file
@ -0,0 +1,10 @@
|
||||
1,10,0,4
|
||||
2,84,44,19
|
||||
3,42,24,9
|
||||
4,22,14,4
|
||||
5,94,66,14
|
||||
6,66,48,9
|
||||
7,148,100,24
|
||||
8,98,70,14
|
||||
9,30,22,4
|
||||
10,90,32,29
|
|
1
performanceRR.csv
Normal file
1
performanceRR.csv
Normal file
@ -0,0 +1 @@
|
||||
290,130,0
|
|
11
results.csv
Normal file
11
results.csv
Normal file
@ -0,0 +1,11 @@
|
||||
PID,Arrival Time,Execution Time,Wait Time,Turnaround Time,Remaining Time
|
||||
1,0,10,6,14,0
|
||||
2,20,40,545,129,0
|
||||
3,30,20,123,54,0
|
||||
4,60,10,42,33,0
|
||||
5,80,30,709,140,0
|
||||
6,90,20,295,90,0
|
||||
7,100,50,2016,196,0
|
||||
8,130,30,739,131,0
|
||||
9,180,10,60,46,0
|
||||
10,200,60,1694,143,0
|
|
235
simulator.c
235
simulator.c
@ -4,6 +4,7 @@
|
||||
|
||||
#define RR_QUANTUM 2
|
||||
#define CNTXT_SWITCH 1
|
||||
#define MAX_PROCESSES 10
|
||||
|
||||
enum pstate {
|
||||
WAITING,
|
||||
@ -16,11 +17,11 @@ struct pinfo {
|
||||
int arrival_time;
|
||||
int execution_time;
|
||||
int priority;
|
||||
int nb_time_pre_empted;
|
||||
|
||||
int wait_time;
|
||||
int turnaround_time;
|
||||
int remaining_time;
|
||||
int nb_time_pre_empted; // Ajouté pour compter les préemptions
|
||||
enum pstate state;
|
||||
|
||||
struct pinfo * next_pinfo;
|
||||
@ -67,11 +68,13 @@ struct pinfo * create_process(int id, int arrival_time, int execution_time, int
|
||||
info->wait_time = 0;
|
||||
info->turnaround_time = 0;
|
||||
info->remaining_time = execution_time;
|
||||
info->nb_time_pre_empted = 0; // Initialiser le nombre de préemptions
|
||||
info->state = WAITING;
|
||||
info->next_pinfo = NULL;
|
||||
return info;
|
||||
}
|
||||
|
||||
// Méthode de planification FCFS
|
||||
struct perf_info schedule_FCFS(struct pinfo * processes) {
|
||||
int current_time = 0;
|
||||
struct pinfo * process = processes;
|
||||
@ -94,6 +97,97 @@ struct perf_info schedule_FCFS(struct pinfo * processes) {
|
||||
return perf;
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
void write_file(struct pinfo * process, struct perf_info * perf) {
|
||||
FILE *myStream_execution = fopen("executionRR.csv", "w");
|
||||
FILE *myStream_performance = fopen("performanceRR.csv", "w");
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
struct pinfo * read_file() {
|
||||
FILE * file = fopen("tasks.csv", "r");
|
||||
@ -121,12 +215,12 @@ struct pinfo * read_file() {
|
||||
atoi(prio_str)
|
||||
);
|
||||
|
||||
// If linked list not initialized
|
||||
// Si la liste n'est pas initialisée
|
||||
if (first == NULL) {
|
||||
first = process;
|
||||
}
|
||||
|
||||
// If there is an element in list
|
||||
// Si un élément est déjà dans la liste
|
||||
if (last != NULL) {
|
||||
last->next_pinfo = process;
|
||||
}
|
||||
@ -139,9 +233,6 @@ struct pinfo * read_file() {
|
||||
return first;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void free_processes(struct pinfo * next) {
|
||||
struct pinfo * cur;
|
||||
while (next != NULL) {
|
||||
@ -152,146 +243,16 @@ void free_processes(struct pinfo * next) {
|
||||
}
|
||||
|
||||
|
||||
void write_file(struct pinfo * process, struct perf_info * perf) {
|
||||
|
||||
FILE *myStream_execution = fopen("executionRR.csv", "w");
|
||||
FILE *myStream_performance = fopen("performanceRR.csv", "w");
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
struct pinfo *enqueue(struct pinfo *queue, struct pinfo *process) {
|
||||
if (queue == NULL) {
|
||||
return process;
|
||||
}
|
||||
struct pinfo *temp = queue;
|
||||
while (temp->next_pinfo != NULL) {
|
||||
temp = temp->next_pinfo;
|
||||
}
|
||||
temp->next_pinfo = process;
|
||||
return queue;
|
||||
}
|
||||
|
||||
struct pinfo *dequeue(struct pinfo **queue) {
|
||||
if (*queue == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
struct pinfo *process = *queue;
|
||||
*queue = (*queue)->next_pinfo;
|
||||
process->next_pinfo = NULL;
|
||||
return process;
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct perf_info schedule_RR(struct pinfo *processes) {
|
||||
struct perf_info perf = {0, 0, 0};
|
||||
int current_time = 0;
|
||||
struct pinfo *queue = NULL;
|
||||
struct pinfo *current_process = NULL;
|
||||
int remaining_quantum = RR_QUANTUM;
|
||||
|
||||
while (processes != NULL || queue != NULL || current_process != NULL) {
|
||||
// Ajouter les nouveaux processus arrivés à la file d'attente
|
||||
while (processes != NULL && processes->arrival_time <= current_time) {
|
||||
processes->state = READY;
|
||||
queue = enqueue(queue, processes);
|
||||
printf("Processus %d ajouté à la file d'attente à l'heure %d\n", processes->id, current_time);
|
||||
processes = processes->next_pinfo;
|
||||
}
|
||||
|
||||
// Si aucun processus en cours, prendre le suivant dans la file d'attente
|
||||
if (current_process == NULL && queue != NULL) {
|
||||
current_process = dequeue(&queue);
|
||||
current_process->nb_time_pre_empted++;
|
||||
remaining_quantum = RR_QUANTUM; // Réinitialiser le quantum
|
||||
printf("Processus %d pris de la file d'attente pour exécution à l'heure %d\n", current_process->id, current_time);
|
||||
}
|
||||
|
||||
// Exécuter le processus courant
|
||||
if (current_process != NULL) {
|
||||
current_process->remaining_time--;
|
||||
current_process->turnaround_time++;
|
||||
remaining_quantum--;
|
||||
printf("Processus %d exécuté à l'heure %d, temps restant: %d, quantum restant: %d\n", current_process->id, current_time, current_process->remaining_time, remaining_quantum);
|
||||
current_time++;
|
||||
|
||||
// Vérifier si le processus est terminé
|
||||
if (current_process->remaining_time == 0) {
|
||||
current_process->state = FINISHED;
|
||||
printf("Processus %d terminé à l'heure %d\n", current_process->id, current_time);
|
||||
current_process = NULL;
|
||||
} else if (remaining_quantum == 0) {
|
||||
// Si le quantum RR est atteint, remettre le processus en file d'attente
|
||||
current_process->state = WAITING;
|
||||
queue = enqueue(queue, current_process);
|
||||
printf("Processus %d préempté et remis en file d'attente à l'heure %d\n", current_process->id, current_time);
|
||||
current_process = NULL;
|
||||
}
|
||||
} else {
|
||||
// Si aucun processus à exécuter, avancer le temps
|
||||
printf("Aucun processus à exécuter à l'heure %d, avancer le temps\n", current_time);
|
||||
current_time++;
|
||||
}
|
||||
|
||||
// Mise à jour des performances
|
||||
perf.total_time = current_time;
|
||||
perf.total_nr_ctxt_switch++;
|
||||
perf.total_time_ctxt_switch += CNTXT_SWITCH;
|
||||
}
|
||||
|
||||
printf("Planification RR terminée\n");
|
||||
return perf;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
struct pinfo * processes = read_file();
|
||||
|
||||
//struct perf_info perf = schedule_FCFS(processes);
|
||||
|
||||
//struct perf_info perf = schedule_FCFS(processes); // Remise en place de FCFS
|
||||
struct perf_info perf = schedule_RR(processes);
|
||||
|
||||
write_file(processes, &perf);
|
||||
//print_processes(processes);
|
||||
//print_perf(&perf);
|
||||
|
||||
free_processes(processes);
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
BIN
tempCodeRunnerFile
Executable file
BIN
tempCodeRunnerFile
Executable file
Binary file not shown.
Loading…
Reference in New Issue
Block a user