Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RR bugs #19

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(scheduler): RR bugs are resolved
AhmedHamed3699 committed Apr 28, 2024
commit 68393b198d6d937f58deb130ec09e604de588bea
2 changes: 1 addition & 1 deletion code/Makefile
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
all: build run

build:
gcc list.c queue.c process_generator.c -o process_generator.out -lm
gcc list.c queue.c process_generator.c -o process_generator.out
gcc clk.c -o clk.out
gcc list.c queue.c minHeap.c scheduler.c -o scheduler.out
gcc list.c process.c -o process.out
6 changes: 3 additions & 3 deletions code/queue.c
Original file line number Diff line number Diff line change
@@ -53,14 +53,14 @@ void push(queue *q, void *item) {
* @q: pointer to queue
*/
void *pop(queue *q) {
d_node *n;
void *n;

if (empty(q))
return NULL;

n = getNode(q->list, 0);
n = getNode(q->list, 0)->data;
deleteNodeStart(q->list);
return n->data;
return n;
}

/**
41 changes: 23 additions & 18 deletions code/scheduler.c
Original file line number Diff line number Diff line change
@@ -78,6 +78,7 @@ void schedule(scheduler_type schType, int quantem, int gen_msgQID) {
process_t process, *newProcess;
int processesFlag = 1; // to know when to stop getting processes from gen
int rQuantem = quantem;
int quantemClk = 0, currentClk = 0;
int (*algorithm)(void **readyQ, process_t *newProcess, int *rQuantem);

switch (schType) {
@@ -90,39 +91,40 @@ void schedule(scheduler_type schType, int quantem, int gen_msgQID) {
algorithm = SRTNScheduling;
break;
case RR:
readyQ = createQueue(free);
readyQ = createQueue(freeQueueData);
algorithm = RRScheduling;
break;
default:
exit(-1);
}

int curtime = getClk();
int lasttime = curtime;
quantemClk = getClk();
while (1) {
curtime = getClk();
if (curtime == lasttime) {
lasttime = curtime;
continue;
currentClk = getClk();
if (currentClk - quantemClk >= quantem) {
quantemClk = currentClk;
rQuantem = 0;
}
newProcess = NULL;

newProcess = NULL;
if (processesFlag) {
if (getProcess(&processesFlag, gen_msgQID, &process))
newProcess = createProcess(&process);
}

if (!algorithm(&readyQ, newProcess, &rQuantem) && !processesFlag)
break;

if (rQuantem <= 0)
rQuantem = quantem;
lasttime = curtime;
}
printf(ANSI_BLUE "==>SCH: All processes are done\n" ANSI_RESET);
// TODO: remove this infinite loop
// Clean up and exit
}

void freeQueueData(void *data) { return; }

/**
* compareHPF - compare function for HPF ready queue
*
@@ -164,18 +166,19 @@ int compareSRTN(void *e1, void *e2) {
*/
int HPFScheduling(void **readyQueue, process_t *process, int *rQuantem) {
min_heap **readyQ = (min_heap **)readyQueue;
process_t *newScheduledProcess = NULL;
(void)rQuantem;

if (process)
insertMinHeap(readyQ, process);

printf("Queue dif size: %d\n", (int)(*readyQ)->size);

if (!currentProcess && !getMin(*readyQ))
return 0;

if (!currentProcess)
contextSwitch((process_t *)(extractMin(*readyQ)));
if (!currentProcess) {
newScheduledProcess = (process_t *)extractMin(*readyQ);
contextSwitch(newScheduledProcess);
}
return 1;
}

@@ -189,15 +192,13 @@ int HPFScheduling(void **readyQueue, process_t *process, int *rQuantem) {
* Return: 0 if no process is no the system, 1 otherwise
*/
int SRTNScheduling(void **readyQueue, process_t *process, int *rQuantem) {
min_heap **readyQ = (min_heap**)readyQueue;
min_heap **readyQ = (min_heap **)readyQueue;
process_t *newScheduledProcess = NULL;
(void)rQuantem;

if (process)
insertMinHeap(readyQ, process);

printf("SRTN Queue size: %d\n", (int)(*readyQ)->size);

if (!currentProcess && !getMin(*readyQ))
return 0;

@@ -230,17 +231,21 @@ int SRTNScheduling(void **readyQueue, process_t *process, int *rQuantem) {
*/
int RRScheduling(void **readyQueue, process_t *process, int *rQuantem) {
queue **readyQ = (queue **)readyQueue;
process_t *newScheduledProcess = NULL;

if (process)
push(*readyQ, process);

if (!(currentProcess) && empty(*readyQ))
return 0;

printf("Queue size: %d\n", (int)size(*readyQ));
if (!currentProcess)
contextSwitch((process_t *)pop(*readyQ));

(*rQuantem)--;
if (*rQuantem <= 0) {
preemptProcess(currentProcess);
push(*readyQ, currentProcess);
currentProcess = NULL;
contextSwitch((process_t *)pop(*readyQ));
}

1 change: 1 addition & 0 deletions code/scheduler.h
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ void clearSchResources(int signum);
// Scheduling Algorithms
//===============================
void schedule(scheduler_type schType, int quantem, int gen_msgQID);
void freeQueueData(void *data);
int compareHPF(void *e1, void *e2);
int compareSRTN(void *e1, void *e2);
int HPFScheduling(void **readyQueue, process_t *process, int *rQuantem);