-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathheaders.h
152 lines (125 loc) · 3.3 KB
/
headers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h> //if you don't use scanf/printf change this include
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <queue>
#include <string>
#include <math.h>
#include <vector>
#include <iomanip>
using namespace std;
#define SHKEY 300
//Defining Structs
//Struct process data for all processes loaded from input file and processes in ready queue
struct processData {
int id;
int arrivalTime;
int runningTime;
int priority;
int remainingTime;
int criteria;
int PID;
int runningID; //order of running
processData(){}
processData(int i, int a, int r, int p)
{id=i; arrivalTime=a; runningTime=r; priority=p;}
/* overload the less-than operator so priority queues know how to compare two processData objects */
bool operator>(const processData& right) const
{
return criteria < right.criteria;
}
bool operator<(const processData& right) const
{
return criteria > right.criteria;
}
void print() {
printf("\n Process Data: id %d \t arrivaltime %d runningtime %d priority %d", id, arrivalTime, runningTime,
priority);
}
}processData;
//Struct for process data in process control block
struct processBlock {
int id;
int arrivalTime;
int runningTime;
int priority;
int remainingTime;
int startTime;
int finishTime;
int waitTime; //derived from other attributes?
string state;
processBlock() {id = -1;}
processBlock(struct processData data) {
id= data.id;
arrivalTime=data.arrivalTime;
runningTime=data.runningTime;
priority=data.priority;
remainingTime=data.remainingTime;
}
}processBlock;
//Struct message buffer to be used when sending a process from process generator to scheduler
struct processMsgBuff
{
long mtype;
struct processData mProcess;
};
//Enum for Algorithms' choice
enum Algorithm {
HPF,
SRTN,
RoundRobin
};
///==============================
//don't mess with this variable//
int* shmaddr; //
//===============================
int nProcesses=0;
int getClk()
{
int clk=*shmaddr;
return clk;
}
int pulse(int ¤tClk)
{
if (getClk()==currentClk) return false;
else {currentClk=getClk(); return true;}
}
/* All process call this function at the begining to establish communication
between them and the clock module
Again, Remember that the clock is only emulation
*/
void initClk()
{
int shmid = shmget(SHKEY, 4, 0444);
while((int)shmid == -1)
{
//Make sure that the Clock exists
printf("wait, Clock not initialized yet\n");
sleep(1);
shmid = shmget(SHKEY, 4, 0444);
}
shmaddr = (int*) shmat(shmid, (void *)0, 0);
}
/* All process call this function at the end to release the communication
resources between them and the clock module
Again, Remember that the clock is only emulation
input: terminateAll : is a flag to indicate whether that
this is the end of simulation it terminates all the system and release resources
*/
void destroyClk(bool terminateAll)
{
shmdt(shmaddr);
if(terminateAll)
killpg(getpgrp(),SIGINT);
}