-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaders.h
71 lines (58 loc) · 1.55 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
#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 <string.h>
#include <math.h>
#include "data_structures.h"
typedef short bool;
#define true 1
#define false 0
#define SHKEY 300
///==============================
//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)
{
killpg(getpgrp(), SIGINT);
}
}