Skip to content

Commit

Permalink
fix: fixing doublingCapacity in minheap
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelruhmanSamy authored and amir-kedis committed Apr 27, 2024
1 parent 4187a1b commit 502fbf9
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 62 deletions.
28 changes: 16 additions & 12 deletions code/minHeap.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
min_heap *createMinHeap(int (*comp)(void *, void *)) {
min_heap *newHeap = malloc(sizeof(*newHeap));
newHeap->arr = malloc(sizeof(void *));
newHeap->capacity = 10;
newHeap->capacity = 1;
newHeap->size = 0;
newHeap->compare = comp;

Expand All @@ -24,8 +24,8 @@ min_heap *createMinHeap(int (*comp)(void *, void *)) {
*/
void insertMinHeap(min_heap **heap, void *element) {
// if(DEBUG){
// printf("entered insertion safely\n");
// printHeap((*heap));
printf("entered insertion safely\n");
printHeap((*heap));
// }
//
// FIXME: something here is not working size increae then decrease suddenly
Expand All @@ -38,6 +38,8 @@ void insertMinHeap(min_heap **heap, void *element) {
(*heap)->size++;

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

printHeap(*heap);
}

/**
Expand All @@ -55,6 +57,7 @@ void *extractMin(min_heap *heap) {
heap->arr[heap->size] = 0;
minHeapify(heap, 0);

printHeap(heap);
return minElement;
}

Expand Down Expand Up @@ -119,7 +122,10 @@ void minHeapify(min_heap *heap, int ind) {
* - copying data
* - freeing memory of the old heap
*/
min_heap **doubleCapacity(min_heap *heap) {
min_heap **doubleCapacity(min_heap* heap) {
printf("Entered doubleCapacity with the following heap:!!\n");
printHeap(heap);

min_heap *newHeap = malloc(sizeof(*newHeap));
newHeap->capacity = 2 * heap->capacity;
newHeap->arr = calloc(newHeap->capacity, sizeof(void *));
Expand All @@ -130,11 +136,9 @@ min_heap **doubleCapacity(min_heap *heap) {

min_heap **returnval = &newHeap;

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

printf("doubling occurred!!\n");
printHeap(*returnval);
printf("Exiting doubleCapcity\n");
return returnval;
}

Expand All @@ -146,7 +150,7 @@ void swap(void **arr, int ind1, int ind2) {
}

void printHeap(min_heap *heap) {
// printf("=============================\n");
// printf("capacity: %d \nsize: %d\n", (int)heap->capacity, (int)heap->size);
// printf("=============================\n");
printf("=============================\n");
printf("capacity: %d, size: %d\n", (int)heap->capacity, (int)heap->size);
printf("=============================\n");
}
104 changes: 81 additions & 23 deletions code/minHeapTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,96 @@ int compare(void* a , void* b){

}

int compareProcesses(void* p1 , void* p2){

if(((process_t*)p1)->priority > ((process_t*)p2)->priority)
return 1;
if(((process_t*)p1)->priority < ((process_t*)p2)->priority)
return -1;

return 0;
}

void initializeProcess(process_t* p ,int at , int bt , int id , int pri ){
p->AT = at;
p->BT = bt;
p->id = id;
p->priority = pri;
}


int main(int argc , char* argv[]){

min_heap* newHeap = createMinHeap(&compare);
min_heap** newHeapRef = &newHeap;

insertMinHeap(newHeapRef , (void*)5);
insertMinHeap(newHeapRef , (void*)3);
insertMinHeap(newHeapRef , (void*)6);

printf("currentMin without extraction: %d \n" , (newHeap)->arr[0]);
printf("currentMin: %d \n" , (int*)extractMin(newHeap));

insertMinHeap(newHeapRef , (void*)9);
insertMinHeap(newHeapRef , (void*)2);
min_heap* newHeap = createMinHeap(compareProcesses);

process_t p1;
process_t p2;
process_t p3;
process_t p4;

printf("currentMin without extraction: %d \n" , (newHeap)->arr[0]);
printf("currentMin: %d \n" , (int*)extractMin(newHeap));
initializeProcess(&p1 , 5 , 6 ,1 , 4);
initializeProcess(&p2 , 7 , 10 , 2 , 2);
initializeProcess(&p3 , 7 , 10 , 3 , 8);
initializeProcess(&p4 , 7 , 10 , 4 , 1);

printf("size = %d\n", *(int*) &newHeap->size);

insertMinHeap(&newHeap , &p3);
printf("size = %d\n", *(int*) &newHeap->size);

insertMinHeap(&newHeap , &p1);
printf("size = %d\n", *(int*) &newHeap->size);

insertMinHeap(&newHeap , &p4);
printf("size = %d\n", *(int*) &newHeap->size);

insertMinHeap(&newHeap , &p2);
printf("size = %d\n", *(int*) &newHeap->size);


printf("priority of the currentMin: %d \n" , ((process_t*)extractMin(newHeap))->priority);
printf("size = %d\n", *(int*) &newHeap->size);

printf("priority of the currentMin: %d \n" , ((process_t*)extractMin(newHeap))->priority);
printf("size = %d\n", *(int*) &newHeap->size);

printf("currentMin without extraction: %d \n" , (newHeap)->arr[0]);
printf("currentMin: %d \n" , (int*)extractMin(newHeap));

printf("priority of the currentMin: %d \n" , ((process_t*)extractMin(newHeap))->priority);
printf("size = %d\n", *(int*) &newHeap->size);

printf("currentMin without extraction: %d \n" , (newHeap)->arr[0]);
printf("currentMin: %d \n" , (int*)extractMin(newHeap));
printf("priority of the currentMin: %d \n" , ((process_t*)extractMin(newHeap))->priority);
printf("size = %d\n", *(int*) &newHeap->size);

printf("currentMin without extraction: %d \n" , (newHeap)->arr[0]);
printf("currentMin: %d \n" , (int*)extractMin(newHeap));

printf("currentMin without extraction: %d \n" , (newHeap)->arr[0]);
printf("currentMin: %d \n" , (int*)extractMin(newHeap));

//Testing integers
/*
insertMinHeap(newHeapRef , (void*)5);
insertMinHeap(newHeapRef , (void*)3);
insertMinHeap(newHeapRef , (void*)6);
printf("currentMin without extraction: %d \n" , (newHeap)->arr[0]);
printf("currentMin: %d \n" , (int*)extractMin(newHeap));
insertMinHeap(newHeapRef , (void*)9);
insertMinHeap(newHeapRef , (void*)2);
printf("currentMin without extraction: %d \n" , (newHeap)->arr[0]);
printf("currentMin: %d \n" , (int*)extractMin(newHeap));
printf("currentMin without extraction: %d \n" , (newHeap)->arr[0]);
printf("currentMin: %d \n" , (int*)extractMin(newHeap));
printf("currentMin without extraction: %d \n" , (newHeap)->arr[0]);
printf("currentMin: %d \n" , (int*)extractMin(newHeap));
printf("currentMin without extraction: %d \n" , (newHeap)->arr[0]);
printf("currentMin: %d \n" , (int*)extractMin(newHeap));
printf("currentMin without extraction: %d \n" , (newHeap)->arr[0]);
printf("currentMin: %d \n" , (int*)extractMin(newHeap));
*/

return 0;
}

48 changes: 24 additions & 24 deletions code/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +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 (*algorithm)(void *readyQ, process_t *newProcess, int *rQuantem);
int (*algorithm)(void **readyQ, process_t *newProcess, int *rQuantem);

switch (schType) {
case HPF:
Expand Down Expand Up @@ -112,7 +112,7 @@ void schedule(scheduler_type schType, int quantem, int gen_msgQID) {
newProcess = createProcess(&process);
}

if (!algorithm(readyQ, newProcess, &rQuantem) && !processesFlag)
if (!algorithm(&readyQ, newProcess, &rQuantem) && !processesFlag)
break;
if (rQuantem <= 0)
rQuantem = quantem;
Expand Down Expand Up @@ -162,20 +162,20 @@ int compareSRTN(void *e1, void *e2) {
*
* Return: 0 if no process is no the system, 1 otherwise
*/
int HPFScheduling(void *readyQueue, process_t *process, int *rQuantem) {
min_heap *readyQ = (min_heap *)readyQueue;
int HPFScheduling(void **readyQueue, process_t *process, int *rQuantem) {
min_heap **readyQ = (min_heap **)readyQueue;
(void)rQuantem;

if (process)
insertMinHeap(&readyQ, process);
insertMinHeap(readyQ, process);

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

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

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

Expand All @@ -188,27 +188,27 @@ 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;
int SRTNScheduling(void **readyQueue, process_t *process, int *rQuantem) {
min_heap **readyQ = (min_heap**)readyQueue;
process_t *newScheduledProcess = NULL;
(void)rQuantem;

if (process)
insertMinHeap(&readyQ, process);
insertMinHeap(readyQ, process);

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

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

if (!currentProcess) {
newScheduledProcess = (process_t *)extractMin(readyQ);
newScheduledProcess = (process_t *)extractMin(*readyQ);
contextSwitch(newScheduledProcess);
} else if (getMin(readyQ) &&
compareSRTN(getMin(readyQ), currentProcess) < 0) {
} else if (getMin(*readyQ) &&
compareSRTN(getMin(*readyQ), currentProcess) < 0) {
preemptProcess(currentProcess);
newScheduledProcess = (process_t *)extractMin(readyQ);
insertMinHeap(&readyQ, currentProcess);
newScheduledProcess = (process_t *)extractMin(*readyQ);
insertMinHeap(readyQ, currentProcess);
currentProcess = NULL;
contextSwitch(newScheduledProcess);
}
Expand All @@ -228,20 +228,20 @@ int SRTNScheduling(void *readyQueue, process_t *process, int *rQuantem) {
*
* Return: 0 if no process is no the system, 1 otherwise
*/
int RRScheduling(void *readyQueue, process_t *process, int *rQuantem) {
queue *readyQ = (queue *)readyQueue;
int RRScheduling(void **readyQueue, process_t *process, int *rQuantem) {
queue **readyQ = (queue **)readyQueue;

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

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

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

(*rQuantem)--;
if (*rQuantem <= 0) {
contextSwitch((process_t *)pop(readyQ));
contextSwitch((process_t *)pop(*readyQ));
}

return 1;
Expand Down
6 changes: 3 additions & 3 deletions code/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ void clearSchResources(int signum);
void schedule(scheduler_type schType, int quantem, int gen_msgQID);
int compareHPF(void *e1, void *e2);
int compareSRTN(void *e1, void *e2);
int HPFScheduling(void *readyQueue, process_t *process, int *rQuantem);
int SRTNScheduling(void *readyQueue, process_t *process, int *rQuantem);
int RRScheduling(void *readyQueue, process_t *process, int *rQuantem);
int HPFScheduling(void **readyQueue, process_t *process, int *rQuantem);
int SRTNScheduling(void **readyQueue, process_t *process, int *rQuantem);
int RRScheduling(void **readyQueue, process_t *process, int *rQuantem);
void contextSwitch(process_t *newProcess);

//===============================
Expand Down

0 comments on commit 502fbf9

Please sign in to comment.