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

Sync with semaphors #27

Merged
merged 3 commits 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
34 changes: 34 additions & 0 deletions code/headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ void clearResources(int signum) {
exit(0);
}

//===============================
// IPC Functions
//===============================

/**
* intiSchGenCom - Initializes the message queue between the scheduler and the
* process generator.
Expand Down Expand Up @@ -170,3 +174,33 @@ int initSchProShm(int pid) {

return shmid;
}


int initSchProQ() {
int id = ftok("keyfiles/PRO_SCH_Q", SCH_PRO_COM);
int q_id = msgget(id, IPC_CREAT | 0666);

if (q_id == -1) {
perror("error in creating msg queue between process & scheduler");
exit(-1);
} else if (DEBUG) {
printf("Message queue created successfully with pid = %d\n", q_id);
}

return q_id;
}

int initSchProSem()
{
int id = ftok("keyfiles/PRO_SCH_SEM", SCH_PRO_COM);
int semid = semget(id, 1, 0666 | IPC_CREAT);

if (semid == -1){
perror("Error in create sem\n");
exit(-1);
}


return semid;
}

25 changes: 19 additions & 6 deletions code/process.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "headers.h"
#include <unistd.h>

int *shmAdd;
int *RT;
int shmid;

int semid;
void sigTermHandler(int signum) {
if (shmdt(shmAdd) == -1) {
if (shmdt(RT) == -1) {
perror("Error in detach\n");
} else if (DEBUG) {
printf(ANSI_GREEN
Expand All @@ -25,20 +25,33 @@ int main(int agrc, char *argv[]) {

signal(SIGTERM, sigTermHandler);

// initailizing remaining time shared memory
shmid = initSchProShm(getpid());
shmAdd = (int *)shmat(shmid, (void *)0, 0);
RT = (int *)shmat(shmid, (void *)0, 0);

semid = initSchProSem();

initClk();

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

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

down(semid);
int val = *RT;
up(semid);

while (val > 0) {

if (currTime != preTime) {
preTime = currTime;
(*shmAdd)--;

down(semid);
(*RT)--;
val = *RT;
up(semid);

}

currTime = getClk();
Expand Down
13 changes: 10 additions & 3 deletions code/processes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#id arrival runtime priority
1 3 1 3
2 3 2 2
3 3 3 1
1 6 1 0
2 16 2 4
3 17 11 8
4 28 1 6
5 29 23 10
6 34 4 3
7 44 24 10
8 51 4 2
9 52 21 8
10 56 17 3
74 changes: 47 additions & 27 deletions code/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

process_t *currentProcess = NULL;
perfStats stats;

int semid;
/**
* main - The main function of the scheduler.
*
Expand Down Expand Up @@ -70,8 +70,15 @@ void schedule(scheduler_type schType, int quantem, int gen_msgQID) {
int quantemClk = 0, currentClk = 0;
int (*algorithm)(void **readyQ, process_t *newProcess, int *rQuantem);

initPerformanceStats();
semid = initSchProSem(); //initializing semaphor to control RT shared mem.
SemUn semun;
semun.val = 1;
if (semctl(semid, 0, SETVAL, semun) == -1){
perror("Error in semctl");
exit(-1);
}

initPerformanceStats();
switch (schType) {
case HPF:
readyQ = createMinHeap(compareHPF);
Expand Down Expand Up @@ -102,10 +109,14 @@ void schedule(scheduler_type schType, int quantem, int gen_msgQID) {
if (currentClk != lastClk) {
printf(ANSI_GREY "========================================\n" ANSI_RESET);
printf(ANSI_BLUE "==>SCH: Current Clk = %i\n" ANSI_RESET, currentClk);
if (currentProcess && *currentProcess->RT > 0) {
printf(ANSI_BLUE "==>SCH:" ANSI_GREEN " Process %d " ANSI_BOLD
"RT = %i\n" ANSI_RESET,
currentProcess->id, *currentProcess->RT);

if (currentProcess) {
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);
}
}
}

Expand Down Expand Up @@ -174,9 +185,9 @@ int compareHPF(void *e1, void *e2) {
* Return: 1 if e2 Remaining Time is less, -1 if e1 is less, 0 if they are equal
*/
int compareSRTN(void *e1, void *e2) {
if (*((process_t *)e1)->RT < *((process_t *)e2)->RT)
if(getRemTime((process_t *)e1) < getRemTime((process_t *)e2))
return -1;
else if (*((process_t *)e1)->RT > *((process_t *)e2)->RT)
if(getRemTime((process_t *)e1) > getRemTime((process_t *)e2))
return 1;
return 0;
}
Expand Down Expand Up @@ -372,12 +383,15 @@ process_t *createProcess(process_t *process) {
// printf(ANSI_BLUE "==>SCH: Created process with id = %i\n" ANSI_RESET,
// newProcess->pid);

//initilizing RT shared mem.
int shmid = initSchProShm(pid);
int *shmAdd = (int *)shmat(shmid, (void *)0, 0);


newProcess->RT = shmAdd;
*newProcess->RT = process->BT;

setRemTime(newProcess , process->BT);

return newProcess;
}

