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

Scheduling #13

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat(scheduler): Create base functionality of all algorithms
AhmedHamed3699 committed Apr 23, 2024
commit d66edd82531f1a35f5e2867e891191cf676133f1
4 changes: 2 additions & 2 deletions code/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.PHONY: build all clean test

build:
gcc list.c queue.c process_generator.c -o process_generator.out
gcc list.c queue.c process_generator.c -o process_generator.out -lm
gcc clk.c -o clk.out
gcc list.c scheduler.c -o scheduler.out
gcc list.c queue.c minHeap.c scheduler.c -o scheduler.out
gcc list.c process.c -o process.out
gcc test_generator.c -o test_generator.out

202 changes: 98 additions & 104 deletions code/minHeap.c
Original file line number Diff line number Diff line change
@@ -3,142 +3,136 @@

/**
* @comp: pass a function to be used to compare between any two heap elements
return:
return:
- 1: element1 > element2
- -1: element1 < element1
- 0: element1 = element2
- 0: element1 = element2
*/
min_heap* createMinHeap(int (*comp)(void *, void *))
{
min_heap* newHeap = malloc(sizeof(*newHeap));
newHeap->arr = malloc(sizeof(void*));
newHeap->capacity = 1;
newHeap->size = 0;
newHeap->compare = comp;

return newHeap;
min_heap *createMinHeap(int (*comp)(void *, void *)) {
min_heap *newHeap = malloc(sizeof(*newHeap));
newHeap->arr = malloc(sizeof(void *));
newHeap->capacity = 1;
newHeap->size = 0;
newHeap->compare = comp;

return newHeap;
}

/**
* insert in the heap
* pass a void pointer to the element you want to insert
*/
void insertMinHeap(min_heap** heap, void * element)
{
printf("entered insertion safely\n");
printHeap((*heap));

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

(*heap)->arr[(*heap)->size] = element;
(*heap)->size++;
printf("arr[%ld]: %d\n" , (*heap)->size-1 , (int*)(*heap)->arr[(*heap)->size-1]);
decreaseKey((*heap) , (*heap)->size-1);
*/
void insertMinHeap(min_heap **heap, void *element) {
if ((*heap)->size == (*heap)->capacity)
(*heap) = *doubleCapacity((*heap));

(*heap)->arr[(*heap)->size] = element;
(*heap)->size++;
decreaseKey((*heap), (*heap)->size - 1);
}

/**
* get the min element in the heap
*/
void * extractMin(min_heap * heap)
{
printf("entered extraction!!\n");
printHeap(heap);

void* minElement = heap->arr[0];
heap->size--;
heap->arr[0] = heap->arr[heap->size];
heap->arr[heap->size]=0;
minHeapify(heap , 0);

return minElement;
*/
void *extractMin(min_heap *heap) {
void *minElement;
if (!heap || heap->size <= 0)
return NULL;

minElement = heap->arr[0];
heap->size--;
heap->arr[0] = heap->arr[heap->size];
heap->arr[heap->size] = 0;
minHeapify(heap, 0);

return minElement;
}

/**
* get the min element in the heap without extracting
*/
void *getMin(min_heap *heap) {
if (!heap || heap->size <= 0)
return NULL;
return heap->arr[0];
}

/**
* Shift-up operation, called on decreasing the priority value
*/
void decreaseKey(min_heap* heap ,int ind){
if(ind >= heap->size)
return;

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

while(parentInd >= 0 && arr[parentInd] > arr[ind]){
swap(arr , ind , parentInd);
ind = parentInd;
parentInd = (ind-1)/2;
}
void decreaseKey(min_heap *heap, int ind) {
if (ind >= heap->size)
return;

}
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;
}
}

/**
* Shift-down operation, used locally
*/
void minHeapify(min_heap * heap, int ind)
{
int chInd = 2*ind+1 , minInd = ind;

bool flag = true;
void** arr = heap->arr;

while(flag && chInd < heap->size){
if(heap->compare(arr[ind] , arr[chInd]) > 0)
minInd = chInd;


if(chInd+1 < heap->size && heap->compare(arr[minInd] , arr[chInd+1])>0)
minInd = chInd+1;

if(minInd == ind)
flag = false;
else{
swap(arr , ind , minInd);
ind = minInd;
chInd = 2*ind+1;
}
}

*/
void minHeapify(min_heap *heap, int ind) {
int chInd = 2 * ind + 1, minInd = ind;

bool flag = true;
void **arr = heap->arr;

while (flag && chInd < heap->size) {
if (heap->compare(arr[ind], arr[chInd]) > 0)
minInd = chInd;

if (chInd + 1 < heap->size &&
heap->compare(arr[minInd], arr[chInd + 1]) > 0)
minInd = chInd + 1;

if (minInd == ind)
flag = false;
else {
swap(arr, ind, minInd);
ind = minInd;
chInd = 2 * ind + 1;
}
}
}


/**
* Description: doubles the size of the array if the max capacity is reached.
* Description: doubles the size of the array if the max capacity is reached.
* used in insert function

* - initializing new heap by double capacity
* - copying data
* - freeing memory of the old heap
*/
min_heap** doubleCapacity(min_heap *heap)
{
min_heap* newHeap = malloc(sizeof(*newHeap));
newHeap->capacity = 2*heap->capacity;
newHeap->arr = calloc(newHeap->capacity , sizeof(void*));
newHeap->size = heap->size;
newHeap->compare = heap->compare;
for(int i = 0 ; i<heap->size ; i++)
newHeap->arr[i] = heap->arr[i];

printf("doubling occured!!\n");
min_heap** returnval = &newHeap;
printHeap(*returnval);
return returnval;
min_heap **doubleCapacity(min_heap *heap) {
min_heap *newHeap = malloc(sizeof(*newHeap));
newHeap->capacity = 2 * heap->capacity;
newHeap->arr = calloc(newHeap->capacity, sizeof(void *));
newHeap->size = heap->size;
newHeap->compare = heap->compare;
for (int i = 0; i < heap->size; i++)
newHeap->arr[i] = heap->arr[i];

min_heap **returnval = &newHeap;
return returnval;
}

void swap(void ** arr, int ind1, int ind2)
{
void* temp = arr[ind1];
arr[ind1] = arr[ind2];
arr[ind2] = temp;
return;
void swap(void **arr, int ind1, int ind2) {
void *temp = arr[ind1];
arr[ind1] = arr[ind2];
arr[ind2] = temp;
return;
}

void printHeap(min_heap* heap){
printf("\n=============================\n");
printf("capacity: %d \nsize: %d\narr:",heap->capacity , heap->size);
for(int i =0 ; i< heap->capacity ; i++)
printf("%d ", heap->arr[i]);
printf("\n=============================\n\n");
}
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");
}
43 changes: 24 additions & 19 deletions code/minHeap.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
#pragma once

#include <stdlib.h>
#include <stdbool.h>
#include <stdlib.h>

/**
* @list: array of data
* @size: number of elements in the heap
* @compare:
returns:
* @compare:
returns:
- 1: element1 > element2
- -1: element1 < element1
- -1: element1 < element2
- 0: element1 = element2

* Description: minimum heap structure
*/
typedef struct min_heap{
size_t capacity;
void** arr;
size_t size;
int (*compare)(void* e1, void* e2);
}min_heap;
typedef struct min_heap {
size_t capacity;
void **arr;
size_t size;
int (*compare)(void *e1, void *e2);
} min_heap;

min_heap* createMinHeap(int comp(void* , void* )); //returns pointer on the heap to start using it
void insertMinHeap(min_heap** heap , void* element); //insert new element in the heap
void* extractMin(min_heap* heap); //get the min element
void decreaseKey(min_heap* heap ,int ind); //shift-up operation, called on decreasing the priority value
void minHeapify(min_heap* heap , int ind);
min_heap** doubleCapacity(min_heap* heap);
void swap(void** arr, int ind1, int ind2);
void printHeap(min_heap* heap);
// returns pointer on the heap to start using it
min_heap *createMinHeap(int comp(void *, void *));
// insert new element in the heap
void insertMinHeap(min_heap **heap, void *element);
// get the min element
void *extractMin(min_heap *heap);
void *getMin(min_heap *heap);
// shift-up operation, called on decreasing the priority value
void decreaseKey(min_heap *heap, int ind);
void minHeapify(min_heap *heap, int ind);
min_heap **doubleCapacity(min_heap *heap);
void swap(void **arr, int ind1, int ind2);
void printHeap(min_heap *heap);
4 changes: 2 additions & 2 deletions code/process_generator.c
Original file line number Diff line number Diff line change
@@ -252,7 +252,7 @@ void createSchedulerAndClock(pid_t *schedulerPid, pid_t *clockPid,

if (*schedulerPid == 0) {
char type[2];
char q[(int)(ceil(log10(quantum)) + 2) * sizeof(char)];
char q[5];

sprintf(type, "%d", schedulerType);
sprintf(q, "%d", quantum);
@@ -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 = 0;
process->RT = process->BT;
process->WT = 0;
process->TA = 0;
process->LST = currentTime;
Loading