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

fix: inserting in the heap bug fixedgit add . #14

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion code/headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
//===============================
// ARGUMENTS
//===============================
const int DEBUG = false; // set to true to enable debug prints
const int DEBUG = true; // set to true to enable debug prints
const int DELAY = false; // set to true to add delay

///==============================
Expand Down
38 changes: 13 additions & 25 deletions code/minHeap.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "minHeap.h"
#include "headers.h"

#include <stdbool.h>
#include <stdio.h>
/**
* @comp: pass a function to be used to compare between any two heap elements
return:
Expand All @@ -24,20 +24,19 @@ min_heap *createMinHeap(int (*comp)(void *, void *)) {
*/
void insertMinHeap(min_heap** heap, void * element)
{
if(DEBUG){
// if(DEBUG){
printf("entered insertion safely\n");
printHeap((*heap));
}
// }

if((*heap)->size == (*heap)->capacity)
(*heap) = *doubleCapacity((*heap));


(*heap)->arr[(*heap)->size] = element;
(*heap)->size++;

if(DEBUG)
printf("arr[%ld]: %d\n" , (*heap)->size-1 , (int*)(*heap)->arr[(*heap)->size-1]);


decreaseKey((*heap) , (*heap)->size-1);
}

Expand All @@ -46,10 +45,10 @@ void insertMinHeap(min_heap** heap, void * element)
*/
void * extractMin(min_heap * heap)
{
if(DEBUG){
// if(DEBUG){
printf("entered extraction!!\n");
printHeap(heap);
}
// }

void* minElement = heap->arr[0];
heap->size--;
Expand All @@ -75,15 +74,6 @@ void decreaseKey(min_heap* heap ,int ind){
ind = parentInd;
parentInd = (ind-1)/2;
}

void **arr = heap->arr;
int parentInd = (ind - 1) / 2;

while (parentInd >= 0 && heap->compare(arr[parentInd], arr[ind]) > 0) {
swap(arr, ind, parentInd);
ind = parentInd;
parentInd = (ind - 1) / 2;
}
}

/**
Expand Down Expand Up @@ -133,10 +123,10 @@ min_heap** doubleCapacity(min_heap *heap)

min_heap** returnval = &newHeap;

if(DEBUG){
// if(DEBUG){
printf("doubling occured!!\n");
printHeap(*returnval);
}
// }

return returnval;
}
Expand All @@ -149,9 +139,7 @@ void swap(void **arr, int ind1, int ind2) {
}

void printHeap(min_heap *heap) {
printf("\n=============================\n");
printf("capacity: %d \nsize: %d\narr:", (int)heap->capacity, (int)heap->size);
for (int i = 0; i < heap->capacity; i++)
printf("%d ", *(int *)heap->arr[i]);
printf("\n=============================\n\n");
printf("=============================\n");
printf("capacity: %d \nsize: %d\n", (int)heap->capacity, (int)heap->size);
printf("=============================\n");
}
2 changes: 1 addition & 1 deletion code/process_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ void sendProcessesToScheduler(queue *processes, int msgQID) {
"priority: %d to scheduler\n" ANSI_RESET,
process->id, process->AT, process->BT, process->priority);
// TODO: check this initial values later
process->RT = process->BT;
//process->RT = process->BT; //this is set inside the scheduler (shmAdd)
process->WT = 0;
process->TA = 0;
process->LST = currentTime;
Expand Down
13 changes: 9 additions & 4 deletions code/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,13 @@ int SRTNScheduling(void *readyQueue, process_t *process, int *rQuantem) {
process_t *currentProcess;

if (process) {
insertMinHeap(&readyQ, process);
currentProcess = (process_t *)getMin(readyQ);
insertMinHeap(&readyQ, (void*)process);
currentProcess = (process_t *)extractMin(readyQ);

if(DEBUG)
printf("inside SRTN, RT = %d\n" , *(currentProcess->RT));
fflush(stdout);

if (process == currentProcess)
contextSwitch();
}
Expand Down Expand Up @@ -313,8 +318,8 @@ process_t *createProcess(d_list *processTable, process_t *process) {
int shmid = initSchProShm(pid);
int* shmAdd = (int*)shmat(shmid , (void*)0 , 0);

pcb->process.RT = shmAdd;
*pcb->process.RT = process->BT;
processEntry->PCB.process->RT = shmAdd;
*processEntry->PCB.process->RT = process->BT;

if (pid == -1) {
perror("fork");
Expand Down