I have been trying to understand how to use a struct that is in a header file. The code looks like this:
now in my file i have one function that is setup to add given variables into a linked list using the struct. I can get the information into the struct but i don't know how to get the information to go into the other functions. here is the code i have written:Code:/** * Representation of a task in the system. */ #ifndef TASK_H #define TASK_H // representation of a task typedef struct task { char *name; int tid; int priority; int burst; } Task; #endif
and here is the code for the insert function.Code://Last Date Modified: 3/27/2020 //File Name: schedule_fcfs.c //Description: This is the algorithm that produces the first come first server schedule setup #include <stdlib.h> #include <stdio.h> #include "schedulers.h" #include "task.h" #include "list.h" #include "cpu.h" void add(char *name, int priority, int burst) { Task *taskNew = malloc(sizeof(Task)); struct node **head = malloc(sizeof(**head)); int value=0; __sync_fetch_and_add(&value,1); taskNew->name = name; taskNew->tid=value; taskNew->priority=priority; taskNew->burst=burst; insert(head, taskNew); //use insert from list.h } void pickNextTask() { struct node *head = malloc(sizeof(*head)); traverse(head); } void schedule() { struct node **head = malloc(sizeof(**head)); while(head != NULL){ int slice = taskNew->burst; run(task, slice); pickNextTask(); schedule(); } }
whenever i run this i get seg faults which is due to the information not being there but i don't know how to get it there, I also know that this code is horribly setup aswell.Code:// add a new task to the list of tasks void insert(struct node **head, Task *newTask) { // add the new task to the list struct node *newNode = malloc(sizeof(struct node)); newNode->task = newTask; newNode->next = *head; *head = newNode; }