Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is an assignment for an OS class (same assignment as previous question). I am attempting to add processes to a linked list and then

This is an assignment for an OS class (same assignment as previous question). I am attempting to add processes to a linked list and then return them in the nextProcess() function. However, it seems as thought the final process is able to finish but the program still attempts to grab a NULL process. (hits "NULL Process, something went wrong..." print statement after last proc finishes). It only does this on the last proc in the list and I cannot figure out why.. It is probably a slight error in my logic but I am just not seeing it. Thank you for reading! There are two files pasted below separated by a line.(//--------) The error is somewhere in the first file as the second is given as reference. Also, it may be a problem with how I am releasing memory.. I am really rusty with C sorry.

#include "schedule.h"

#include

#include

#include

/**

* Function to initialize any global variables for the scheduler.

*/

struct node {

PCB* proc;

struct node* next;

};

struct node* root; //root node

struct node* cur;//current node

void init(){

root = (struct node*) malloc( sizeof(struct node));

cur = root;

}

/**

* 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(){

cur=root;

if(cur!=NULL){

PCB* temp_proc;//stores proc temporarily

temp_proc=cur->proc;//assign value

cur = root;//free up memory of root

free(cur);

if(root->next!=NULL)

root = root->next;

return temp_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)

if(root==NULL)

return 0;

else

return 1;

}

//--------------------------------------------------------------second file below -----------------------------------------------------------------------------------

#include #include #include #include #include #include #include #include

#include "schedule.h"

// // main - The simulator's main routine // int main(int argc, char **argv){ int processes[10]; init(); int i; for(i=0;i<10;i++){ processes[i]=100; printf("Scheduled Process: %d ", i); PCB* proc = (PCB *) malloc(sizeof(PCB)); proc->pid = i; proc->priority=0; addProcess(proc); }

PCB* process = 0; while(hasProcess()){ process = nextProcess(); if(!process){ printf("NULL Process, something went wrong in your code. "); exit(1); } if(process){ for(;;){ printf("Process %d executed ", process->pid); processes[process->pid]--; if(processes[process->pid]<0){ printf("Process %d Finished ", process->pid); break; } } } }

exit(0); //control never reaches here }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

Students also viewed these Databases questions