-
Notifications
You must be signed in to change notification settings - Fork 2
/
headers.h
235 lines (212 loc) · 5.63 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#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 <unistd.h>
#include <string.h>
#include <time.h>
typedef short bool;
#define true 1
#define false 0
#define SHKEY 300
#define SEM1KEY 'B'
#define SEM_PROCESS_KEY 'A'
#define MSG_SCHED_KEY 'H'
#define MSG_PROC_DOWN_KEY 'D'
#define MSG_PROC_UP_KEY 'U'
bool terminateSignalSent = 0;
enum Algorithms
{
HPF,
SRTN,
RR
};
// ================================================= Used Structs ===================================
struct processInfo
{
int id;
int arrivalTime;
int runTime;
int priority;
int pid;
bool isRunning;
int remainingTime;
int finishTime;
int startTime;
int memsize;
int blockStart;
int actualSize; // actual size allocated in memory as a power of 2
};
struct processStats
{
int id;
int arrivalTime;
int runTime;
int remainingTime;
int finishTime;
int startTime;
int waitingTime;
int TA;
float WTA;
};
struct msgbuff
{
long mtype;
int numberOfProcesses;
struct processInfo p_info;
bool finished;
};
struct msgAlgorithm
{
long mtype;
int algorithm;
int opts;
};
struct msgProcessTimeBuff
{
long mtype;
int remainingTime;
};
/* arg for semctl system calls. */
union Semun
{
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
ushort *array; /* array for GETALL & SETALL */
struct seminfo *__buf; /* buffer for IPC_INFO */
void *__pad;
};
// =======================================================================================================
// ================================ Clock control Funtions ============================
///==============================
//don't mess with this variable//
int *shmaddr; //
//===============================
int getClk()
{
return *shmaddr;
}
/*
* All process call this function at the beginning 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! The 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: 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 && !terminateSignalSent)
{
printf("Call from destory clk \n");
terminateSignalSent = 1;
killpg(getpgrp(), SIGINT);
}
}
// =======================================================================================
// ================================ Error Validation ============================
enum error_types
{
CREATION,
EMPTY_SEM_INIT,
EMPTY_SEM_GET_VAL,
EMPTY_SEM_SET_VAL,
FULL_SEM_INIT,
FULL_SEM_GET_VAL,
FULL_SEM_SET_VAL,
MUTEX_SEM_SET_VAL,
MSG_RECEIVE,
MSG_SEND,
SHM_ATTCH,
SEM_DOWN,
SEM_UP
};
void validate(int errorNumber, int checkVar)
{
if (checkVar != -1)
return;
switch (errorNumber)
{
case CREATION:
perror("error in creation.\n");
break;
case EMPTY_SEM_INIT:
perror("error in initializing empty semaphore.\n");
break;
case EMPTY_SEM_GET_VAL:
perror("error in getting empty semaphore value.\n");
break;
case FULL_SEM_INIT:
perror("error in initializing full semaphore.\n");
break;
case FULL_SEM_GET_VAL:
perror("error in getting full semaphore value.\n");
break;
case MSG_RECEIVE:
perror("error in receiving messege.\n");
break;
case MSG_SEND:
perror("error in sending messege.\n");
break;
case SHM_ATTCH:
perror("error in attaching shared memeory.\n");
break;
case SEM_DOWN:
perror("error in downing the semaphore value.\n");
break;
case SEM_UP:
perror("error in uping the semaphore value.\n");
break;
case FULL_SEM_SET_VAL:
perror("error in setting the full value.\n");
break;
case EMPTY_SEM_SET_VAL:
perror("error in setting the empty value.\n");
break;
case MUTEX_SEM_SET_VAL:
perror("error in setting the mutex value.\n");
break;
default:
perror("unhandled error occured.\n");
break;
}
exit(-1);
}
// ==============================================================================
// ======================================Common functions========================
/**
* function to perform a down operation on a semaphore providing its id
* @param semid the id of the semaphore to be modified
*/
void down(int sem)
{
struct sembuf p_op;
p_op.sem_num = 0;
p_op.sem_op = -1;
p_op.sem_flg = !IPC_NOWAIT;
validate(SEM_DOWN, semop(sem, &p_op, 1));
}