1
0

refactored RR with arrays

This commit is contained in:
Louis Heredero 2024-10-15 15:57:58 +02:00
parent 1ba893868e
commit 800f965b81
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
3 changed files with 75 additions and 67 deletions

View File

@ -1,10 +1,10 @@
1,10,0,4 1,10,0,0
2,84,44,19 2,188,148,15
3,42,24,9 3,81,61,9
4,22,14,4 4,53,43,4
5,94,66,14 5,218,188,14
6,66,48,9 6,159,139,9
7,148,100,24 7,277,227,24
8,98,70,14 8,200,170,14
9,30,22,4 9,77,67,4
10,90,32,29 10,201,141,18

1 1 10 0 4 0
2 2 84 188 44 148 19 15
3 3 42 81 24 61 9 9
4 4 22 53 14 43 4 4
5 5 94 218 66 188 14 14
6 6 66 159 48 139 9 9
7 7 148 277 100 227 24 24
8 8 98 200 70 170 14 14
9 9 30 77 22 67 4 4
10 10 90 201 32 141 29 18

View File

@ -1 +1 @@
290,130,0 401,111,111

1 290 401 130 111 0 111

View File

@ -2,7 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#define RR_QUANTUM 1 #define RR_QUANTUM 2
#define CNTXT_SWITCH 1 #define CNTXT_SWITCH 1
int MAX_PROCESSES = 0; 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 schedule_RR(struct pinfo *processes) {
struct perf_info perf = {0, 0, 0}; // Initialisation de la structure de performance 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 current_time = 0; // Temps actuel
int finished_processes = 0; // Compteur de processus terminés int finished_processes = 0; // Compteur de processus terminés
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);
while (finished_processes < MAX_PROCESSES) { if (p->state == WAITING) {
int process_found = 0; // Indicateur de processus trouvé if (p->arrival_time <= current_time) {
struct pinfo *current_process = processes; p->state = READY;
running_processes++;
while (current_process != NULL) { if (last_running != NULL) {
// Vérifie si le processus est prêt à être exécuté printf("Preempting last running process (current time: %d, pid: %d)\n", current_time, last_running->id);
if (current_process->state != FINISHED && current_process->arrival_time <= current_time) { last_running->nb_time_pre_empted++;
process_found = 1; // Un processus prêt a été trouvé perf.total_nr_ctxt_switch++;
current_time += CNTXT_SWITCH;
// 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;
} }
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);
}
}
} }
if (p->state == READY) {
last_running = p;
p->remaining_time -= RR_QUANTUM;
current_process = current_process->next_pinfo; 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 (finished_processes < N && running_processes == 0) {
if (!process_found) { current_time++;
current_time++; // Si aucun processus n'est prêt, incrémente le temps
} }
} }
@ -567,8 +575,8 @@ struct perf_info schedule_SRTF(struct pinfo *processes) {
} }
void write_file(struct pinfo * process, struct perf_info * perf) { void write_file(struct pinfo * process, struct perf_info * perf) {
FILE *myStream_execution = fopen("executionSRTF.csv", "w"); FILE *myStream_execution = fopen("executionRR.csv", "w");
FILE *myStream_performance = fopen("performanceSRTF.csv", "w"); FILE *myStream_performance = fopen("performanceRR.csv", "w");
if (myStream_execution == NULL || myStream_performance == NULL) { if (myStream_execution == NULL || myStream_performance == NULL) {
perror("Erreur à l'ouverture des fichiers"); perror("Erreur à l'ouverture des fichiers");
@ -594,7 +602,7 @@ void write_file(struct pinfo * process, struct perf_info * perf) {
} }
struct pinfo * read_file() { struct pinfo * read_file() {
FILE * file = fopen("tasks RR.csv", "r"); FILE * file = fopen("tasks.csv", "r");
unsigned long buf_size = sizeof(char) * 64; unsigned long buf_size = sizeof(char) * 64;
char * line = (char *) malloc(buf_size); char * line = (char *) malloc(buf_size);
char * pid_str; char * pid_str;
@ -657,9 +665,9 @@ 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_RR(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); //struct perf_info perf = schedule_SRTF(processes);
compute_waiting_time(processes); compute_waiting_time(processes);