-
Notifications
You must be signed in to change notification settings - Fork 0
/
cars.c
100 lines (80 loc) · 2.85 KB
/
cars.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
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
#include "simulator.h"
#include "plates.h"
#include "lpr.h"
#include "shm.h"
#include "cars.h"
pthread_mutex_t fileLock;
// Struct for car
typedef struct cars {
char plate[6];
int entrance;
} cars_t;
// Calculate the bill based on time spent, adding to the total revenue of manager.c
void calcBill(int totalCarTime, char *plate) {
double totalBill = totalCarTime * BILLING_RATE;
carBill += totalBill;
createBillingFile(plate, totalBill);
}
// Create / write to the billing.txt file with the plate and the total bill
void createBillingFile(char *plate, double totalBill) {
pthread_mutex_lock(&fileLock);
FILE *fp = fopen("billing.txt", "a");
fprintf(fp, "%s $%.2f\n", plate, totalBill);
fclose(fp);
pthread_mutex_unlock(&fileLock);
}
// Calculate the total time a car is in the car park for after entering
int sleepCarTime( ) {
int parkingTime = 0;
int parkingGenerateTime = generateInRange(100, 10000);
int totalCarTime = DRIVE_TO_PARKING_TIME + parkingGenerateTime + DRIVE_TO_EXIT_TIME;
sleep(msSleep(DRIVE_TO_PARKING_TIME));
parkingTime = msSleep(parkingGenerateTime);
sleep(parkingTime);
sleep(msSleep(DRIVE_TO_EXIT_TIME));
return totalCarTime;
}
// Work that the car does
void *car(void *params)
{
cars_t *carThreadParams = params;
char *plate = carThreadParams->plate;
// Calculate the bill and sleep the thread for the designated time
calcBill(sleepCarTime(), plate);
int exit = carThreadParams->entrance;
// Boom Gate Status: when the car exits the car park
pthread_mutex_lock(&shm.data->exits[exit].gate.lock);
strcpy(shm.data->exits[exit].sensor.plate, plate);
shm.data->exits[exit].gate.status = 'R';
sleep(msSleep(RAISE_TIME));
pthread_mutex_unlock(&shm.data->exits[exit].gate.lock);
pthread_mutex_lock(&shm.data->exits[exit].gate.lock);
shm.data->exits[exit].gate.status = 'O';
sleep(msSleep(OPEN_TIME));
pthread_mutex_unlock(&shm.data->exits[exit].gate.lock);
pthread_mutex_lock(&shm.data->exits[exit].gate.lock);
shm.data->exits[exit].gate.status = 'L';
sleep(msSleep(LOWER_TIME));
pthread_mutex_unlock(&shm.data->exits[exit].gate.lock);
pthread_mutex_lock(&shm.data->exits[exit].gate.lock);
shm.data->exits[exit].gate.status = 'C';
pthread_mutex_unlock(&shm.data->exits[exit].gate.lock);
// Decrement the capacity for the level after the car leaves
level[exit]--;
free(carThreadParams);
return 0;
}
// Initialise the variables to be sent into the car thread
void initCars(char *plate, int entrance)
{
pthread_t carsThread;
cars_t *carThreadParams = malloc(sizeof(cars_t));
strcpy(carThreadParams->plate, plate);
carThreadParams->entrance = entrance;
// Initialize the the locks
pthread_mutex_init(&fileLock, NULL);
// Creating a thread for each individual car which enters the car park
// and passing through its plate and level information
pthread_create(&carsThread, NULL, car, (void *)carThreadParams);
return;
}