Expand All @@ -402,13 +416,13 @@ void startProcess(process_t *process) {
printf(ANSI_BLUE "==>SCH: Starting process with id = %i\n" ANSI_RESET,
process->pid);

kill(process->pid, SIGCONT);
process->state = RUNNING;

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

// log this
logger("started", process);
kill(process->pid, SIGCONT);
}
}

Expand Down Expand Up @@ -457,7 +471,9 @@ void resumeProcess(process_t *process) {
*/
void cleanUpScheduler() {
msgctl(initSchGenCom(), IPC_RMID, (struct msqid_ds *)0);
msgctl(initSchProQ(), IPC_RMID, (struct msqid_ds *)0);

//deleting semaphor
semctl(semid , 1 , IPC_RMID);
destroyClk(true);
}

Expand All @@ -471,31 +487,20 @@ void clearSchResources(int signum) {
exit(0);
}

int initSchProQ() {
int id = ftok("keyfiles/PRO_SCH_Q", SCH_PRO_COM);
int q_id = msgget(id, IPC_CREAT | 0666);

if (q_id == -1) {
perror("error in creating msg queue between process & scheduler");
exit(-1);
} else if (DEBUG) {
printf("Message queue created successfully with pid = %d\n", q_id);
}

return q_id;
}

void sigUsr1Handler(int signum) {
pid_t killedProcess;
int status;

killedProcess = wait(&status);

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

if(currentProcess) //FIXME: added to avoid double freeing currentProcess pointer (however it's expected to be unfreed *isn't this true?*)
free(currentProcess);
currentProcess = NULL;
}


void createLogFile() {
FILE *logFileptr = fopen(LOG_FILE, "w");
if (logFileptr == NULL) {
Expand All @@ -519,6 +524,7 @@ void logger(char *msg, process_t *p) {
int clk = getClk();
float WTA = p->TA / (float)p->BT;


fprintf(logFileptr,
"At time %i process %i %s arr %i total %i remain %i wait %i", clk,
p->id, msg, p->AT, p->BT, *p->RT, p->WT);
Expand Down Expand Up @@ -565,3 +571,17 @@ void writePerfFile() {
fprintf(perfFile, "Avg Waiting = %.2f\n", stats.avgWaitingTime);
fprintf(perfFile, "Std WTA = %.2f\n", stats.stdWTA);
}

int getRemTime(process_t* p){
down(semid);
int val = *(p->RT);
up(semid);

return val;
}

void setRemTime(process_t* p ,int val){
down(semid);
*(p->RT) = val;
up(semid);
}
7 changes: 3 additions & 4 deletions code/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ void resumeProcess(process_t *process);
//===============================
// IPC Functions
//===============================
// TODO: create a queue between scheduler and all processes to be able to signal
// it's termination
int initSchProQ();
int getRemTime(process_t* p);
void setRemTime(process_t* p , int val);
void sigUsr1Handler(int signum);

//==============================
Expand All @@ -55,4 +54,4 @@ void sigUsr1Handler(int signum);

void createLogFile();
void logger(char * action, process_t* process_pcb);
void writePerfFile();
void writePerfFile();
5 changes: 3 additions & 2 deletions code/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ typedef struct process {
int LST; // Last Stop time (last time the process was stopped)
// used to calculate the waiting time
int *RT; // remaining time
// int RTSemid; // id of the semaphor used to access this process
int TA; // turnaround time
} process_t;

union SemUn {
typedef union SemUn {
int val; /* Value for SETVAL */
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* Array for GETALL, SETALL */
struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */
};
} SemUn;

// To create scheduler.perf.
// The scheduler will create only one instant from this struct
Expand Down