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

Bug Fix #26

Merged
merged 1 commit into from
May 3, 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
18 changes: 0 additions & 18 deletions .vscode/c_cpp_properties.json

This file was deleted.

24 changes: 0 additions & 24 deletions .vscode/launch.json

This file was deleted.

59 changes: 0 additions & 59 deletions .vscode/settings.json

This file was deleted.

1 change: 1 addition & 0 deletions code/clk.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void cleanUpClk(int signum) {
int main(int argc, char *argv[]) {
printf(ANSI_YELLOW "==>CLK: Clock starting\n" ANSI_RESET);
signal(SIGINT, cleanUpClk);
signal(SIGTERM, cleanUpClk);
int clk = 0;
// Create shared memory for one integer variable 4 bytes
shmid = shmget(SHKEY, 4, IPC_CREAT | 0644);
Expand Down
12 changes: 5 additions & 7 deletions code/headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

#define SHKEY 300

#define LOG_FILE "scheduler.log3"
#define PERF_FILE "scheduler.perf3"
#define LOG_FILE "scheduler.log"
#define PERF_FILE "scheduler.perf"

//===============================
// ARGUMENTS
Expand Down Expand Up @@ -97,7 +97,6 @@ void destroyClk(bool terminateAll) {
* cleanUp - Make necessary cleaning
*/
void cleanUp() {
// TODO any other needed clean up
destroyClk(true);
killpg(getpgrp(), SIGINT);
}
Expand Down Expand Up @@ -140,7 +139,7 @@ void up(int semid) {
perror("Error in up operation");
exit(-1);
} else if (DEBUG) {
printf("up operation performed sucessfully with semid = %d\n", semid);
printf("up operation performed successfully with semid = %d\n", semid);
}
}

Expand All @@ -154,7 +153,7 @@ void down(int semid) {
perror("Error in down operation");
exit(-1);
} else if (DEBUG) {
printf("Down operation performed sucessfully of semid = %d\n", semid);
printf("Down operation performed successfully of semid = %d\n", semid);
}
}

Expand All @@ -166,9 +165,8 @@ int initSchProShm(int pid) {
perror("error in creating shared memory\n");
exit(-1);
} else if (DEBUG) {
printf("shmadd created sucessfully\n");
printf("shmadd created successfully\n");
}
fflush(stdout);

return shmid;
}
27 changes: 5 additions & 22 deletions code/process.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include "headers.h"
#include "scheduler.h"
#include "structs.h"
#include <unistd.h>

int *shmAdd;
Expand All @@ -11,54 +9,39 @@ void sigTermHandler(int signum) {
perror("Error in detach\n");
} else if (DEBUG) {
printf(ANSI_GREEN
"process %d detached from memory sucessfully\n" ANSI_RESET,
"process %d detached from memory successfully\n" ANSI_RESET,
getpid());
}

if (shmctl(shmid, IPC_RMID, (struct shmid_ds *)0) == -1) {
perror("error in deleting shared memory");
}

// FIXME: Samy look at these 2 lines
// I added them because process was reaching return 0 and making the exit
// cleanup which kills the clock and so the whole app
// make sure it doesn't affect the process
printf(ANSI_GREY "==>process %d: Terminating\n" ANSI_RESET, getpid());
exit(getpid());
}

int main(int agrc, char *argv[]) {

fflush(stdout);

signal(SIGINT, clearResources);
signal(SIGTERM, sigTermHandler);
// FIXME: commented those because they kill the clock
//
// if (atexit(cleanUp) != 0) {
// perror("atexit");
// exit(1);
// }

shmid = initSchProShm(getpid());
shmAdd = (int *)shmat(shmid, (void *)0, 0);

fflush(stdout);
initClk();

printf(ANSI_TEAL "==>process %d: Started\n" ANSI_RESET, getpid());

int preTime = getClk();
int currTime = getClk();
int preTime = currTime;
while (*shmAdd > 0) {
int currTime = getClk();

if (currTime != preTime) {
preTime = currTime;

// printf(ANSI_TEAL "==>process %d: RT = %d \n" ANSI_RESET, getpid(),
// *shmAdd);
(*shmAdd)--;
}

currTime = getClk();
}

kill(getppid(), SIGUSR1);
Expand Down
18 changes: 3 additions & 15 deletions code/process_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ int main(int argc, char *argv[]) {

signal(SIGINT, clearResources);
signal(SIGTERM, clearResources);
if (atexit(cleanUp) != 0) {
perror("atexit");
exit(1);
}

printBanner();

Expand All @@ -48,13 +44,9 @@ int main(int argc, char *argv[]) {
sendProcessesToScheduler(processes, msgQID);
destroyQueue(processes);

while (wait(NULL) == -1) {
perror("wait");
exit(1);
}
// TODO: make wait correctly for the scheduler instead of busy waiting forever
wait(NULL);

destroyClk(true);
cleanUp();
}

/**
Expand Down Expand Up @@ -301,10 +293,6 @@ void sendProcessesToScheduler(queue *processes, int msgQID) {
while (!empty(processes)) {
process = (process_t *)front(processes);
currentTime = getClk();
if (currentTime == lastTime) {
lastTime = currentTime;
continue;
}

if (currentTime < process->AT) {
lastTime = currentTime;
Expand All @@ -314,7 +302,7 @@ void sendProcessesToScheduler(queue *processes, int msgQID) {
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);
// TODO: check this initial values later

process->RT = malloc(sizeof(int) * process->BT);
*process->RT = process->BT;
process->WT = 0;
Expand Down
8 changes: 3 additions & 5 deletions code/processes.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#id arrival runtime priority
1 0 1 0
2 1 2 4
3 2 11 8
4 13 1 6
5 14 6 10
1 3 1 3
2 3 2 2
3 3 3 1
Loading