Skip to content

Commit

Permalink
feat(scheduler): Main setup for scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedHamed3699 committed Apr 13, 2024
1 parent db2f77e commit c6058e2
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 50 deletions.
6 changes: 4 additions & 2 deletions code/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
.PHONY: build all clean test

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

clean:
Expand Down
28 changes: 23 additions & 5 deletions code/headers.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h> //if you don't use scanf/printf change this include
Expand Down Expand Up @@ -37,6 +38,9 @@
#define true 1
#define false 0

// key proj_ids
#define SCH_GEN_COM 5

#define SHKEY 300

///==============================
Expand All @@ -51,10 +55,9 @@ int *shmaddr; //

queue *readInputFile();
void printBanner();
enum scheduler_type getSchedulerType();
void getInput(enum scheduler_type *, int *);
void clearResources(int);
void createSchedulerAndClock(pid_t *, pid_t *);
scheduler_type getSchedulerType();
void getInput(scheduler_type *, int *);
void createSchedulerAndClock(pid_t *, pid_t *, int);
void sendProcessesToScheduler(queue *, int);
int intiSchGenCom();

Expand Down Expand Up @@ -85,10 +88,25 @@ void initClk() {
* Input: terminateAll: a flag to indicate whether that this is the end of
* simulation. It terminates the whole system and releases resources.
*/

void destroyClk(bool terminateAll) {
shmdt(shmaddr);
if (terminateAll) {
killpg(getpgrp(), SIGINT);
}
}

void cleanUp() {
destroyClk(true);
killpg(getpgrp(), SIGINT);
}

/**
* clearResources - Clears all resources in case of interruption.
*
* @signum: the signal number
*/
void clearResources(int signum) {
// TODO: Clears all resources in case of interruption
cleanUp();
exit(0);
}
Empty file added code/keyfiles/SCH_GEN_COM
Empty file.
10 changes: 9 additions & 1 deletion code/process.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#include "headers.h"

/* Modify this file as needed*/
int remainingtime;
int remainingtime = 1;

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

signal(SIGINT, clearResources);
signal(SIGTERM, clearResources);
if (atexit(cleanUp) != 0) {
perror("atexit");
exit(1);
}

initClk();

// TODO it needs to get the remaining time from somewhere
Expand Down
68 changes: 44 additions & 24 deletions code/process_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
*/
int main(int argc, char *argv[]) {
queue *processes;
enum scheduler_type schedulerType;
scheduler_type schedulerType;
int quantum;
pid_t schedulerPid, clockPid;
int msgQID;

signal(SIGINT, clearResources);
signal(SIGTERM, clearResources);
if (atexit(cleanUp) != 0) {
perror("atexit");
exit(1);
}

printBanner();

Expand All @@ -33,14 +38,18 @@ int main(int argc, char *argv[]) {

getInput(&schedulerType, &quantum);

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

initClk();
msgQID = intiSchGenCom();

sendProcessesToScheduler(processes, msgQID);

destroyQueue(processes);

if (wait(NULL) == -1) {
perror("wait");
exit(1);
}
destroyClk(true);
}

Expand All @@ -54,7 +63,7 @@ queue *readInputFile() {
FILE *file;
char *line = NULL;
size_t len = 0;
queue *processes = createQueue();
queue *processes = createQueue(free);

printf(ANSI_YELLOW "============================" ANSI_RESET "\n");
printf(ANSI_YELLOW "|| Reading processes file ||" ANSI_RESET "\n");
Expand Down Expand Up @@ -97,7 +106,7 @@ queue *readInputFile() {
*
* return: the chosen scheduler type
*/
enum scheduler_type getSchedulerType() {
scheduler_type getSchedulerType() {
int choice;

printf(ANSI_TEAL "==========================================================="
Expand All @@ -123,7 +132,7 @@ enum scheduler_type getSchedulerType() {
case 3:
return RR;
default:
return -1;
exit(-1);
}
}

Expand All @@ -134,7 +143,7 @@ enum scheduler_type getSchedulerType() {
* @schedulerType: a pointer to the chosen scheduler type
* @quantum: a pointer to the quantum value
*/
void getInput(enum scheduler_type *schedulerType, int *quantum) {
void getInput(scheduler_type *schedulerType, int *quantum) {
*schedulerType = getSchedulerType();

if (*schedulerType == -1) {
Expand Down Expand Up @@ -214,7 +223,8 @@ void printBanner() {
* @schedulerPid: a pointer to the scheduler process id
* @clockPid: a pointer to the clock process id
*/
void createSchedulerAndClock(pid_t *schedulerPid, pid_t *clockPid) {
void createSchedulerAndClock(pid_t *schedulerPid, pid_t *clockPid,
int schedulerType) {

*clockPid = fork();

Expand All @@ -237,7 +247,23 @@ void createSchedulerAndClock(pid_t *schedulerPid, pid_t *clockPid) {
}

if (*schedulerPid == 0) {
char *args[] = {"./scheduler.out", NULL};
char *type;

switch (schedulerType) {
case 0:
type = "0";
break;
case 1:
type = "1";
break;
case 2:
type = "2";
break;
default:
exit(-1);
}

char *args[] = {"./scheduler.out", type, NULL};
execvp(args[0], args);
exit(0);
}
Expand All @@ -247,18 +273,6 @@ void createSchedulerAndClock(pid_t *schedulerPid, pid_t *clockPid) {
printf(ANSI_PURPLE "|| ==> Clock PID: %d\n" ANSI_RESET, *clockPid);
}

/**
* clearResources - Clears all resources in case of interruption.
*
* @signum: the signal number
*/
void clearResources(int signum) {
// TODO: Clears all resources in case of interruption
destroyClk(true);
killpg(getpgrp(), SIGINT);
exit(0);
}

/**
* sendProcessesToScheduler - Sends the processes to the scheduler at the
* appropriate time.
Expand Down Expand Up @@ -293,14 +307,20 @@ void sendProcessesToScheduler(queue *processes, int msgQID) {

response = msgsnd(msgQID, process, sizeof(process_t), !IPC_NOWAIT);
if (response == -1) {
fprintf(stderr,
ANSI_RED "==>Error in sending process to scheduler\n" ANSI_RESET);
perror("Error in sending process to scheduler\n");
exit(-1);
}

pop(processes);
lastTime = currentTime;
}

process->id = -1;
response = msgsnd(msgQID, process, sizeof(process_t), !IPC_NOWAIT);
if (response == -1) {
perror("Error in terminating sending processes to scheduler\n");
exit(-1);
}
}

/**
Expand All @@ -310,7 +330,7 @@ void sendProcessesToScheduler(queue *processes, int msgQID) {
* return: the message queue id
*/
int intiSchGenCom() {
int key = ftok("SCH_GEN_COM", 18);
int key = ftok("keyfiles/SCH_GEN_COM", SCH_GEN_COM);
int msgQID = msgget(key, 0666 | IPC_CREAT);

if (msgQID == -1) {
Expand Down
Loading

0 comments on commit c6058e2

Please sign in to comment.