Question
This is a homework assignment for an OS class. I was wondering why I am getting errors for not initializing cur in the functions when
This is a homework assignment for an OS class. I was wondering why I am getting errors for not initializing cur in the functions when it is in the init() function. Basically it is a FIFO queue scheduler and these are the functions called to add or see processes. It has other problems besides not initializing cur in each function, but this is my main concern right now.
Edit: I think it has something to do with construction?.. I have mainly used C++ before now so I am new to how this works in C
#include "schedule.h"
#include
#include
#include
struct node {
PCB proc;
struct node* next;
};
/**
* Function to initialize any global variables for the scheduler.
*/
void init(){
struct node* root; //root node
struct node* cur;//current node
root = (struct node*) malloc( sizeof(struct node));
cur = root;
cur->proc = NULL;//to avoid problems with hasproc before addproc
cur->next = NULL;//avoid problems with nextproc before addproc
}
/**
* Function to add a process to the scheduler
* @Param PCB * - pointer to the PCB for the process/thread to be added to the
* scheduler queue
* @return true/false response for if the addition was successful
*/
int addProcess(PCB *process){
cur->proc = process;
cur->next = (struct node*) malloc( sizeof(struct node));//allocate space for next node
cur->next = NULL;
cur = cur->next;//change current node
return 0;
}
/**
* Function to get the next process from the scheduler
*
* @Return returns the Process Control Block of the next process that should be
* executed, returns NULL if there are no processes
*/
PCB* nextProcess(){
if(cur->next!=NULL){
cur = cur->next;
return cur->proc;
}
else
return NULL;
}
/**
* Function that returns a boolean 1 True/0 False based on if there are any
* processes still scheduled
* @Return 1 if there are processes still scheduled 0 if there are no more
* scheduled processes
*/
int hasProcess(){
if(cur==NULL && cur->next == NULL)
return 0;
else
return 1;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started