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

fix: cpu utilization fixed #28

Merged
merged 1 commit into from
May 4, 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
28 changes: 27 additions & 1 deletion code/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
process_t *currentProcess = NULL;
perfStats stats;
int semid;

bool started;
bool terminated;
/**
* main - The main function of the scheduler.
*
Expand Down Expand Up @@ -98,6 +101,10 @@ void schedule(scheduler_type schType, int quantem, int gen_msgQID) {

quantemClk = getClk();
int lastClk = quantemClk;
started = 0;
terminated = 0;
bool WasRunning = 0;

while (1) {
currentClk = getClk();

Expand All @@ -110,14 +117,25 @@ void schedule(scheduler_type schType, int quantem, int gen_msgQID) {
printf(ANSI_GREY "========================================\n" ANSI_RESET);
printf(ANSI_BLUE "==>SCH: Current Clk = %i\n" ANSI_RESET, currentClk);

if(started || WasRunning){
stats.totalWorkingTime++;
}

started =0;
terminated =0;

if (currentProcess) {
WasRunning = 1;
int remTime = getRemTime(currentProcess);
if(remTime > 0){
printf(ANSI_BLUE "==>SCH:" ANSI_GREEN " Process %d " ANSI_BOLD
"RT = %i\n" ANSI_RESET,
currentProcess->id, remTime);
}
}
else{
WasRunning = 0;
}
}

newProcess = NULL;
Expand All @@ -138,6 +156,10 @@ void schedule(scheduler_type schType, int quantem, int gen_msgQID) {
rQuantem = quantem;
}

if(currentClk!=lastClk){

}

lastClk = currentClk;
}

Expand Down Expand Up @@ -420,6 +442,7 @@ void startProcess(process_t *process) {

process->WT = getClk() - process->AT;

started =1;
// log this
logger("started", process);
kill(process->pid, SIGCONT);
Expand Down Expand Up @@ -460,6 +483,8 @@ void resumeProcess(process_t *process) {
kill(process->pid, SIGCONT);
process->state = RUNNING;

started =1;

// log this
logger("resumed", process);
}
Expand Down Expand Up @@ -492,6 +517,8 @@ void sigUsr1Handler(int signum) {
int status;
killedProcess = wait(&status);

terminated = 1;

currentProcess->TA = getClk() - currentProcess->AT;
logger("finished", currentProcess);

Expand Down Expand Up @@ -533,7 +560,6 @@ void logger(char *msg, process_t *p) {
fprintf(logFileptr, " TA %i WTA %.2f", p->TA, WTA);
stats.WTAs[stats.numFinished] = WTA;
stats.numFinished += 1;
stats.totalWorkingTime += p->BT;
stats.totalWaitingTime += p->WT;
stats.totalWTA += WTA;
}
Expand Down
26 changes: 20 additions & 6 deletions code/scheduler.log
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
At time 3 process 3 started arr 3 total 3 remain 3 wait 0
At time 6 process 3 finished arr 3 total 3 remain 0 wait 0 TA 3 WTA 1.00
At time 6 process 2 started arr 3 total 2 remain 2 wait 3
At time 8 process 2 finished arr 3 total 2 remain 0 wait 3 TA 5 WTA 2.50
At time 8 process 1 started arr 3 total 1 remain 1 wait 5
At time 9 process 1 finished arr 3 total 1 remain 0 wait 5 TA 6 WTA 6.00
At time 6 process 1 started arr 6 total 1 remain 1 wait 0
At time 7 process 1 finished arr 6 total 1 remain 0 wait 0 TA 1 WTA 1.00
At time 16 process 2 started arr 16 total 2 remain 2 wait 0
At time 18 process 2 finished arr 16 total 2 remain 0 wait 0 TA 2 WTA 1.00
At time 18 process 3 started arr 17 total 11 remain 11 wait 1
At time 29 process 3 finished arr 17 total 11 remain 0 wait 1 TA 12 WTA 1.09
At time 29 process 4 started arr 28 total 1 remain 1 wait 1
At time 30 process 4 finished arr 28 total 1 remain 0 wait 1 TA 2 WTA 2.00
At time 30 process 5 started arr 29 total 23 remain 23 wait 1
At time 53 process 5 finished arr 29 total 23 remain 0 wait 1 TA 24 WTA 1.04
At time 53 process 8 started arr 51 total 4 remain 4 wait 2
At time 57 process 8 finished arr 51 total 4 remain 0 wait 2 TA 6 WTA 1.50
At time 57 process 6 started arr 34 total 4 remain 4 wait 23
At time 61 process 6 finished arr 34 total 4 remain 0 wait 23 TA 27 WTA 6.75
At time 61 process 10 started arr 56 total 17 remain 17 wait 5
At time 78 process 10 finished arr 56 total 17 remain 0 wait 5 TA 22 WTA 1.29
At time 78 process 9 started arr 52 total 21 remain 21 wait 26
At time 99 process 9 finished arr 52 total 21 remain 0 wait 26 TA 47 WTA 2.24
At time 99 process 7 started arr 44 total 24 remain 24 wait 55
At time 123 process 7 finished arr 44 total 24 remain 0 wait 55 TA 79 WTA 3.29
8 changes: 4 additions & 4 deletions code/scheduler.perf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CPU utilization = 66.67%
Avg WTA = 3.17
Avg Waiting = 2.67
Std WTA = 4.39
CPU utilization = 87.80%
Avg WTA = 2.12
Avg Waiting = 11.40
Std WTA = 2.86