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-08 12:14:31 +00:00
|
|
|
enum pstate {
|
|
|
|
WAITING,
|
|
|
|
READY,
|
|
|
|
FINISHED
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pinfo {
|
|
|
|
int id;
|
|
|
|
int arrival_time;
|
|
|
|
int execution_time;
|
|
|
|
int priority;
|
|
|
|
|
|
|
|
int wait_time;
|
2024-10-08 14:44:55 +00:00
|
|
|
int turnaround_time;
|
2024-10-08 13:24:55 +00:00
|
|
|
int remaining_time;
|
2024-10-08 12:14:31 +00:00
|
|
|
enum pstate state;
|
|
|
|
|
|
|
|
struct pinfo * next_pinfo;
|
|
|
|
};
|
|
|
|
|
|
|
|
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-08 14:44:55 +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-08 14:44:55 +00:00
|
|
|
info->turnaround_time = 0;
|
2024-10-08 13:24:55 +00:00
|
|
|
info->remaining_time = execution_time;
|
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-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-08 14:44:55 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
);
|
|
|
|
|
|
|
|
// If linked list not initialized
|
|
|
|
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-08 12:54:19 +00:00
|
|
|
// If there is an element in list
|
|
|
|
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-14 15:20:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void free_processes(struct pinfo * next) {
|
|
|
|
struct pinfo * cur;
|
|
|
|
while (next != NULL) {
|
|
|
|
cur = next;
|
|
|
|
next = cur->next_pinfo;
|
|
|
|
free(cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-08 12:14:31 +00:00
|
|
|
void write_file(struct pinfo * process, struct perf_info * perf) {
|
2024-10-08 14:44:55 +00:00
|
|
|
|
2024-10-14 15:20:29 +00:00
|
|
|
FILE *myStream_execution = fopen("execution2.csv", "w");
|
|
|
|
FILE *myStream_performance = fopen("performance2.csv", "w");
|
|
|
|
|
2024-10-08 14:44:55 +00:00
|
|
|
|
|
|
|
if (myStream_execution == NULL || myStream_performance == NULL) {
|
|
|
|
perror("Erreur à l'ouverture des fichiers");
|
|
|
|
return;
|
|
|
|
}
|
2024-10-08 12:14:31 +00:00
|
|
|
|
2024-10-08 14:44:55 +00:00
|
|
|
while (process != NULL) {
|
2024-10-14 15:20:29 +00:00
|
|
|
fprintf(myStream_execution, "%d,%d,%d,%d\n", // Ajout de la colonne des préemptions
|
2024-10-08 14:44:55 +00:00
|
|
|
process->id,
|
|
|
|
process->turnaround_time,
|
2024-10-14 15:20:29 +00:00
|
|
|
process->wait_time,
|
|
|
|
process->execution_time - process->remaining_time); // Nombre de préemptions
|
2024-10-08 14:44:55 +00:00
|
|
|
process = process->next_pinfo;
|
2024-10-08 13:16:41 +00:00
|
|
|
}
|
2024-10-08 14:44:55 +00:00
|
|
|
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 13:16:41 +00:00
|
|
|
}
|
|
|
|
|
2024-10-14 15:20:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-14 15:20:29 +00:00
|
|
|
//struct perf_info perf = schedule_FCFS(processes);
|
|
|
|
|
|
|
|
struct perf_info perf = schedule_RR(processes);
|
2024-10-08 13:16:41 +00:00
|
|
|
|
2024-10-08 14:44:55 +00:00
|
|
|
write_file(processes, &perf);
|
2024-10-14 15:20:29 +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
|
|
|
|
2024-10-08 14:44:55 +00:00
|
|
|
|
|
|
|
|
2024-10-08 11:20:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|