From d843d4d068fe7fa77258947fc6a78480cf5c633a Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 8 Oct 2024 15:16:41 +0200 Subject: [PATCH] fixed pointer issues --- simulator.c | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/simulator.c b/simulator.c index b90024f..07df2b6 100644 --- a/simulator.c +++ b/simulator.c @@ -42,20 +42,20 @@ void print_pinfo(struct pinfo * info) { printf(" Arrival time: %d\n", info->arrival_time); printf(" Execution time: %d\n", info->execution_time); printf(" Priority: %d\n", info->priority); + printf(" NEXT -> %p\n", info->next_pinfo); printf("}>\n"); } -struct pinfo create_process(int id, int arrival_time, int execution_time, int priority) { - struct pinfo info = { - id, - arrival_time, - execution_time, - priority, - 0, - 0, - WAITING, - NULL - }; +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->completion_time = 0; + info->state = WAITING; + info->next_pinfo = NULL; return info; } @@ -93,7 +93,7 @@ struct pinfo * read_file() { struct pinfo * first = NULL; struct pinfo * last = NULL; - struct pinfo process; + struct pinfo * process; while (fgets(line, buf_size, file)) { pid_str = strtok(line, " "); @@ -110,17 +110,18 @@ struct pinfo * read_file() { // If linked list not initialized if (first == NULL) { - first = &process; + first = process; } // If there is an element in list if (last != NULL) { - last->next_pinfo = &process; + last->next_pinfo = process; } - last = &process; + last = process; - print_pinfo(&process); + print_pinfo(process); } + free(line); fclose(file); return first; } @@ -129,13 +130,24 @@ void write_file(struct pinfo * process, struct perf_info * perf) { // TODO } +void free_processes(struct pinfo * next) { + struct pinfo * cur; + while (next != NULL) { + cur = next; + next = cur->next_pinfo; + free(cur); + } +} + 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); + + free_processes(processes); return 0; } \ No newline at end of file