diff --git a/code/minHeap.c b/code/minHeap.c index 29f407c..f857a20 100644 --- a/code/minHeap.c +++ b/code/minHeap.c @@ -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; @@ -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 @@ -38,6 +38,8 @@ void insertMinHeap(min_heap **heap, void *element) { (*heap)->size++; decreaseKey((*heap), (*heap)->size - 1); + + printHeap(*heap); } /** @@ -55,6 +57,7 @@ void *extractMin(min_heap *heap) { heap->arr[heap->size] = 0; minHeapify(heap, 0); + printHeap(heap); return minElement; } @@ -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 *)); @@ -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; } @@ -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"); } diff --git a/code/minHeapTest.c b/code/minHeapTest.c index d7655bb..d31dc28 100644 --- a/code/minHeapTest.c +++ b/code/minHeapTest.c @@ -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; } diff --git a/code/scheduler.c b/code/scheduler.c index 23398dc..450fc92 100644 --- a/code/scheduler.c +++ b/code/scheduler.c @@ -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: @@ -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; @@ -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; } @@ -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); } @@ -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; diff --git a/code/scheduler.h b/code/scheduler.h index 29d312c..b88eaa6 100644 --- a/code/scheduler.h +++ b/code/scheduler.h @@ -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); //===============================