From 800f965b81a33541d8bd309501e027c929977695 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 15 Oct 2024 15:57:58 +0200 Subject: [PATCH] refactored RR with arrays --- executionRR.csv | 20 ++++---- performanceRR.csv | 2 +- simulator.c | 120 ++++++++++++++++++++++++---------------------- 3 files changed, 75 insertions(+), 67 deletions(-) diff --git a/executionRR.csv b/executionRR.csv index 7c4c0c1..e8106f8 100644 --- a/executionRR.csv +++ b/executionRR.csv @@ -1,10 +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,10,0,0 +2,188,148,15 +3,81,61,9 +4,53,43,4 +5,218,188,14 +6,159,139,9 +7,277,227,24 +8,200,170,14 +9,77,67,4 +10,201,141,18 diff --git a/performanceRR.csv b/performanceRR.csv index fc78dc5..5d4616e 100644 --- a/performanceRR.csv +++ b/performanceRR.csv @@ -1 +1 @@ -290,130,0 +401,111,111 diff --git a/simulator.c b/simulator.c index 3c12e04..5e2b12c 100644 --- a/simulator.c +++ b/simulator.c @@ -2,7 +2,7 @@ #include #include -#define RR_QUANTUM 1 +#define RR_QUANTUM 2 #define CNTXT_SWITCH 1 int MAX_PROCESSES = 0; @@ -382,63 +382,71 @@ void compute_waiting_time(struct pinfo * processes) { struct perf_info schedule_RR(struct pinfo *processes) { struct perf_info perf = {0, 0, 0}; // Initialisation de la structure de performance + + // Conversion de la liste chaînée en array + int N = n_processes(processes); + struct pinfo ** proc_list = (struct pinfo **) malloc(sizeof(struct pinfo *) * N); + struct pinfo * p = processes; + int i = 0; + while (p != NULL) { + proc_list[i] = p; + p = p->next_pinfo; + i++; + } + + struct pinfo *current_process = processes; int current_time = 0; // Temps actuel int finished_processes = 0; // Compteur de processus terminés - - while (finished_processes < MAX_PROCESSES) { - int process_found = 0; // Indicateur de processus trouvé - struct pinfo *current_process = processes; - - while (current_process != NULL) { - // Vérifie si le processus est prêt à être exécuté - if (current_process->state != FINISHED && current_process->arrival_time <= current_time) { - process_found = 1; // Un processus prêt a été trouvé - - // Détermine la tranche de temps à exécuter (quantum ou le temps restant, selon le moindre) - int time_slice = (current_process->remaining_time < RR_QUANTUM) ? current_process->remaining_time : RR_QUANTUM; - - // Simule l'exécution - current_time += time_slice; - current_process->remaining_time -= time_slice; - - // Mise à jour du temps d'attente des autres processus - struct pinfo *other_process = processes; - while (other_process != NULL) { - if (other_process->state != FINISHED && other_process != current_process && other_process->arrival_time <= current_time) { - other_process->wait_time += time_slice; + int running_processes = 0; + struct pinfo * last_running = NULL; + while (finished_processes < N) { + printf("\n"); + for (int i = 0; i < N; i++) { + p = proc_list[i]; + if (p->state == FINISHED) { + continue; + } + printf("Current time: %d, pid: %d\n", current_time, p->id); + + if (p->state == WAITING) { + if (p->arrival_time <= current_time) { + p->state = READY; + running_processes++; + if (last_running != NULL) { + printf("Preempting last running process (current time: %d, pid: %d)\n", current_time, last_running->id); + last_running->nb_time_pre_empted++; + perf.total_nr_ctxt_switch++; + current_time += CNTXT_SWITCH; } - other_process = other_process->next_pinfo; + printf("Process %d is now ready\n", p->id); + printf("Running processes: %d\n", running_processes); } - - // Gestion des statistiques de préemption - if (current_process->remaining_time == 0) { - current_process->state = FINISHED; // Marquer le processus comme terminé - finished_processes++; // Incrémenter le compteur des processus terminés - current_process->turnaround_time = current_time - current_process->arrival_time; // Calculer le temps de turnaround - } else { - // Incrémenter le nombre de préemptions uniquement si le processus est interrompu avant de terminer - if (time_slice == RR_QUANTUM) { - current_process->nb_time_pre_empted++; // Incrémente le compteur de préemptions - perf.total_nr_ctxt_switch++; // Incrémente le compteur des commutations de contexte - - - // Débogage : Afficher les informations du processus - printf("Current time %d: Processus %d: remaining_time=%d, nb_time_pre_empted=%d\n", 1 , - current_process->id, current_process->remaining_time, current_process->nb_time_pre_empted); - } - } - - } - - - - current_process = current_process->next_pinfo; + if (p->state == READY) { + last_running = p; + p->remaining_time -= RR_QUANTUM; + current_time += RR_QUANTUM; + printf("Executing quantum for process %d, remaining_time %d\n", p->id, p->remaining_time); + if (p->remaining_time <= 0) { + printf(" Process has finished\n"); + p->remaining_time = 0; + p->turnaround_time = current_time - p->arrival_time; + p->state = FINISHED; + last_running = NULL; + finished_processes++; + running_processes--; + } else if (running_processes > 1) { + printf(" Preempting process (current time: %d, pid: %d)\n", current_time, p->id); + last_running = NULL; + p->nb_time_pre_empted++; + perf.total_nr_ctxt_switch++; + current_time += CNTXT_SWITCH; + } + } } - - if (!process_found) { - current_time++; // Si aucun processus n'est prêt, incrémente le temps + if (finished_processes < N && running_processes == 0) { + current_time++; } } @@ -567,8 +575,8 @@ struct perf_info schedule_SRTF(struct pinfo *processes) { } void write_file(struct pinfo * process, struct perf_info * perf) { - FILE *myStream_execution = fopen("executionSRTF.csv", "w"); - FILE *myStream_performance = fopen("performanceSRTF.csv", "w"); + 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"); @@ -594,7 +602,7 @@ void write_file(struct pinfo * process, struct perf_info * perf) { } struct pinfo * read_file() { - FILE * file = fopen("tasks RR.csv", "r"); + FILE * file = fopen("tasks.csv", "r"); unsigned long buf_size = sizeof(char) * 64; char * line = (char *) malloc(buf_size); char * pid_str; @@ -657,9 +665,9 @@ int main() { struct pinfo * processes = read_file(); //struct perf_info perf = schedule_FCFS(processes); - //struct perf_info perf = schedule_RR(processes); + struct perf_info perf = schedule_RR(processes); //struct perf_info perf = schedule_Pr(processes); - struct perf_info perf = schedule_SRTF(processes); + //struct perf_info perf = schedule_SRTF(processes); compute_waiting_time(processes);