From 6abe829fdb5d43779ed310d8a8c8912eaa9f7749 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 8 Oct 2024 14:54:19 +0200 Subject: [PATCH] implemented file reading --- .gitignore | 4 ++- my_tasks.csv | 9 ++++++ simulator.c | 79 +++++++++++++++++++++++++++++++++++++++++----------- tasks.csv | 19 +++++++------ 4 files changed, 85 insertions(+), 26 deletions(-) create mode 100644 my_tasks.csv diff --git a/.gitignore b/.gitignore index 1530978..fb28230 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -*.o \ No newline at end of file +*.o +simulator +.vscode \ No newline at end of file diff --git a/my_tasks.csv b/my_tasks.csv new file mode 100644 index 0000000..74349b6 --- /dev/null +++ b/my_tasks.csv @@ -0,0 +1,9 @@ +1 0 10 1 +2 7 100 3 +3 12 10 2 +4 23 100 2 +5 40 1000 3 +6 41 100 3 +7 54 100 2 +8 80 100 2 +9 90 1000 1 \ No newline at end of file diff --git a/simulator.c b/simulator.c index 0bcd6a6..b90024f 100644 --- a/simulator.c +++ b/simulator.c @@ -1,5 +1,6 @@ #include #include +#include #define RR_QUANTUM 2 #define CNTXT_SWITCH 1 @@ -29,8 +30,23 @@ struct perf_info { int total_time_ctxt_switch; }; -struct pinfo * create_process(int id, int arrival_time, int execution_time, int priority) { - struct pinfo * info = { +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("id); + printf(" Arrival time: %d\n", info->arrival_time); + printf(" Execution time: %d\n", info->execution_time); + printf(" Priority: %d\n", info->priority); + printf("}>\n"); +} + +struct pinfo create_process(int id, int arrival_time, int execution_time, int priority) { + struct pinfo info = { id, arrival_time, execution_time, @@ -43,11 +59,11 @@ struct pinfo * create_process(int id, int arrival_time, int execution_time, int return info; } -struct perf_info * schedule_FCFS(struct pinfo * processes) { +struct perf_info schedule_FCFS(struct pinfo * processes) { int current_time = 0; struct pinfo * process = processes; - struct perf_info * perf = {0, 0, 0}; + struct perf_info perf = {0, 0, 0}; while (process != NULL) { int wait_time = current_time - process->arrival_time; @@ -60,35 +76,66 @@ struct perf_info * schedule_FCFS(struct pinfo * processes) { process = process->next_pinfo; } - perf->total_time = current_time; + perf.total_time = current_time; return perf; } struct pinfo * read_file() { - // TODO + 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); + } + fclose(file); + return first; } void write_file(struct pinfo * process, struct perf_info * perf) { // TODO } -void print_perf(struct perf_info * perf) { - println("Total time: %d\n", perf->total_time); - println("Total number of context switches: %d\n", perf->total_nr_ctxt_switch); - println("Total time spent on context switching: %d\n", perf->total_time_ctxt_switch); -} - - int main() { struct pinfo * processes = read_file(); - struct perf_info * perf = schedule_FCFS(processes); + /*struct perf_info perf = schedule_FCFS(processes); - write_file(processes, perf); - print_perf(perf); + write_file(processes, &perf); + print_perf(perf);*/ return 0; } \ No newline at end of file diff --git a/tasks.csv b/tasks.csv index 74349b6..05a0604 100644 --- a/tasks.csv +++ b/tasks.csv @@ -1,9 +1,10 @@ -1 0 10 1 -2 7 100 3 -3 12 10 2 -4 23 100 2 -5 40 1000 3 -6 41 100 3 -7 54 100 2 -8 80 100 2 -9 90 1000 1 \ No newline at end of file +1 0 10 3 +2 20 40 2 +3 30 20 2 +4 60 10 3 +5 80 30 1 +6 90 20 1 +7 100 50 3 +8 130 30 2 +9 180 10 3 +10 200 60 1 \ No newline at end of file