1
0

Compare commits

...

13 Commits

14 changed files with 595 additions and 14 deletions

10
executionFCFS.csv Normal file
View File

@ -0,0 +1,10 @@
1,10,0,0
2,40,0,0
3,50,30,0
4,30,20,0
5,40,10,0
6,50,30,0
7,90,40,0
8,90,60,0
9,50,40,0
10,90,30,0
1 1 10 0 0
2 2 40 0 0
3 3 50 30 0
4 4 30 20 0
5 5 40 10 0
6 6 50 30 0
7 7 90 40 0
8 8 90 60 0
9 9 50 40 0
10 10 90 30 0

10
executionPr.csv Normal file
View File

@ -0,0 +1,10 @@
1,10,0,0
2,40,0,0
3,50,30,0
4,211,201,0
5,30,0,0
6,40,20,0
7,191,141,1
8,30,0,0
9,101,91,0
10,61,1,0
1 1 10 0 0
2 2 40 0 0
3 3 50 30 0
4 4 211 201 0
5 5 30 0 0
6 6 40 20 0
7 7 191 141 1
8 8 30 0 0
9 9 101 91 0
10 10 61 1 0

10
executionRR.csv Normal file
View File

@ -0,0 +1,10 @@
1,10,0,0
2,188,148,15
3,81,61,9
4,53,43,4
5,218,188,14
6,159,139,9
7,277,227,24
8,200,170,14
9,77,67,4
10,201,141,18
1 1 10 0 0
2 2 188 148 15
3 3 81 61 9
4 4 53 43 4
5 5 218 188 14
6 6 159 139 9
7 7 277 227 24
8 8 200 170 14
9 9 77 67 4
10 10 201 141 18

5
executionRRtest.csv Normal file
View 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
1 1 6 3 2
2 2 10 5 4
3 3 5 3 1
4 4 9 4 4
5 5 8 3 4

10
executionSRTF.csv Normal file
View 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 10 0 0
2 2 72 32 2
3 3 21 1 0
4 4 11 1 0
5 5 62 32 0
6 6 22 2 0
7 7 133 83 1
8 8 42 12 0
9 9 11 1 0
10 10 93 33 0

1
performanceFCFS.csv Normal file
View File

@ -0,0 +1 @@
290,0,0
1 290 0 0

1
performancePr.csv Normal file
View File

@ -0,0 +1 @@
291,1,1
1 291 1 1

1
performanceRR.csv Normal file
View File

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

1
performanceRRtest.csv Normal file
View File

@ -0,0 +1 @@
20,15,15
1 20 15 15

1
performanceSRTF.csv Normal file
View File

@ -0,0 +1 @@
293,3,3
1 293 3 3

11
results.csv Normal file
View File

@ -0,0 +1,11 @@
PID,Arrival Time,Execution Time,Wait Time,Turnaround Time,Remaining Time
1,0,10,6,14,0
2,20,40,545,129,0
3,30,20,123,54,0
4,60,10,42,33,0
5,80,30,709,140,0
6,90,20,295,90,0
7,100,50,2016,196,0
8,130,30,739,131,0
9,180,10,60,46,0
10,200,60,1694,143,0
1 PID Arrival Time Execution Time Wait Time Turnaround Time Remaining Time
2 1 0 10 6 14 0
3 2 20 40 545 129 0
4 3 30 20 123 54 0
5 4 60 10 42 33 0
6 5 80 30 709 140 0
7 6 90 20 295 90 0
8 7 100 50 2016 196 0
9 8 130 30 739 131 0
10 9 180 10 60 46 0
11 10 200 60 1694 143 0

View File

