Question
I am working on a Round Robin scheduler with time quanta of 4 for an OS class currently. The only file I am editing is
I am working on a Round Robin scheduler with time quanta of 4 for an OS class currently. The only file I am editing is the scheduler file. I believe I implemented the linked list correctly, but it seems as though I am adding processes back to the list incorrectly(aka my round robin implementation is wrong). When the simulation is run, It executes each process for 4 time units but doesnt seem to add them back correctly. Only the last process executes fully. I feel like this should be handled correctly when I look at the simulation file, yet it is not. Any input or noticed problems would be helpful even if it is not a full fix! Thank you.
//HEADER FILE_ FOR SCHEDULER-----------------------------
#ifndef _schedule_h_ #define _schedule_h_
typedef struct { int pid; int priority; } PCB;
void init(); int addProcess(PCB* process);
PCB* nextProcess(int *time); int hasProcess();
#endif
//----------------SCHEDULER FILE----------------------
#include "schedule.h"
#include
#include
/**
* Function to initialize any global variables for the scheduler.
*/
struct node{
PCB* proc;
struct node* next;
};
struct node* root;
struct node* cur;
void init(){
root=NULL;
cur = root;
}
/**
* Function to add a process to the scheduler
* @Param process - Pointer to the process control block for the process that
* needs to be scheduled. PCB is defined in the header.
* @return true/false response for if the addition was successful
*/
int addProcess(PCB* process){
if(root==NULL){
root = (struct node*) malloc( sizeof(struct node));
cur = root;
}
else{
cur->next = (struct node*) malloc( sizeof(struct node));
cur = cur->next;
}
cur->proc = process;
return 0;
}
/**
* Function to get the next process from the scheduler
* @Param time - pass by reference variable to store the quanta of time
* the scheduled process should run for
* @Return returns pointer to process control block that needs to be executed
* returns NULL if there is no process to be scheduled.
*/
PCB* nextProcess(int *time){
cur=root;
//if(!(cur->proc->pid<0))
*time=4;
if(cur!=NULL){
PCB* temp_proc;
temp_proc=cur->proc;
cur=root;
if(root->next!=NULL)
root=root->next;
else
root=NULL;
free(cur);
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(root==NULL)
return 0;
else
return 1;
}
//------------------SIMULATION FILE (CANNOT EDIT) -----------------------------
#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 = NULL; int time = 0; while(hasProcess()){ process = nextProcess(&time); if(!process){ printf("NULL Process, something went wrong in your code. "); exit(1); } for(;time>0;time--){ printf("Process %d executed ", process->pid); processes[process->pid]--; if(processes[process->pid]<0){ printf("Process %d Finished ", process->pid); break; } } if(processes[process->pid]>=0){ addProcess(process); } }
exit(0); //control never reaches here }
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