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

Preempting Start #9

Merged
merged 3 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions code/clk.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)++;
}
Expand Down
10 changes: 9 additions & 1 deletion code/headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
11 changes: 5 additions & 6 deletions code/process_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);

Expand Down
49 changes: 49 additions & 0 deletions code/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ int main(int argc, char *argv[]) {

signal(SIGINT, clearSchResources);
signal(SIGTERM, clearSchResources);

if (atexit(cleanUpScheduler) != 0) {
perror("atexit");
exit(1);
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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;
}
}
10 changes: 10 additions & 0 deletions code/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);