Try
This commit is contained in:
parent
4c221983c0
commit
fb90cbf14a
10
execution.csv
Normal file
10
execution.csv
Normal file
@ -0,0 +1,10 @@
|
||||
1,10,0
|
||||
2,40,0
|
||||
3,50,30
|
||||
4,30,20
|
||||
5,40,10
|
||||
6,50,30
|
||||
7,90,40
|
||||
8,90,60
|
||||
9,50,40
|
||||
10,90,30
|
|
1
performance.csv
Normal file
1
performance.csv
Normal file
@ -0,0 +1 @@
|
||||
290,0,0
|
|
117
simulator.c
117
simulator.c
@ -138,10 +138,24 @@ struct pinfo * read_file() {
|
||||
return first;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void free_processes(struct pinfo * next) {
|
||||
struct pinfo * cur;
|
||||
while (next != NULL) {
|
||||
cur = next;
|
||||
next = cur->next_pinfo;
|
||||
free(cur);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void write_file(struct pinfo * process, struct perf_info * perf) {
|
||||
|
||||
FILE *myStream_execution = fopen("execution.csv", "w");
|
||||
FILE *myStream_performance = fopen("performance.csv", "w");
|
||||
FILE *myStream_execution = fopen("execution2.csv", "w");
|
||||
FILE *myStream_performance = fopen("performance2.csv", "w");
|
||||
|
||||
|
||||
if (myStream_execution == NULL || myStream_performance == NULL) {
|
||||
perror("Erreur à l'ouverture des fichiers");
|
||||
@ -149,32 +163,117 @@ void write_file(struct pinfo * process, struct perf_info * perf) {
|
||||
}
|
||||
|
||||
while (process != NULL) {
|
||||
fprintf(myStream_execution, "%d,%d,%d\n",
|
||||
fprintf(myStream_execution, "%d,%d,%d,%d\n", // Ajout de la colonne des préemptions
|
||||
process->id,
|
||||
process->turnaround_time,
|
||||
process->wait_time);
|
||||
process->wait_time,
|
||||
process->execution_time - process->remaining_time); // Nombre de préemptions
|
||||
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 perf_info schedule_RR(struct pinfo * processes) {
|
||||
int current_time = 0; // Temps actuel
|
||||
int context_switches = 0; // Nombre de changements de contexte
|
||||
struct perf_info perf = {0, 0, 0};
|
||||
|
||||
struct pinfo * queue = processes; // File d'attente de processus
|
||||
struct pinfo * temp; // Variable temporaire pour itérer
|
||||
|
||||
while (1) {
|
||||
int all_done = 1; // Vérifier si tous les processus sont terminés
|
||||
|
||||
// Itérer sur les processus dans la queue
|
||||
temp = queue;
|
||||
while (temp != NULL) {
|
||||
// Vérifiez si le processus a encore du temps restant
|
||||
if (temp->remaining_time > 0) {
|
||||
all_done = 0; // Il y a au moins un processus qui n'est pas terminé
|
||||
|
||||
// Si le temps actuel est inférieur au temps d'arrivée
|
||||
if (current_time < temp->arrival_time) {
|
||||
current_time = temp->arrival_time; // Mettez à jour le temps
|
||||
}
|
||||
|
||||
// Exécutez le processus pendant le quantum
|
||||
if (temp->remaining_time > RR_QUANTUM) {
|
||||
current_time += RR_QUANTUM; // Ajoute le quantum au temps
|
||||
temp->remaining_time -= RR_QUANTUM; // Diminue le temps restant
|
||||
context_switches++; // Compter le changement de contexte
|
||||
} else {
|
||||
// Le processus se termine ici
|
||||
current_time += temp->remaining_time; // Ajoutez le temps restant à current_time
|
||||
temp->turnaround_time = current_time - temp->arrival_time; // Calculer le TAT
|
||||
temp->wait_time += (temp->turnaround_time - temp->execution_time); // Calculer le WT
|
||||
temp->remaining_time = 0; // Le processus est terminé
|
||||
temp->state = FINISHED; // Marquez comme terminé
|
||||
}
|
||||
}
|
||||
temp = temp->next_pinfo; // Passer au processus suivant
|
||||
}
|
||||
|
||||
// Réinitialiser la file d'attente
|
||||
temp = processes; // Revenir au début de la liste
|
||||
struct pinfo * last = NULL; // Pour maintenir la fin de la queue
|
||||
|
||||
// Créer une nouvelle file d'attente
|
||||
while (temp != NULL) {
|
||||
// Ne pas ajouter les processus terminés à la nouvelle queue
|
||||
if (temp->remaining_time > 0) {
|
||||
if (last == NULL) {
|
||||
queue = temp; // Premier processus dans la nouvelle queue
|
||||
} else {
|
||||
last->next_pinfo = temp; // Ajouter à la fin
|
||||
}
|
||||
last = temp; // Mettre à jour le dernier élément de la queue
|
||||
}
|
||||
temp = temp->next_pinfo; // Passer au processus suivant
|
||||
}
|
||||
if (last != NULL) {
|
||||
last->next_pinfo = NULL; // Terminer la liste
|
||||
}
|
||||
|
||||
// Si tous les processus sont terminés, sortez de la boucle
|
||||
if (all_done) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Mise à jour des statistiques de performance
|
||||
perf.total_time = current_time; // Total du temps écoulé
|
||||
perf.total_nr_ctxt_switch = context_switches; // Total des changements de contexte
|
||||
perf.total_time_ctxt_switch = context_switches * CNTXT_SWITCH; // Temps total pour les changements de contexte
|
||||
|
||||
return perf; // Retournez les performances
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
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);
|
||||
|
||||
write_file(processes, &perf);
|
||||
print_processes(processes);
|
||||
print_perf(&perf);
|
||||
//print_processes(processes);
|
||||
//print_perf(&perf);
|
||||
|
||||
free_processes(processes);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user