-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.c
68 lines (51 loc) · 1.56 KB
/
simulator.c
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
#include "cpmss.h"
#include "shm.h"
#include "simulator.h"
#include "lpr.h"
#include "plates.h"
#include "cars.h"
// Generate and return a random number in the provided range
int generateInRange(int lower, int upper) {
int num = (rand() % (upper - lower + 1)) + lower;
return num;
}
// Function to convert to milliseconds for sleep
int msSleep (long msec) {
struct timespec ts;
int result;
if (msec < 0)
{
errno = EINVAL;
return -1;
}
ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;
do {
result = nanosleep(&ts, &ts);
} while (result && errno == EINTR);
return result;
}
// The main thread function for the simulator.c file
void *simulatorMain() {
// Read plates.txt, adding all entries to a hashtable
readPlates("plates.txt", "r");
// Seeded random to current time
srand(time(NULL));
// Initialise all level car capacities to 0
for (int i = 0; i < 5; i++) {
level[i] = 0;
}
// Initialisation of threads
pthread_t threadWhileLoop, threadRange, threadTemp;
while (1) {
// Create three threads and run them simultaneously
pthread_create(&threadWhileLoop, NULL, generateCars, NULL);
pthread_create(&threadRange, NULL, generatePlateTime, NULL);
pthread_create(&threadTemp, NULL, generateTemp, NULL);
// Join runs the thread(function) after it exits once
pthread_join(threadWhileLoop, NULL);
pthread_join(threadRange, NULL);
pthread_join(threadTemp, NULL);
}
return 0;
}