@ -4,10 +4,12 @@
#define RR_QUANTUM 2
#define CNTXT_SWITCH 1
int MAX_PROCESSES = 0;
enum pstate {
WAITING,
READY,
RUNNING,
FINISHED
};
@ -18,13 +20,25 @@ struct pinfo {
int priority;
int wait_time;
int completion_time;
int turnaround_time;
int remaining_time;
int nb_time_pre_empted; // Ajouté pour compter les préemptions
enum pstate state;
struct pinfo * next_pinfo;
};
int n_processes(struct pinfo * head) {
int n = 0;
while (head != NULL) {
n++;
head = head->next_pinfo;
}
return n;
}
struct perf_info {
int total_time;
int total_nr_ctxt_switch;
@ -44,7 +58,7 @@ void print_pinfo(struct pinfo * info) {
printf(" Execution time: %d\n", info->execution_time);
printf(" Priority: %d\n", info->priority);
printf(" Wait time: %d\n", info->wait_time);
printf(" Completion time: %d\n", info->completion_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");
@ -64,13 +78,15 @@ struct pinfo * create_process(int id, int arrival_time, int execution_time, int
info->execution_time = execution_time;
info->priority = priority;
info->wait_time = 0;
info->completion_time = 0;
info->turnaround_time = 0;
info->remaining_time = execution_time;
info->nb_time_pre_empted = 0; // Initialiser le nombre de préemptions
info->state = WAITING;
info->next_pinfo = NULL;
return info;
}
// Méthode de planification FCFS
struct perf_info schedule_FCFS(struct pinfo * processes) {
int current_time = 0;
struct pinfo * process = processes;
@ -83,8 +99,8 @@ struct perf_info schedule_FCFS(struct pinfo * processes) {
wait_time = 0;
}
process->wait_time = wait_time;
process->completion_time = process->execution_time + process->wait_time;
current_time = process->arrival_time + process->completion_time;
process->turnaround_time = process->execution_time + process->wait_time;
current_time = process->arrival_time + process->turnaround_time;
process = process->next_pinfo;
}
@ -93,6 +109,497 @@ struct perf_info schedule_FCFS(struct pinfo * processes) {
return perf;
}
struct prio_list {
struct pinfo ** proc;
int length;
};
/**
* Pops and returns the last element (highest priority) of the given priority list
*
* Returns NULL if the list is empty
*/
struct pinfo * prio_list_pop(struct prio_list * list) {
if (list->length == 0) {
return NULL;
}
list->length--;
return *(list->proc + list->length);
}
/**
* Adds the given process in the priority list at
* the appropriate place (according to its priority)
*/
void prio_list_add(struct prio_list * list, struct pinfo * proc) {
struct pinfo * proc2;
int idx = 0;
// Find first element (from the right) with lower priority
for (int i=list->length-1; i>=0; i--) {
proc2 = *(list->proc + i);
if (proc2->priority > proc->priority) {
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 priority list
*/
struct prio_list * create_prio_list(int max_size) {
struct pinfo ** processes = (struct pinfo **) malloc(sizeof(struct pinfo *) * max_size);
struct prio_list * list = (struct prio_list *) malloc(sizeof(struct prio_list));
list->length = 0;
list->proc = processes;
return list;
}
/**
* Returns the last element (highest priority) of the given priority list
*/
struct pinfo * prio_list_last(struct prio_list * list) {
return *(list->proc + list->length - 1);
}
/**
* Prints the pids of the processes in the given priority list
*/
void prio_list_print(struct prio_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 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 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 prio_list * queue = create_prio_list(N);
int finished = 0;
while (finished != N) {
printf("\nCurrent time: %d / ", current_time);
if (current != NULL) {
printf("Current: %d / ", current->id);
} else {
printf("Current: none / ");
}
if (next != NULL) {
printf("Next: %d\n", next->id);
} else {
printf("Next: none\n");
}
if (current == NULL) {
printf("No running process: running %d\n", next->id);
current = next;
current->state = RUNNING;
next = next->next_pinfo;
} else if (next != NULL) {
next->state = READY;
prio_list_print(queue);
printf("Processing next process (%d)\n", next->id);
// If current finished before next
while (current != NULL && current_time + current->remaining_time <= next->arrival_time) {
printf(" Process %d finished before next\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 = prio_list_pop(queue);
}
if (current != NULL) {
printf("Removing time from current process\n");
current->remaining_time -= next->arrival_time - current_time;
current->turnaround_time += next->arrival_time - current_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->priority < current->priority) {
// Preempt current process
printf("Next process (%d) has higher priority\n", next->id);
if (current->state == RUNNING) {
printf(" Preempting current process (%d)\n", current->id);
current->nb_time_pre_empted++;
current_time += CNTXT_SWITCH;
perf.total_nr_ctxt_switch++;
}
current->state = READY;
prio_list_add(queue, current);
// Run process with higher priority
current = next;
current->state = RUNNING;
next = next->next_pinfo;
} else {
printf("Adding next process (%d) to list\n", next->id);
prio_list_add(queue, next);
next = next->next_pinfo;
}
if (current != NULL) {
current->state = RUNNING;
}
} else {
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 = prio_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 compute_waiting_time(struct pinfo * processes) {
while (processes != NULL) {
processes->wait_time = processes->turnaround_time - processes->execution_time;
processes = processes->next_pinfo;
}
}
struct perf_info schedule_RR(struct pinfo *processes) {
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 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);
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 {
printf("Current: none / ");
}
if (next != NULL) {
printf("Next: %d\n", next->id);
} else {
printf("Next: none\n");
}
if (current == NULL) {
printf("No running process: running %d\n", next->id);
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);
}
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;
}
} else {
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) {
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 * read_file() {
FILE * file = fopen("tasks.csv", "r");
@ -108,6 +615,8 @@ struct pinfo * read_file() {
struct pinfo * process;
while (fgets(line, buf_size, file)) {
MAX_PROCESSES += 1;
pid_str = strtok(line, " ");
arrival_str = strtok(NULL, " ");
execution_str = strtok(NULL, " ");
@ -120,12 +629,12 @@ struct pinfo * read_file() {
atoi(prio_str)
);
// If linked list not initialized
// Si la liste n'est pas initialisée
if (first == NULL) {
first = process;
}
// If there is an element in list
// Si un élément est déjà dans la liste
if (last != NULL) {
last->next_pinfo = process;
}
@ -135,11 +644,10 @@ struct pinfo * read_file() {
}
free(line);
fclose(file);
return first;
}
void write_file(struct pinfo * process, struct perf_info * perf) {
// TODO
printf("Maximum des processus : %d\n", MAX_PROCESSES);
return first;
}
void free_processes(struct pinfo * next) {
@ -151,12 +659,19 @@ void free_processes(struct pinfo * next) {
}
}
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);
//struct perf_info perf = schedule_Pr(processes);
//struct perf_info perf = schedule_SRTF(processes);
//write_file(processes, &perf);
compute_waiting_time(processes);
write_file(processes, &perf);
print_processes(processes);
print_perf(&perf);

5
tasks RR.csv Normal file
View 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
1 1 0 3 3
2 2 1 5 2
3 3 3 2 1
4 4 9 5 3
5 5 12 5 1

BIN
tempCodeRunnerFile Executable file

Binary file not shown.