Skip to content

Commit

Permalink
Fixing Process Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Yahya-Ashraf-Mohamed committed Dec 24, 2022
1 parent 4c33349 commit 424ed11
Show file tree
Hide file tree
Showing 20 changed files with 1,181 additions and 92 deletions.
12 changes: 12 additions & 0 deletions Events_Log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
At time 2 process 4 started arr 17 total 20 remain 20 wait -15
At time 12 process 4 stopped arr 17 total 20 remain 10 wait -15
At time 12 process 1 started arr 2 total 10 remain 10 wait 10
At time 21 process 1 finished arr 2 total 10 remain 0 wait 10 TA 19 WTA 1.90
At time 21 process 2 started arr 5 total 4 remain 4 wait 16
At time 25 process 2 finished arr 5 total 4 remain 0 wait 16 TA 20 WTA 5.00
At time 25 process 3 started arr 7 total 3 remain 3 wait 18
At time 28 process 3 finished arr 7 total 3 remain 0 wait 18 TA 21 WTA 7.00
At time 28 process 4 resumed arr 17 total 20 remain 10 wait 1
At time 38 process 4 finished arr 17 total 20 remain 0 wait 1 TA 21 WTA 1.05
At time 38 process 4 started arr 17 total 20 remain 20 wait 21
At time 57 process 4 finished arr 17 total 20 remain 0 wait 21 TA 40 WTA 2.00
6 changes: 0 additions & 6 deletions Header_File/Event_Struct.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
==============================
|| Created By YAHYA Mohamed ||
==============================
*/

#ifndef OS_STARTER_CODE_EVENT_STRUCT_H
#define OS_STARTER_CODE_EVENT_STRUCT_H

Expand Down
6 changes: 0 additions & 6 deletions Header_File/Events_Queue.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
==============================
|| Created By YAHYA Mohamed ||
==============================
*/

// Create queue of processes
// original source (serach for:- "Doubly linked list"): http://rosettacode.org/wiki/Queue/Definition#C
#ifndef OS_STARTER_CODE_EVENTS_QUEUE_H
Expand Down
6 changes: 0 additions & 6 deletions Header_File/Message_Buffer.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
==============================
|| Created By YAHYA Mohamed ||
==============================
*/

// Message send process

#ifndef OS_STARTER_CODE_MESSAGE_BUFFER_H
Expand Down
8 changes: 1 addition & 7 deletions Header_File/Process_Heap.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
/*
==============================
|| Created By YAHYA Mohamed ||
==============================
*/

//Create Priority queue of processes
//Create Periority queue of processes
//original source: http://rosettacode.org/wiki/Priority_queue#C

#ifndef OS_STARTER_CODE_PROCESSHEAP_H
Expand Down
6 changes: 0 additions & 6 deletions Header_File/Process_Queue.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
==============================
|| Created By YAHYA Mohamed ||
==============================
*/

// Create queue of processes
// original source (serach for:- "Doubly linked list"): http://rosettacode.org/wiki/Queue/Definition#C
#ifndef OS_STARTER_CODE_PROCESS_QUEUE_H
Expand Down
6 changes: 0 additions & 6 deletions Header_File/Process_Struct.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
==============================
|| Created By YAHYA Mohamed ||
==============================
*/

// Struct for a Process

#ifndef OS_STARTER_CODE_PROCESS_H
Expand Down
6 changes: 0 additions & 6 deletions Header_File/headers.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
==============================
|| Created By YAHYA Mohamed ||
==============================
*/

#ifndef OS_STARTER_CODE_HEADERS_H
#define OS_STARTER_CODE_HEADERS_H

Expand Down
6 changes: 0 additions & 6 deletions Header_File/scheduler.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
==============================
|| Created By YAHYA Mohamed ||
==============================
*/

#ifndef OS_STARTER_CODE_SCHEDULER_H
#define OS_STARTER_CODE_SCHEDULER_H

Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
build:
gcc process_generator.c -o process_generator.out
gcc clk.c -o clk.out
gcc scheduler.c -o scheduler.out
gcc Round_Robin.c -o RR.out
gcc PHPF.c -o PHPF.out
gcc SRTN.c -o SRTN.out
gcc SJF.c -o SJF.out
gcc process.c -o process.out
gcc test_generator.c -o test_generator.out

Expand Down
123 changes: 123 additions & 0 deletions Min_Heap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include "Header_File/Min_Heap.h"

#include <stdlib.h>

#ifndef SC_HEAP_MAX
#define SC_HEAP_MAX SIZE_MAX / sizeof(struct sc_heap_data)
#endif

bool sc_heap_init(struct sc_heap *h, size_t cap)
{
void *e;
const size_t sz = cap * sizeof(struct sc_heap_data);

*h = (struct sc_heap){0};

if (cap == 0) {
return true;
}

if (cap > SC_HEAP_MAX || (e = sc_heap_malloc(sz)) == NULL) {
return false;
}

h->elems = e;
h->cap = cap;

return true;
}

