-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcar.c
More file actions
192 lines (150 loc) · 5.07 KB
/
Copy pathcar.c
File metadata and controls
192 lines (150 loc) · 5.07 KB
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
/*
Operating Systems - Final Project
@author Miguel Dinis | 2019219010 | miguelbarroso@student.dei.uc.pt | github.com/MigDinny
@author Rodrigo Ferreira | 2019220060 | rferreira@student.dei.uc.pt | github.com/IronMan988
*/
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include "include.h"
void finish(int my_index) {
char pCommand[MAX_COMMAND];
sprintf(pCommand, "[%d] ending", cars[my_index].carNum);
dlog(pCommand);
// add carID to array of winners
// minus one car
sem_wait(shmutex);
runningCarsT--;
shmem->runningCarsTotal--;
sem_post(shmutex);
// [Unnamed Pipe] signal raceman: this car finished
command_t send;
send.carID = my_index;
send.carStatus = FINISHED;
write(channel[1], &send, sizeof(command_t));
// if last car, unblock team-manager (it is stuck on while loop)
if (runningCarsT == 0)
pthread_cond_signal(&in_box);
pthread_exit(NULL);
}
void *car_worker(void *car_index) {
char pCommand[MAX_COMMAND];
int my_index = *((int*) car_index);
int team_index = my_index / config.nCars;
// minimum fuel for 1, 2 or 4 laps
float fuel1 = cars[my_index].consumption * config.distance / cars[my_index].speed;
float fuel2 = 2 * fuel1;
float fuel4 = 4 * fuel1;
int tryBox = 0; // 0 false 1 true
cars[my_index].boxStops = 0;
message_t receivedMSG;
int receivedBytes = 0;
int isCarFailure = 0;
// each iteration is a TIME UNIT
while(1) {
// try to read a message from MQ; NO BLOCK
receivedBytes = msgrcv(shmem->mqid, &receivedMSG, sizeof(message_t), my_index+1, IPC_NOWAIT);
if (receivedBytes > 0) {
sprintf(pCommand, "NEW PROBLEM IN CAR %d", cars[my_index].carNum);
plog(pCommand);
cars[my_index].status = SAFETY;
sprintf(pCommand, "[%d] changed status to SAFETY", cars[my_index].carNum);
plog(pCommand);
isCarFailure = 1;
receivedBytes = 0;
tryBox = 1;
shmem->countBreakDowns++;
}
// proccess a time unit -> update positions
if (cars[my_index].status == SAFETY) {
cars[my_index].pos += 0.3*cars[my_index].speed;
cars[my_index].fuel -= 0.4*cars[my_index].consumption;
} else {
cars[my_index].pos += cars[my_index].speed;
cars[my_index].fuel -= cars[my_index].consumption;
}
// new lap
if (cars[my_index].pos >= config.distance) {
// update vars
cars[my_index].pos -= config.distance;
cars[my_index].laps++;
// if finished race
if (cars[my_index].laps >= config.nTurns) {
cars[my_index].status = FINISHED;
sprintf(pCommand, "[%d] changed status to FINISHED", cars[my_index].carNum);
plog(pCommand);
sprintf(pCommand, "[%d] finished\n", cars[my_index].carNum);
dlog(pCommand);
sem_wait(shmutex);
addCarWIDs(my_index);
sem_post(shmutex);
finish(my_index);
}
// if this car is trying to get into box
if (tryBox == 1) {
// we are in box, acquire lock
pthread_mutex_lock(&tc_mutex);
// check if box is empty
if ( (teams[team_index].status == FREE) || (teams[team_index].status == RESERVED && cars[my_index].status == SAFETY) ) {
// change these variables so the condition breaks on the other side and it stops looping on cond_wait()
awaitingCars++;
if (cars[my_index].status == SAFETY)
awaitingSafetyCars++;
boxCarIndex = my_index;
sprintf(pCommand, "[%d] in box", cars[my_index].carNum);
dlog(pCommand);
if (isCarFailure == 1) {
isTeamCarFailure = 1;
isCarFailure = 0;
}
// inside box
cars[my_index].status = BOX;
sprintf(pCommand, "[%d] changed status to BOX", cars[my_index].carNum);
plog(pCommand);
pthread_cond_signal(&in_box);
pthread_cond_wait(&out_box, &tc_mutex);
sprintf(pCommand, "[%d] left box", cars[my_index].carNum);
dlog(pCommand);
// left box
tryBox = 0;
cars[my_index].status = RUNNING;
sprintf(pCommand, "[%d] changed status to RUNNING", cars[my_index].carNum);
plog(pCommand);
}
pthread_mutex_unlock(&tc_mutex);
}
// if race was interrupted -> finish this car because he crossed lap
if (shmem->status == OFF) {
finish(my_index);
}
}
// check fuel
if (cars[my_index].fuel >= fuel4 && cars[my_index].fuel < fuel4+fuel1 && tryBox != 1) {
// fuel for only 4 laps -> TRY TO GET INTO BOX
sprintf(pCommand, "[%d] try box", cars[my_index].carNum);
dlog(pCommand);
tryBox = 1;
} else if (cars[my_index].fuel >= fuel2 && cars[my_index].fuel < fuel2+fuel1 && cars[my_index].status != SAFETY) {
// fuel for only 2 laps -> SAFETY MODE
cars[my_index].status = SAFETY;
sprintf(pCommand, "[%d] changed status to SAFETY", cars[my_index].carNum);
plog(pCommand);
tryBox = 1;
} else if (cars[my_index].fuel <= 0) {
// NO FUEL
cars[my_index].status = NO_FUEL;
sprintf(pCommand, "[%d] changed status to NO FUEL", cars[my_index].carNum);
plog(pCommand);
sem_wait(shmutex);
shmem->quitCars++;
sem_post(shmutex);
finish(my_index);
}
usleep(1 * 1000 * 1000 * config.multiplier); // sleep 1 TIME UNIT
}
finish(my_index);
}