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

Scheduler setup #8

Merged
merged 3 commits into from
Apr 14, 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): Main setup for scheduler
AhmedHamed3699 committed Apr 13, 2024
commit c6058e21f154f701c9c008e3b059e8d81cad412e
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:
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
@@ -37,6 +38,9 @@
#define true 1
#define false 0

// key proj_ids
#define SCH_GEN_COM 5

#define SHKEY 300

///==============================
@@ -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();

@@ -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
68 changes: 44 additions & 24 deletions code/process_generator.c
Original file line number Diff line number Diff line change
@@ -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();

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

@@ -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");
@@ -97,7 +106,7 @@ queue *readInputFile() {
*
* return: the chosen scheduler type
*/
enum scheduler_type getSchedulerType() {
scheduler_type getSchedulerType() {
int choice;

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

@@ -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) {
@@ -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();

@@ -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);
}
@@ -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.
@@ -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);
}
}

/**
@@ -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) {
Loading