Skip to content

Commit

Permalink
Merge pull request #13 from AhmedHamed3699/Scheduling
Browse files Browse the repository at this point in the history
Scheduling
  • Loading branch information
AbdelruhmanSamy authored Apr 23, 2024
2 parents 2cd3a14 + ab7fb0a commit c704d40
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 192 deletions.
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

Expand Down
106 changes: 54 additions & 52 deletions code/minHeap.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@

/**
* @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;
}

/**
Expand Down Expand Up @@ -77,41 +76,45 @@ void decreaseKey(min_heap* heap ,int ind){
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;
}
}

/**
* 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
Expand All @@ -138,18 +141,17 @@ min_heap** doubleCapacity(min_heap *heap)
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");
}
42 changes: 23 additions & 19 deletions code/minHeap.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
#pragma once
#include "structs.h"
// #include "headers.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);
34 changes: 13 additions & 21 deletions code/process_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "process_generator.h"
#include "headers.h"
#include <math.h>

/**
* main - The main function of the process generator.
Expand All @@ -20,7 +21,7 @@
int main(int argc, char *argv[]) {
queue *processes;
scheduler_type schedulerType;
int quantum;
int quantum = 0;
pid_t schedulerPid, clockPid;
int msgQID;

Expand All @@ -38,7 +39,8 @@ int main(int argc, char *argv[]) {

getInput(&schedulerType, &quantum);

createSchedulerAndClock(&schedulerPid, &clockPid, (int)schedulerType);
createSchedulerAndClock(&schedulerPid, &clockPid, (int)schedulerType,
quantum);

initClk();
msgQID = initSchGenCom();
Expand Down Expand Up @@ -226,7 +228,7 @@ void printBanner() {
* @clockPid: a pointer to the clock process id
*/
void createSchedulerAndClock(pid_t *schedulerPid, pid_t *clockPid,
int schedulerType) {
int schedulerType, int quantum) {

*clockPid = fork();

Expand All @@ -249,23 +251,13 @@ void createSchedulerAndClock(pid_t *schedulerPid, pid_t *clockPid,
}

if (*schedulerPid == 0) {
char *type;

switch (schedulerType) {
case 0:
type = "0";
break;
case 1:
type = "1";
break;
case 2:
type = "2";
break;
default:
exit(-1);
}
char type[2];
char q[5];

sprintf(type, "%d", schedulerType);
sprintf(q, "%d", quantum);

char *args[] = {"./scheduler.out", type, NULL};
char *args[] = {"./scheduler.out", type, q, NULL};
execvp(args[0], args);
exit(0);
}
Expand Down Expand Up @@ -303,8 +295,8 @@ void sendProcessesToScheduler(queue *processes, int msgQID) {
printf(ANSI_PURPLE "=>GEN:Sending process with id: %d, AT: %d, BT: %d, "
"priority: %d to scheduler\n" ANSI_RESET,
process->id, process->AT, process->BT, process->priority);
// TODO: check this intial values later
process->RT = 0;
// TODO: check this initial values later
process->RT = process->BT;
process->WT = 0;
process->TA = 0;
process->LST = currentTime;
Expand Down
2 changes: 1 addition & 1 deletion code/process_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ queue *readInputFile();
void printBanner();
scheduler_type getSchedulerType();
void getInput(scheduler_type *, int *);
void createSchedulerAndClock(pid_t *, pid_t *, int);
void createSchedulerAndClock(pid_t *, pid_t *, int, int);
void sendProcessesToScheduler(queue *, int);
Loading

0 comments on commit c704d40

Please sign in to comment.