diff --git a/code/clk.c b/code/clk.c index 2483967..a9bf7b5 100644 --- a/code/clk.c +++ b/code/clk.c @@ -34,6 +34,7 @@ int main(int argc, char *argv[]) { } *shmaddr = clk; /* initialize shared memory */ while (1) { + printf(ANSI_YELLOW "=>CLK:Current time: %d\n" ANSI_RESET, (*shmaddr)); sleep(1); (*shmaddr)++; } diff --git a/code/headers.h b/code/headers.h index f5ab7d9..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" @@ -39,6 +40,12 @@ #define SHKEY 300 +//=============================== +// ARGUMENTS +//=============================== +const int DEBUG = true; // set to true to enable debug prints +const int DELAY = false; // set to true to add delay + ///============================== // don't mess with this variable// int *shmaddr; @@ -55,7 +62,8 @@ void initClk() { int shmid = shmget(SHKEY, 4, 0444); while ((int)shmid == -1) { // Make sure that the clock exists - printf("Wait! The clock not initialized yet!\n"); + if (DEBUG) + printf("Wait! The clock not initialized yet!\n"); sleep(1); shmid = shmget(SHKEY, 4, 0444); } diff --git a/code/process_generator.c b/code/process_generator.c index 5b094ac..85f7363 100644 --- a/code/process_generator.c +++ b/code/process_generator.c @@ -211,9 +211,11 @@ void printBanner() { " \\/ \\/ |__| \\/ \\/ " "\\/ \n"); - sleep(1); + if (DELAY) + sleep(1); printf("Welcome to OctopusOS\n"); - sleep(1); + if (DELAY) + sleep(1); printf(ANSI_RESET); } @@ -293,15 +295,12 @@ void sendProcessesToScheduler(queue *processes, int msgQID) { continue; } - // TODO: printing current time should be the responsibility of the scheudler - // or clock - printf(ANSI_YELLOW "=>Current time: %d\n" ANSI_RESET, currentTime); if (currentTime < process->AT) { lastTime = currentTime; continue; } - printf(ANSI_PURPLE "=>Sending process with id: %d, AT: %d, BT: %d, " + printf(ANSI_PURPLE "=>GEN:Sending process with id: %d, AT: %d, BT: %d, " "priority: %d to scheduler\n" ANSI_RESET, process->id, process->AT, process->BT, process->priority); 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);