Skip to content

Commit

Permalink
feat: preempting and resuming basic form
Browse files Browse the repository at this point in the history
  • Loading branch information
amir-kedis authored and g-magdy committed Apr 20, 2024
1 parent 856cf01 commit d45300a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
3 changes: 2 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 @@ -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

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

0 comments on commit d45300a

Please sign in to comment.