From 82d9a9622b83c6f527819492bddf94439e5e7a4b Mon Sep 17 00:00:00 2001 From: AhmedHamed3699 Date: Sun, 14 Apr 2024 02:27:47 +0200 Subject: [PATCH] refactor: organize functions and header files --- code/clk.c | 4 +- code/headers.h | 52 +++++---- code/list.h | 5 +- code/process_generator.c | 24 +--- code/process_generator.h | 16 +++ code/scheduler.c | 231 ++++++++++++++++++++++----------------- code/scheduler.h | 11 ++ code/structs.h | 2 +- 8 files changed, 196 insertions(+), 149 deletions(-) create mode 100644 code/process_generator.h create mode 100644 code/scheduler.h diff --git a/code/clk.c b/code/clk.c index d8443eb..2483967 100644 --- a/code/clk.c +++ b/code/clk.c @@ -10,7 +10,7 @@ int shmid; /* Clear the resources before exit */ -void cleanup(int signum) { +void cleanUpClk(int signum) { shmctl(shmid, IPC_RMID, NULL); printf("Clock terminating!\n"); exit(0); @@ -19,7 +19,7 @@ void cleanup(int signum) { /* This file represents the system clock for ease of calculations */ int main(int argc, char *argv[]) { printf("Clock starting\n"); - signal(SIGINT, cleanup); + signal(SIGINT, cleanUpClk); int clk = 0; // Create shared memory for one integer variable 4 bytes shmid = shmget(SHKEY, 4, IPC_CREAT | 0644); diff --git a/code/headers.h b/code/headers.h index 2e26426..f5ab7d9 100644 --- a/code/headers.h +++ b/code/headers.h @@ -1,3 +1,5 @@ +#pragma once + #include #include #include @@ -12,11 +14,6 @@ #include #include #include -///============================== -// CUSTOM INCLUDES -//=============================== -#include "./queue.h" -#include "./structs.h" //=============================== // CONSTANTS @@ -38,31 +35,15 @@ #define true 1 #define false 0 -// key proj_ids #define SCH_GEN_COM 5 #define SHKEY 300 ///============================== // don't mess with this variable// -int *shmaddr; // +int *shmaddr; //=============================== -// =================================== -// ====== PROCESS GENERATOR ====== -// ====== FUNCTION DECLARATIONS ====== -// =================================== - -queue *readInputFile(); -void printBanner(); -scheduler_type getSchedulerType(); -void getInput(scheduler_type *, int *); -void createSchedulerAndClock(pid_t *, pid_t *, int); -void sendProcessesToScheduler(queue *, int); -int intiSchGenCom(); - -// =================================== - int getClk() { return *shmaddr; } /* @@ -95,7 +76,15 @@ void destroyClk(bool terminateAll) { } } +//=============================== +// CUSTOM COMMON FUNCTIONS +//=============================== + +/** + * cleanUp - Make necessary cleaning + */ void cleanUp() { + // TODO any other needed clean up destroyClk(true); killpg(getpgrp(), SIGINT); } @@ -106,7 +95,24 @@ void cleanUp() { * @signum: the signal number */ void clearResources(int signum) { - // TODO: Clears all resources in case of interruption cleanUp(); exit(0); } + +/** + * intiSchGenCom - Initializes the message queue between the scheduler and the + * process generator. + * + * return: the message queue id + */ +int initSchGenCom() { + int key = ftok("keyfiles/SCH_GEN_COM", SCH_GEN_COM); + int msgQID = msgget(key, 0666 | IPC_CREAT); + + if (msgQID == -1) { + perror("Error in creating message queue"); + exit(-1); + } + + return msgQID; +} diff --git a/code/list.h b/code/list.h index 32625cb..c096083 100644 --- a/code/list.h +++ b/code/list.h @@ -1,5 +1,4 @@ -#ifndef LIST -#define LIST +#pragma once #include @@ -46,5 +45,3 @@ int deleteNode(d_list *list, unsigned int index); void freeNode(d_list *list, d_node *node); void freeList(d_list *list); void destroyList(d_list **list); - -#endif diff --git a/code/process_generator.c b/code/process_generator.c index ba8402d..5b094ac 100644 --- a/code/process_generator.c +++ b/code/process_generator.c @@ -7,8 +7,8 @@ * ==================================================================== */ -#include "./headers.h" -#include "queue.h" +#include "process_generator.h" +#include "headers.h" /** * main - The main function of the process generator. @@ -41,7 +41,7 @@ int main(int argc, char *argv[]) { createSchedulerAndClock(&schedulerPid, &clockPid, (int)schedulerType); initClk(); - msgQID = intiSchGenCom(); + msgQID = initSchGenCom(); sendProcessesToScheduler(processes, msgQID); destroyQueue(processes); @@ -322,21 +322,3 @@ void sendProcessesToScheduler(queue *processes, int msgQID) { exit(-1); } } - -/** - * intiSchGenCom - Initializes the message queue between the scheduler and the - * process generator. - * - * return: the message queue id - */ -int intiSchGenCom() { - int key = ftok("keyfiles/SCH_GEN_COM", SCH_GEN_COM); - int msgQID = msgget(key, 0666 | IPC_CREAT); - - if (msgQID == -1) { - perror("Error in creating message queue"); - exit(-1); - } - - return msgQID; -} diff --git a/code/process_generator.h b/code/process_generator.h new file mode 100644 index 0000000..9551f27 --- /dev/null +++ b/code/process_generator.h @@ -0,0 +1,16 @@ +#pragma once + +#include "queue.h" +#include "structs.h" + +// =================================== +// ====== PROCESS GENERATOR ====== +// ====== FUNCTION DECLARATIONS ====== +// =================================== +// +queue *readInputFile(); +void printBanner(); +scheduler_type getSchedulerType(); +void getInput(scheduler_type *, int *); +void createSchedulerAndClock(pid_t *, pid_t *, int); +void sendProcessesToScheduler(queue *, int); diff --git a/code/scheduler.c b/code/scheduler.c index 7b2e23e..cfcf21d 100644 --- a/code/scheduler.c +++ b/code/scheduler.c @@ -1,27 +1,90 @@ +/* ==================================================================== + * scheduler.c + * + * scheduler is responsible for creating and managing processes + * according to a specific algorithm + * ==================================================================== + */ + +#include "scheduler.h" #include "headers.h" -#include "list.h" int msgQID; d_list *p_table = NULL; -void cleanUpScheduler() { - msgctl(msgQID, IPC_RMID, (struct msqid_ds *)0); - destroyClk(true); - if (p_table) - destroyList(&p_table); - killpg(getpgrp(), SIGINT); -} - /** - * clearResources - Clears all resources in case of interruption. + * main - The main function of the scheduler. * - * @signum: the signal number + * @argc: the number of arguments + * @argv: the arguments + * return: 0 on success, -1 on failure */ -void clearSchResources(int signum) { - cleanUpScheduler(); - exit(0); +int main(int argc, char *argv[]) { + int key, gen_msgQID, response; + scheduler_type schedulerType; + d_list *processTable = NULL; + + signal(SIGINT, clearSchResources); + signal(SIGTERM, clearSchResources); + if (atexit(cleanUpScheduler) != 0) { + perror("atexit"); + exit(1); + } + + initClk(); + + schedulerType = getScType(atoi(argv[1])); + printf(ANSI_YELLOW "==>SCH: My Scheduler Type is %i\n" ANSI_RESET, + (int)schedulerType); + + p_table = processTable = createList(freeProcessEntry); + if (!processTable) { + perror("Error while creating process table"); + exit(-1); + } + + msgQID = gen_msgQID = initSchGenCom(); + + getProcesses(gen_msgQID, processTable); + + // TODO Initialize Scheduler + // Create Ready queue & (Wait queue ??) + // Create log file + // + // TODO Create process when generator tells you it is time + // Setup COM between process and Scheduler (init msgs queue) + // + // TODO Context Switch (print to log file in start and finish) + // When SRT process comes to ready queue (by myself) + // When quantem ends (communicate with clk) + // When a process finishes (process get SIGTRM) + // When a process gets a signal (SIGKILL, SIGINT, SIGSTP, ...etc) + // The Switch + // Move old process to ready or wait or (clear after, if it has terminated) + // Save PCB if it still exist (set attributes) + // Schedule new Process (We need the algo here) + // Load PCB (set attributes) + // Tell the process to start + // + // TODO Clear After process termination + // Calculate all needed values (till now) + // Remove process from processes Table (Delete its PCB) + // + // TODO Clean everything when Scheduler finishes or if it is killed + // Calculate all needed stats + // create pref file + // kill all living processes and destroy its PCB (is this right?) + // destroy process table + // destroy clk + // Any other clean up } +/** + * getScType - gets scheduler type + * + * @schedulerType: scheduler type + * return: scheduler type enum + */ scheduler_type getScType(int schedulerType) { switch (schedulerType) { case 0: @@ -35,12 +98,51 @@ scheduler_type getScType(int schedulerType) { } } +/** + * getProcesses - gets processes from generator + * + * @gen_msgQID: msg queue with process generator + * @processTable: process table + */ +void getProcesses(int gen_msgQID, d_list *processTable) { + int response; + process_t process; + + while (1) { + response = msgrcv(gen_msgQID, &process, sizeof(process_t), 0, !IPC_NOWAIT); + + if (response == -1) { + perror("Error in receiving process from process generator"); + exit(-1); + } + + if (process.id == -1) { + printf(ANSI_YELLOW "==>SCH: Received All Processes\n" ANSI_RESET); + break; + } + printf(ANSI_BLUE "==>SCH: Received process with id = %i\n" ANSI_RESET, + process.id); + createProcess(processTable, &process); + } +} + +/** + * freeProcessEntry - process entry free function + * + * @processEntry: process entry + */ void freeProcessEntry(void *processEntry) { if (processEntry) free(((process_entry_t *)processEntry)->PCB); free(processEntry); } +/** + * createProcess - create a new process and add it to process table + * + * @processTable: pointer to process table + * @process: pointer to new process info + */ void createProcess(d_list *processTable, process_t *process) { pid_t pid; process_entry_t *processEntry; @@ -83,90 +185,23 @@ void createProcess(d_list *processTable, process_t *process) { printf(ANSI_GREEN "==>SCH: Added process to processes table\n" ANSI_RESET); } -int main(int argc, char *argv[]) { - int key, gen_msgQID, response; - scheduler_type schedulerType; - process_t tmpProcess; - d_list *processTable = NULL; - - signal(SIGINT, clearSchResources); - signal(SIGTERM, clearSchResources); - if (atexit(cleanUpScheduler) != 0) { - perror("atexit"); - exit(1); - } - - initClk(); - - schedulerType = getScType(atoi(argv[1])); - printf(ANSI_YELLOW "==>SCH: My Scheduler Type is %i\n" ANSI_RESET, - (int)schedulerType); - - p_table = processTable = createList(freeProcessEntry); - if (!processTable) { - perror("Error while creating process table"); - exit(-1); - } - - key = ftok("keyfiles/SCH_GEN_COM", SCH_GEN_COM); - msgQID = gen_msgQID = - msgget(key, 0666 | IPC_CREAT); // COM with process_generator - - if (gen_msgQID == -1) { - perror("Error in creating message queue"); - exit(-1); - } - - while (1) { - response = - msgrcv(gen_msgQID, &tmpProcess, sizeof(process_t), 0, !IPC_NOWAIT); - - if (response == -1) { - perror("Error in receiving process from process generator"); - exit(-1); - } - - if (tmpProcess.id == -1) { - printf(ANSI_YELLOW "==>SCH: Received All Processes\n" ANSI_RESET); - break; - } - printf(ANSI_BLUE "==>SCH: Received process with id = %i\n" ANSI_RESET, - tmpProcess.id); - createProcess(processTable, &tmpProcess); - } - +/** + * cleanUpScheduler - Make necessary cleaning + */ +void cleanUpScheduler() { msgctl(msgQID, IPC_RMID, (struct msqid_ds *)0); - - // TODO Initialize Scheduler - // Create Ready queue & (Wait queue ??) - // Create log file - // - // TODO Create process when generator tells you it is time - // Setup COM between process and Scheduler (init msgs queue) - // - // TODO Context Switch (print to log file in start and finish) - // When SRT process comes to ready queue (by myself) - // When quantem ends (communicate with clk) - // When a process finishes (process get SIGTRM) - // When a process gets a signal (SIGKILL, SIGINT, SIGSTP, ...etc) - // The Switch - // Move old process to ready or wait or (clear after, if it has terminated) - // Save PCB if it still exist (set attributes) - // Schedule new Process (We need the algo here) - // Load PCB (set attributes) - // Tell the process to start - // - // TODO Clear After process termination - // Calculate all needed values (till now) - // Remove process from processes Table (Delete its PCB) - // - // TODO Clean everything when Scheduler finishes or if it is killed - // Calculate all needed stats - // create pref file - // kill all living processes and destroy its PCB (is this right?) - // destroy process table - // destroy clk - // Any other clean up - destroyClk(true); + if (p_table) + destroyList(&p_table); + killpg(getpgrp(), SIGINT); +} + +/** + * clearSchResources - Clears all resources in case of interruption. + * + * @signum: the signal number + */ +void clearSchResources(int signum) { + cleanUpScheduler(); + exit(0); } diff --git a/code/scheduler.h b/code/scheduler.h new file mode 100644 index 0000000..285fbb5 --- /dev/null +++ b/code/scheduler.h @@ -0,0 +1,11 @@ +#pragma once + +#include "list.h" +#include "structs.h" + +scheduler_type getScType(int schedulerType); +void getProcesses(int gen_msgQID, d_list *processTable); +void freeProcessEntry(void *processEntry); +void createProcess(d_list *processTable, process_t *process); +void cleanUpScheduler(); +void clearSchResources(int signum); diff --git a/code/structs.h b/code/structs.h index 4cd5cb0..c10517c 100644 --- a/code/structs.h +++ b/code/structs.h @@ -1,6 +1,6 @@ // copyright 2024: Abderlrahman Samy - George Magdy - Ahmed Hamed - Amir Anwar +#pragma once -#include typedef enum scheduler_type { HPF, SRTN, RR } scheduler_type; typedef enum process_state { READY, RUNNING, BLOCKED } process_state;