void sc_heap_term(struct sc_heap *h)
{
sc_heap_free(h->elems);

*h = (struct sc_heap){
.elems = NULL,
};
}

size_t sc_heap_size(struct sc_heap *h)
{
return h->size;
}

void sc_heap_clear(struct sc_heap *h)
{
h->size = 0;
}

bool sc_heap_add(struct sc_heap *h, int64_t key, void *data)
{
size_t i, cap, m;
void *exp;

if (++h->size >= h->cap) {
cap = h->cap != 0 ? h->cap * 2 : 4;
m = cap * 2 * sizeof(*h->elems);

if (h->cap >= SC_HEAP_MAX / 2 ||
(exp = sc_heap_realloc(h->elems, m)) == NULL) {
return false;
}

h->elems = exp;
h->cap = cap;
}

i = h->size;
while (i != 1 && key < h->elems[i / 2].key) {
h->elems[i] = h->elems[i / 2];
i /= 2;
}

h->elems[i].key = key;
h->elems[i].data = data;

return true;
}

struct sc_heap_data *sc_heap_peek(struct sc_heap *h)
{
if (h->size == 0) {
return NULL;
}

// Top element is always at heap->elems[1].
return &h->elems[1];
}

struct sc_heap_data *sc_heap_pop(struct sc_heap *h)
{
size_t i = 1, child = 2;
struct sc_heap_data last;

if (h->size == 0) {
return NULL;
}

// Top element is always at heap->elems[1].
h->elems[0] = h->elems[1];

last = h->elems[h->size--];
while (child <= h->size) {
if (child < h->size &&
h->elems[child].key > h->elems[child + 1].key) {
child++;
};

if (last.key <= h->elems[child].key) {
break;
}

h->elems[i] = h->elems[child];

i = child;
child *= 2;
}

h->elems[i] = last;

return &h->elems[0];
}


16 changes: 2 additions & 14 deletions Round_Robin.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
==============================
|| Created By YAHYA Mohamed ||
==============================
*/

#include "Header_File/headers.h"
#include "Header_File/Process_Struct.h"
#include "Header_File/Process_Heap.h"
Expand Down Expand Up @@ -59,7 +53,7 @@ int main(int argc, char *argv[]) {
pause(); // Wait for the first process to arrive
unsigned int start_time = getClk(); // Get the start time

while (ProcDequeue(Process_Queue, &pCurrentProcess) || Number_Of_Process!=0) // While processes queue is not empty or their is a process that will be sent
while (ProcDequeue(Process_Queue, &pCurrentProcess) /*|| Number_Of_Process!=0*/) // While processes queue is not empty or their is a process that will be sent
{

if (isTie()) // Check if their is a tie & if yes put them in the heap
Expand All @@ -76,14 +70,13 @@ int main(int argc, char *argv[]) {
}
continue; //after handling all tie processes skip below and dequeue a new process from main queue
}

// Cretical section!
Switch_Context_Flag = 0; //turn off switching [LOCK]
Execute_Process(); //execute current process
while (!Switch_Context_Flag) //as long as this flag is set to zero keep pausing until Alarm Signal is sent
pause(); // To Avoid Busy Waiting
}

unsigned int finish_time = getClk(); // Get the finish time
Log_AllEvents(start_time, finish_time);
raise(SIGINT); // Clean & Exit
Expand Down Expand Up @@ -173,7 +166,6 @@ void Alarm_Handler(int signum)


void Execute_Process() {

// [Case 1] the process never ran before
if (pCurrentProcess->Runtime == pCurrentProcess->RemainTime)
{
Expand All @@ -184,7 +176,6 @@ void Execute_Process() {
printf("RoundRobin: Trying again...\n");
pCurrentProcess->Pid = fork();
}

// If I am child then execute the process
if (!pCurrentProcess->Pid)
{
Expand All @@ -197,7 +188,6 @@ void Execute_Process() {
}

alarm(Quanta); // Generate an alarm to raise signal when quanta is over [Call SIGALRM]

//initial wait time for the process (Start time - Arival time)
pCurrentProcess->WaitTime = getClk() - pCurrentProcess->ArrivalTime;
AddEvent(START); // Create a start event
Expand All @@ -211,9 +201,7 @@ void Execute_Process() {
perror(NULL);
return;
}

alarm(Quanta); // Generate an alarm to raise signal when quanta is over [Call SIGALRM]

pCurrentProcess->WaitTime += getClk() - pCurrentProcess->LastStop; //add the additional waiting time (Current time - Last Stop time)
AddEvent(RESUMED);
}
Expand Down
Loading

0 comments on commit 424ed11

Please sign in to comment.