1
0
OS-Lab-Scheduler/simulator.c

297 lines
8.1 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define RR_QUANTUM 2
#define CNTXT_SWITCH 1
enum pstate {
WAITING,
READY,
FINISHED
};
struct pinfo {
int id;
int arrival_time;
int execution_time;
int priority;
int nb_time_pre_empted;
int wait_time;
int turnaround_time;
int remaining_time;
enum pstate state;
struct pinfo * next_pinfo;
};
struct perf_info {
int total_time;
int total_nr_ctxt_switch;
int total_time_ctxt_switch;
};
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);
printf(" Wait time: %d\n", info->wait_time);
printf(" Turnaround time: %d\n", info->turnaround_time);
printf(" Remaining time: %d\n", info->remaining_time);
printf(" NEXT -> %p\n", info->next_pinfo);
printf("}>\n");
}
void print_processes(struct pinfo * processes) {
while (processes != NULL) {
print_pinfo(processes);
processes = processes->next_pinfo;
}
}
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;
info->turnaround_time = 0;
info->remaining_time = execution_time;
info->state = WAITING;
info->next_pinfo = NULL;
return info;
}
struct perf_info schedule_FCFS(struct pinfo * processes) {
int current_time = 0;
struct pinfo * process = processes;
struct perf_info perf = {0, 0, 0};
while (process != NULL) {
int wait_time = current_time - process->arrival_time;
if (wait_time < 0) {
wait_time = 0;
}
process->wait_time = wait_time;
process->turnaround_time = process->execution_time + process->wait_time;
current_time = process->arrival_time + process->turnaround_time;
process = process->next_pinfo;
}
perf.total_time = current_time;
return perf;
}
struct pinfo * read_file() {
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;
struct pinfo * process;
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) {
first = process;
}
// If there is an element in list
if (last != NULL) {
last->next_pinfo = process;
}
last = process;
print_pinfo(process);
}
free(line);
fclose(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("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_RR(processes);
write_file(processes, &perf);
//print_processes(processes);
//print_perf(&perf);
free_processes(processes);
return 0;
}