Skip to content

Commit

Permalink
refactor: organize functions and header files
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedHamed3699 authored and AbdelruhmanSamy committed Apr 14, 2024
1 parent 231b766 commit 2350e57
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 149 deletions.
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
52 changes: 29 additions & 23 deletions code/headers.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include <errno.h>
#include <signal.h>
#include <stdbool.h>
Expand All @@ -12,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 @@ -38,31 +35,15 @@
#define true 1
#define false 0

// key proj_ids
#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();
scheduler_type getSchedulerType();
void getInput(scheduler_type *, int *);
void createSchedulerAndClock(pid_t *, pid_t *, int);
void sendProcessesToScheduler(queue *, int);
int intiSchGenCom();

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

int getClk() { return *shmaddr; }

/*
Expand Down Expand Up @@ -95,7 +76,15 @@ void destroyClk(bool terminateAll) {
}
}

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

/**
* cleanUp - Make necessary cleaning
*/
void cleanUp() {
// TODO any other needed clean up
destroyClk(true);
killpg(getpgrp(), SIGINT);
}
Expand All @@ -106,7 +95,24 @@ void cleanUp() {
* @signum: the signal number
*/
void clearResources(int signum) {
// TODO: Clears all resources in case of interruption
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;
}
5 changes: 1 addition & 4 deletions code/list.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef LIST
#define LIST
#pragma once

#include <stdlib.h>

Expand Down Expand Up @@ -46,5 +45,3 @@ int deleteNode(d_list *list, unsigned int index);
void freeNode(d_list *list, d_node *node);
void freeList(d_list *list);
void destroyList(d_list **list);

#endif
24 changes: 3 additions & 21 deletions code/process_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* ====================================================================
*/

#include "./headers.h"
#include "queue.h"
#include "process_generator.h"
#include "headers.h"

/**
* main - The main function of the process generator.
Expand Down Expand Up @@ -41,7 +41,7 @@ int main(int argc, char *argv[]) {
createSchedulerAndClock(&schedulerPid, &clockPid, (int)schedulerType);

initClk();
msgQID = intiSchGenCom();
msgQID = initSchGenCom();

sendProcessesToScheduler(processes, msgQID);
destroyQueue(processes);
Expand Down Expand Up @@ -322,21 +322,3 @@ void sendProcessesToScheduler(queue *processes, int msgQID) {
exit(-1);
}
}

/**
* intiSchGenCom - Initializes the message queue between the scheduler and the
* process generator.
*
* return: the message queue id
*/
int intiSchGenCom() {
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;
}
16 changes: 16 additions & 0 deletions code/process_generator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "queue.h"
#include "structs.h"

// ===================================
// ====== PROCESS GENERATOR ======
// ====== FUNCTION DECLARATIONS ======
// ===================================
//
queue *readInputFile();
void printBanner();
scheduler_type getSchedulerType();
void getInput(scheduler_type *, int *);
void createSchedulerAndClock(pid_t *, pid_t *, int);
void sendProcessesToScheduler(queue *, int);
Loading

0 comments on commit 2350e57

Please sign in to comment.