Compare commits
	
		
			5 Commits
		
	
	
		
			1e66a95fbd
			...
			main
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 800f965b81 | |||
|  | 1ba893868e | ||
|  | df8f5a5f78 | ||
| 4e527bccfe | |||
| 6cc65667de | 
| @@ -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 | ||||||
|   | |||||||
| 
 | 
							
								
								
									
										5
									
								
								executionRRtest.csv
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								executionRRtest.csv
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | |||||||
|  | 1,6,3,2 | ||||||
|  | 2,10,5,4 | ||||||
|  | 3,5,3,1 | ||||||
|  | 4,9,4,4 | ||||||
|  | 5,8,3,4 | ||||||
| 
 | 
							
								
								
									
										10
									
								
								executionSRTF.csv
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								executionSRTF.csv
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | 1,10,0,0 | ||||||
|  | 2,72,32,2 | ||||||
|  | 3,21,1,0 | ||||||
|  | 4,11,1,0 | ||||||
|  | 5,62,32,0 | ||||||
|  | 6,22,2,0 | ||||||
|  | 7,133,83,1 | ||||||
|  | 8,42,12,0 | ||||||
|  | 9,11,1,0 | ||||||
|  | 10,93,33,0 | ||||||
| 
 | 
| @@ -1 +1 @@ | |||||||
| 290,130,0 | 401,111,111 | ||||||
|   | |||||||
| 
 | 
							
								
								
									
										1
									
								
								performanceRRtest.csv
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								performanceRRtest.csv
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | 20,15,15 | ||||||
| 
 | 
							
								
								
									
										1
									
								
								performanceSRTF.csv
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								performanceSRTF.csv
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | 293,3,3 | ||||||
| 
 | 
							
								
								
									
										308
									
								
								simulator.c
									
									
									
									
									
								
							
							
						
						
									
										308
									
								
								simulator.c
									
									
									
									
									
								
							| @@ -184,6 +184,81 @@ void prio_list_print(struct prio_list * list) { | |||||||
|     printf("\n"); |     printf("\n"); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | struct remtime_list { | ||||||
|  |     struct pinfo ** proc; | ||||||
|  |     int length; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Pops and returns the last element (shortest remaining time) of the given remtime list | ||||||
|  |  *  | ||||||
|  |  * Returns NULL if the list is empty | ||||||
|  |  */ | ||||||
|  | struct pinfo * remtime_list_pop(struct remtime_list * list) { | ||||||
|  |     if (list->length == 0) { | ||||||
|  |         return NULL; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     list->length--; | ||||||
|  |     return *(list->proc + list->length); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Adds the given process in the remtime list at | ||||||
|  |  * the appropriate place (according to its remaining time) | ||||||
|  |  */ | ||||||
|  | void remtime_list_add(struct remtime_list * list, struct pinfo * proc) { | ||||||
|  |     struct pinfo * proc2; | ||||||
|  |     int idx = 0; | ||||||
|  |  | ||||||
|  |     // Find first element (from the right) with longer remaining time | ||||||
|  |     for (int i=list->length-1; i>=0; i--) { | ||||||
|  |         proc2 = *(list->proc + i); | ||||||
|  |         if (proc2->remaining_time > proc->remaining_time) { | ||||||
|  |             idx = i+1; | ||||||
|  |             break; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     // Shift elements | ||||||
|  |     for (int j=list->length-1; j>=idx; j--) { | ||||||
|  |         *(list->proc + j + 1) = *(list->proc + j); | ||||||
|  |     } | ||||||
|  |     *(list->proc + idx) = proc; | ||||||
|  |     list->length++; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Creates a new remtime list | ||||||
|  |  */ | ||||||
|  | struct remtime_list * create_remtime_list(int max_size) { | ||||||
|  |     struct pinfo ** processes = (struct pinfo **) malloc(sizeof(struct pinfo *) * max_size); | ||||||
|  |     struct remtime_list * list = (struct remtime_list *) malloc(sizeof(struct remtime_list)); | ||||||
|  |     list->length = 0; | ||||||
|  |     list->proc = processes; | ||||||
|  |     return list; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Returns the last element (shortest remaining time) of the given remtime list | ||||||
|  |  */ | ||||||
|  | struct pinfo * remtime_list_last(struct remtime_list * list) { | ||||||
|  |     return *(list->proc + list->length - 1); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Prints the pids of the processes in the given remtime list | ||||||
|  |  */ | ||||||
|  | void remtime_list_print(struct remtime_list * list) { | ||||||
|  |     printf("queue: "); | ||||||
|  |     for (int i=0; i < list->length; i++) { | ||||||
|  |         if (i != 0) { | ||||||
|  |             printf(", "); | ||||||
|  |         } | ||||||
|  |         printf("%d", (*(list->proc + i))->id); | ||||||
|  |     } | ||||||
|  |     printf("\n"); | ||||||
|  | } | ||||||
|  |  | ||||||
| struct perf_info schedule_Pr(struct pinfo * processes) { | struct perf_info schedule_Pr(struct pinfo * processes) { | ||||||
|     struct perf_info perf = {0, 0, 0}; |     struct perf_info perf = {0, 0, 0}; | ||||||
|  |  | ||||||
| @@ -305,67 +380,203 @@ void compute_waiting_time(struct pinfo * processes) { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| struct perf_info schedule_RR(struct pinfo *processes) { // Déclaration de la fonction schedule_RR qui prend un pointeur vers une liste de processus | struct perf_info schedule_RR(struct pinfo *processes) { | ||||||
|     struct perf_info perf = {0, 0, 0}; // Initialisation de la structure de performance avec des valeurs à zéro |     struct perf_info perf = {0, 0, 0}; // Initialisation de la structure de performance | ||||||
|     int current_time = 0; // Variable pour suivre le temps actuel |  | ||||||
|     int finished_processes = 0; // Compteur pour le nombre de processus terminés |  | ||||||
|  |  | ||||||
|     while (finished_processes < MAX_PROCESSES) { // Boucle principale jusqu'à ce que tous les processus soient terminés |     // Conversion de la liste chaînée en array | ||||||
|         int process_found = 0; // Indicateur pour savoir si un processus prêt a été trouvé |     int N = n_processes(processes); | ||||||
|         struct pinfo *current_process = processes; // Pointeur pour parcourir la liste des processus |     struct pinfo ** proc_list = (struct pinfo **) malloc(sizeof(struct pinfo *) * N); | ||||||
|  |     struct pinfo * p = processes; | ||||||
|         while (current_process != NULL) { // Boucle pour parcourir tous les processus |     int i = 0; | ||||||
|             // Vérifiez si le processus est prêt à s'exécuter |     while (p != NULL) { | ||||||
|             if (current_process->state != FINISHED && current_process->arrival_time <= current_time) { // Vérifie si le processus n'est pas fini et est arrivé |         proc_list[i] = p; | ||||||
|                 process_found = 1; // Un processus prêt à s'exécuter a été trouvé |         p = p->next_pinfo; | ||||||
|  |         i++; | ||||||
|                 int time_slice = (current_process->remaining_time < RR_QUANTUM) ? current_process->remaining_time : RR_QUANTUM; // Calcule la tranche de temps à exécuter |  | ||||||
|  |  | ||||||
|                 // Simuler l'exécution |  | ||||||
|                 current_time += time_slice; // Incrémente le temps actuel par la tranche de temps |  | ||||||
|                 current_process->remaining_time -= time_slice; // Diminue le temps restant du processus |  | ||||||
|  |  | ||||||
|                 // Calculer les temps d'attente pour les autres processus |  | ||||||
|                 struct pinfo *other_process = processes; // Pointeur pour parcourir à nouveau la liste des processus |  | ||||||
|                 while (other_process != NULL) { // Boucle pour parcourir tous les autres processus |  | ||||||
|                     if (other_process->state != FINISHED && other_process != current_process && other_process->arrival_time <= current_time) { // Vérifie si l'autre processus est prêt |  | ||||||
|                         other_process->wait_time += time_slice; // Augmente le temps d'attente des autres processus |  | ||||||
|                     } |  | ||||||
|                     other_process = other_process->next_pinfo; // Passe au processus suivant |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|                 // Gérer les statistiques de préemption |     struct pinfo *current_process = processes; | ||||||
|                 if (current_process->remaining_time == 0) { // Vérifie si le processus est terminé |     int current_time = 0; // Temps actuel | ||||||
|                     current_process->state = FINISHED; // Met à jour l'état du processus à fini |     int finished_processes = 0; // Compteur de processus terminés | ||||||
|                     finished_processes++; // Incrémente le compteur de processus terminés |     int running_processes = 0; | ||||||
|                     current_process->turnaround_time = current_time - current_process->arrival_time; // Calcule le temps de turnaround |     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; | ||||||
|  |                     } | ||||||
|  |                     printf("Process %d is now ready\n", p->id); | ||||||
|  |                     printf("Running processes: %d\n", running_processes); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             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 (finished_processes < N && running_processes == 0) { | ||||||
|  |             current_time++; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     perf.total_time = current_time; | ||||||
|  |     perf.total_time_ctxt_switch = perf.total_nr_ctxt_switch * CNTXT_SWITCH; | ||||||
|  |     return perf; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | struct perf_info schedule_SRTF(struct pinfo *processes) { | ||||||
|  |     struct perf_info perf = {0, 0, 0}; | ||||||
|  |  | ||||||
|  |     int current_time = 0; | ||||||
|  |     struct pinfo * current = NULL; | ||||||
|  |     struct pinfo * next = processes; | ||||||
|  |  | ||||||
|  |     int N = n_processes(processes); | ||||||
|  |     printf("N = %d\n", N); | ||||||
|  |  | ||||||
|  |     struct remtime_list * queue = create_remtime_list(N); | ||||||
|  |      | ||||||
|  |     int finished = 0; | ||||||
|  |  | ||||||
|  |     while (finished != N) { | ||||||
|  |         printf("\nCurrent time: %d / ", current_time); | ||||||
|  |         if (current != NULL) { | ||||||
|  |             printf("Current: %d / ", current->id); | ||||||
|         } else { |         } else { | ||||||
|                     // Incrémenter le nombre de préemptions |             printf("Current: none / "); | ||||||
|                     current_process->nb_time_pre_empted++; // Incrémente le compteur de préemptions pour le processus actuel |         } | ||||||
|                     perf.total_nr_ctxt_switch++; // Incrémente le nombre total de commutations de contexte |         if (next != NULL) { | ||||||
|  |             printf("Next: %d\n", next->id); | ||||||
|  |         } else { | ||||||
|  |             printf("Next: none\n"); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|                 // Débogage : Afficher les informations du processus |         if (current == NULL) { | ||||||
|                 printf("Processus %d: remaining_time=%d, nb_time_pre_empted=%d\n", // Affiche les informations de débogage pour le processus actuel |             printf("No running process: running %d\n", next->id); | ||||||
|                        current_process->id, current_process->remaining_time, current_process->nb_time_pre_empted); |             current = next; | ||||||
|  |             current->state = RUNNING; | ||||||
|  |             next = next->next_pinfo; | ||||||
|  |          | ||||||
|  |         } else if (next != NULL) { | ||||||
|  |             next->state = READY; | ||||||
|  |             remtime_list_print(queue); | ||||||
|  |             printf("Processing next process (%d)\n", next->id); | ||||||
|  |             int delta = next->arrival_time - current_time; | ||||||
|  |  | ||||||
|  |             // If current finished before next | ||||||
|  |             while (current != NULL && current->remaining_time <= delta) { | ||||||
|  |                 printf("  (%d) Process %d finished before next\n", current_time, current->id); | ||||||
|  |                 current->state = FINISHED; | ||||||
|  |                 current_time += current->remaining_time; | ||||||
|  |                 delta = next->arrival_time - current_time; | ||||||
|  |                 current->turnaround_time = current_time - current->arrival_time; | ||||||
|  |                 current->remaining_time = 0; | ||||||
|  |                 finished++; | ||||||
|  |                 current = remtime_list_pop(queue); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             current_process = current_process->next_pinfo; // Passe au processus suivant dans la liste |             if (current != NULL) { | ||||||
|  |                 printf("Removing time from current process (%d)\n", current->id); | ||||||
|  |                 current->remaining_time -= delta; | ||||||
|  |                 current->state = RUNNING; | ||||||
|  |                 printf("  New remaining time %d\n", current->remaining_time); | ||||||
|  |             } | ||||||
|  |             if (next->arrival_time > current_time) { | ||||||
|  |                 current_time = next->arrival_time; | ||||||
|  |             } | ||||||
|  |             next->state = READY; | ||||||
|  |              | ||||||
|  |  | ||||||
|  |             // If no running process, immediately run next process | ||||||
|  |             if (current == NULL) { | ||||||
|  |                 printf("Queue is empty, running next process %d\n", next->id); | ||||||
|  |                 current = next; | ||||||
|  |                 current->state = RUNNING; | ||||||
|  |                 next = next->next_pinfo; | ||||||
|  |  | ||||||
|  |             } else if (next->remaining_time < current->remaining_time) { | ||||||
|  |                 // Preempt current process | ||||||
|  |                 printf("Next process (%d) has shorter remaining time\n", next->id); | ||||||
|  |                 if (current->state == RUNNING) { | ||||||
|  |                     printf("  Preempting current process (%d)\n", current->id); | ||||||
|  |                     current->nb_time_pre_empted++; | ||||||
|  |                     current->remaining_time -= next->arrival_time - current_time; | ||||||
|  |                     current_time += CNTXT_SWITCH; | ||||||
|  |                     perf.total_nr_ctxt_switch++; | ||||||
|  |                 } | ||||||
|  |                 current->state = READY; | ||||||
|  |                 remtime_list_add(queue, current); | ||||||
|  |  | ||||||
|  |                 // Run process with shortest remaining time | ||||||
|  |                 current = next; | ||||||
|  |                 current->state = RUNNING; | ||||||
|  |                 next = next->next_pinfo; | ||||||
|  |             } else { | ||||||
|  |                 printf("Adding next process (%d) to list\n", next->id); | ||||||
|  |                 remtime_list_add(queue, next); | ||||||
|  |                 next = next->next_pinfo; | ||||||
|  |             } | ||||||
|  |             if (current != NULL) { | ||||||
|  |                 current->state = RUNNING; | ||||||
|             } |             } | ||||||
|  |  | ||||||
|         if (!process_found) { // Vérifie si aucun processus prêt n'a été trouvé |  | ||||||
|             // Aucun processus prêt, avancer le temps |  | ||||||
|             current_time++; // Incrémente le temps actuel si aucun processus n'est prêt |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|              |              | ||||||
|     perf.total_time = current_time; // Enregistre le temps total écoulé dans la structure de performance |         } else { | ||||||
|     return perf; // Renvoie la structure de performance |             printf("No new processes, emptying queue\n"); | ||||||
|  |             while (current != NULL) { | ||||||
|  |                 printf("Completing process %d\n", current->id); | ||||||
|  |                 current->state = FINISHED; | ||||||
|  |                 current_time += current->remaining_time; | ||||||
|  |                 current->turnaround_time = current_time - current->arrival_time; | ||||||
|  |                 current->remaining_time = 0; | ||||||
|  |                 finished++; | ||||||
|  |                 current = remtime_list_pop(queue); | ||||||
|  |                 if (current != NULL) { | ||||||
|  |                     current->state = RUNNING; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     perf.total_time = current_time; | ||||||
|  |     perf.total_time_ctxt_switch = perf.total_nr_ctxt_switch * CNTXT_SWITCH; | ||||||
|  |  | ||||||
|  |     return perf; | ||||||
| } | } | ||||||
|  |  | ||||||
| void write_file(struct pinfo * process, struct perf_info * perf) { | void write_file(struct pinfo * process, struct perf_info * perf) { | ||||||
|     FILE *myStream_execution = fopen("executionRR1.csv", "w"); |     FILE *myStream_execution = fopen("executionRR.csv", "w"); | ||||||
|     FILE *myStream_performance = fopen("performanceRR1.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"); | ||||||
| @@ -456,10 +667,13 @@ int main() { | |||||||
|     //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); | ||||||
|  |  | ||||||
|     compute_waiting_time(processes); |     compute_waiting_time(processes); | ||||||
|  |  | ||||||
|     write_file(processes, &perf); |     write_file(processes, &perf); | ||||||
|  |     print_processes(processes); | ||||||
|  |     print_perf(&perf); | ||||||
|  |  | ||||||
|     free_processes(processes); |     free_processes(processes); | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										5
									
								
								tasks RR.csv
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								tasks RR.csv
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | |||||||
|  | 1 0 3 3 | ||||||
|  | 2 1 5 2 | ||||||
|  | 3 3 2 1 | ||||||
|  | 4 9 5 3 | ||||||
|  | 5 12 5 1 | ||||||
| 
 | 
		Reference in New Issue
	
	Block a user