Hi guys, hoping you can help me out a little here!
I have done C programming a bit before (basic knowledge) but I haven't done any for a while and am pretty rusty, so I apologise for that in advance! I have searched the net and my books but I think I've been staring at the code so long my brain just can't process it anymore!
The code below is part of an operating system scheduling program (runs through SIMCORE). I think the problem is on the lines that I've put "//***" afterwards. Everything compiles fine but when I run it it never terminates, just continually loops. I think the problem is that I'm assigning a pointer to another pointer, making them reference the same thing - is that right? If so, how should I keep a reference to the highest priority node?
READY_Q is a struct containing next and pcb, and pcb is a struct containing a range of things.
What I'm trying to do is find the process with the highest priority (the program does this bit fine) and then store a reference to the node containing that process and a reference to the node before it (so that I can remove it from the linked list). Of course, I have to get to the end of the linked list before I know if I have found the highest priority process.
Hopefully that makes sense, please let me know if I need to include some other information.
Basically, is the pointer thing my problem? Or is there something else that I've overlooked? Thanks for all your help!
Code:
static PCB *find_highest_priority_pcb()
{
READY_Q *highest_prior, *prev_highest_prior, *next_rdy, *prev_next_rdy; /* pointer to READY_Q */
PCB *ret_pcb;
/* initialise variables */
prev_next_rdy = NULL;
prev_highest_prior = NULL;
highest_prior = ready_q.head;
next_rdy = ready_q.head;
/* Find the highest priority process. */
int prev_priority = highest_prior->pcb->priority;
while(next_rdy != NULL){
int priority = next_rdy->pcb->priority;
if (priority > prev_priority){
highest_prior = next_rdy; //***
prev_highest_prior = prev_next_rdy; //***
prev_priority = priority;
}
next_rdy = next_rdy->next;
}
/* Remove the selected process from the ready queue (list). */
if (prev_highest_prior != NULL){
prev_highest_prior->next = highest_prior->next; //***
}
highest_prior->next = NULL;
ret_pcb = highest_prior->pcb;
free(highest_prior);
return(ret_pcb);
}