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

Scheduler setup #8

Merged
merged 3 commits into from
Apr 14, 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
6 changes: 4 additions & 2 deletions code/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
.PHONY: build all clean test

build:
gcc list.c queue.c process_generator.c -o process_generator.out
gcc clk.c -o clk.out
gcc scheduler.c -o scheduler.out
gcc process.c -o process.out
gcc list.c scheduler.c -o scheduler.out
gcc list.c process.c -o process.out
gcc test_generator.c -o test_generator.out

clean:
Expand Down
4 changes: 2 additions & 2 deletions code/clk.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
int shmid;

/* Clear the resources before exit */
void cleanup(int signum) {
void cleanUpClk(int signum) {
shmctl(shmid, IPC_RMID, NULL);
printf("Clock terminating!\n");
exit(0);
Expand All @@ -19,7 +19,7 @@ void cleanup(int signum) {
/* This file represents the system clock for ease of calculations */
int main(int argc, char *argv[]) {
printf("Clock starting\n");
signal(SIGINT, cleanup);
signal(SIGINT, cleanUpClk);
int clk = 0;
// Create shared memory for one integer variable 4 bytes
shmid = shmget(SHKEY, 4, IPC_CREAT | 0644);
Expand Down
70 changes: 47 additions & 23 deletions code/headers.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#pragma once

#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h> //if you don't use scanf/printf change this include
Expand All @@ -11,11 +14,6 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
///==============================
// CUSTOM INCLUDES
//===============================
#include "./queue.h"
#include "./structs.h"

//===============================
// CONSTANTS
Expand All @@ -37,29 +35,15 @@
#define true 1
#define false 0

#define SCH_GEN_COM 5

#define SHKEY 300

///==============================
// don't mess with this variable//
int *shmaddr; //
int *shmaddr;
//===============================

// ===================================
// ====== PROCESS GENERATOR ======
// ====== FUNCTION DECLARATIONS ======
// ===================================

queue *readInputFile();
void printBanner();
enum scheduler_type getSchedulerType();
void getInput(enum scheduler_type *, int *);
void clearResources(int);
void createSchedulerAndClock(pid_t *, pid_t *);
void sendProcessesToScheduler(queue *, int);
int intiSchGenCom();

// ===================================

int getClk() { return *shmaddr; }

/*
Expand All @@ -85,10 +69,50 @@ void initClk() {
* Input: terminateAll: a flag to indicate whether that this is the end of
* simulation. It terminates the whole system and releases resources.
*/

void destroyClk(bool terminateAll) {
shmdt(shmaddr);
if (terminateAll) {
killpg(getpgrp(), SIGINT);
}
}

//===============================
// CUSTOM COMMON FUNCTIONS
//===============================

/**
* cleanUp - Make necessary cleaning
*/
void cleanUp() {
// TODO any other needed clean up
destroyClk(true);
killpg(getpgrp(), SIGINT);
}

/**
* clearResources - Clears all resources in case of interruption.
*
* @signum: the signal number
*/
void clearResources(int signum) {
cleanUp();
exit(0);
}

/**
* intiSchGenCom - Initializes the message queue between the scheduler and the
* process generator.
*
* return: the message queue id
*/
int initSchGenCom() {
int key = ftok("keyfiles/SCH_GEN_COM", SCH_GEN_COM);
int msgQID = msgget(key, 0666 | IPC_CREAT);

if (msgQID == -1) {
perror("Error in creating message queue");
exit(-1);
}

return msgQID;
}
Empty file added code/keyfiles/SCH_GEN_COM
Empty file.
Loading