From d45300abe9e04c925e02578fed2355f0e51a2a85 Mon Sep 17 00:00:00 2001 From: amir-kedis Date: Sat, 20 Apr 2024 22:30:12 +0200 Subject: [PATCH] feat: preempting and resuming basic form --- code/headers.h | 3 ++- code/scheduler.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ code/scheduler.h | 10 ++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/code/headers.h b/code/headers.h index bb29a68..b82aff8 100644 --- a/code/headers.h +++ b/code/headers.h @@ -25,6 +25,7 @@ #define ANSI_YELLOW "\x1b[33m" #define ANSI_BLUE "\x1b[34m" #define ANSI_MAGENTA "\x1b[35m" +#define ANSI_GREY "\x1b[90m" #define ANSI_CYAN "\x1b[36m" #define ANSI_TEAL "\x1b[96m" #define ANSI_WHITE "\x1b[37m" @@ -42,7 +43,7 @@ //=============================== // ARGUMENTS //=============================== -const int DEBUG = false; // set to true to enable debug prints +const int DEBUG = true; // set to true to enable debug prints const int DELAY = false; // set to true to add delay ///============================== diff --git a/code/scheduler.c b/code/scheduler.c index cfcf21d..a1b857c 100644 --- a/code/scheduler.c +++ b/code/scheduler.c @@ -26,6 +26,7 @@ int main(int argc, char *argv[]) { signal(SIGINT, clearSchResources); signal(SIGTERM, clearSchResources); + if (atexit(cleanUpScheduler) != 0) { perror("atexit"); exit(1); @@ -124,6 +125,16 @@ void getProcesses(int gen_msgQID, d_list *processTable) { process.id); createProcess(processTable, &process); } + // FIXME: delete later just for testing + { + sleep(3); + preemptProcessByIndex(processTable, 0); + sleep(1); + resumeProcessByIndex(processTable, 0); + preemptProcessByIndex(processTable, 1); + sleep(1); + resumeProcessByIndex(processTable, 1); + } } /** @@ -205,3 +216,41 @@ void clearSchResources(int signum) { cleanUpScheduler(); exit(0); } + +/** + * preemptProcessByIndex - Preempt a process by its index in the process table + * @processTable: pointer to process table + * @index: index of the process to preempt + */ +void preemptProcessByIndex(d_list *processTable, unsigned int index) { + process_entry_t *processEntry = getNode(processTable, index)->data; + PCB_t *pcb = processEntry->PCB; + + if (DEBUG) + printf(ANSI_GREY "==>SCH: Preempting process with id = %i\n" ANSI_RESET, + processEntry->p_id); + + if (pcb->state == RUNNING) { + kill(processEntry->p_id, SIGSTOP); + pcb->state = READY; + } +} + +/** + * resumeProcessByIndex - Resume a process by its index in the process table + * @processTable: pointer to process table + * @index: index of the process to resume + */ +void resumeProcessByIndex(d_list *processTable, unsigned int index) { + process_entry_t *processEntry = getNode(processTable, index)->data; + PCB_t *pcb = processEntry->PCB; + + if (DEBUG) + printf(ANSI_GREEN "==>SCH: Resuming process with id = %i\n" ANSI_RESET, + processEntry->p_id); + + if (pcb->state == READY) { + kill(processEntry->p_id, SIGCONT); + pcb->state = RUNNING; + } +} diff --git a/code/scheduler.h b/code/scheduler.h index 285fbb5..435320e 100644 --- a/code/scheduler.h +++ b/code/scheduler.h @@ -9,3 +9,13 @@ void freeProcessEntry(void *processEntry); void createProcess(d_list *processTable, process_t *process); void cleanUpScheduler(); void clearSchResources(int signum); + +//=============================== +// Preempting Functions +//=============================== +// TODO: Handle in preempting functions restoring the process state and update +// stats +// TODO: make sure that we want to update by the index if not make a version +// with pid +void preemptProcessByIndex(d_list *processTable, unsigned int index); +void resumeProcessByIndex(d_list *processTable, unsigned int index);