This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemSharedMemPlayer.c
337 lines (277 loc) · 10.3 KB
/
semSharedMemPlayer.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* \file semSharedMemSmoker.c (implementation file)
*
* \brief Problem name: SoccerGame
*
* Synchronization based on semaphores and shared memory.
* Implementation with SVIPC.
*
* Definition of the operations carried out by the players:
* \li arrive
* \li playerConstituteTeam
* \li waitReferee
* \li playUntilEnd
*
* \author Nuno Lau - January 2021
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <string.h>
#include <math.h>
#include "probConst.h"
#include "probDataStruct.h"
#include "logging.h"
#include "sharedDataSync.h"
#include "semaphore.h"
#include "sharedMemory.h"
/** \brief logging file name */
static char nFic[51];
/** \brief shared memory block access identifier */
static int shmid;
/** \brief semaphore set access identifier */
static int semgid;
/** \brief pointer to shared memory region */
static SHARED_DATA *sh;
/** \brief player takes some time to arrive */
static void arrive (int id);
/** \brief player constitutes team */
static int playerConstituteTeam (int id);
/** \brief player waits for referee to start match */
static void waitReferee(int id, int team);
/** \brief player waits for referee to end match */
static void playUntilEnd(int id, int team);
/**
* \brief Main program.
*
* Its role is to generate the life cycle of one of intervening entities in the problem: the player.
*/
int main (int argc, char *argv[])
{
int key; /*access key to shared memory and semaphore set */
char *tinp; /* numerical parameters test flag */
int n, team;
/* validation of command line parameters */
if (argc != 4) {
freopen ("error_PL", "a", stderr);
fprintf (stderr, "Number of parameters is incorrect!\n");
return EXIT_FAILURE;
}
/* get goalie id - argv[1]*/
n = (unsigned int) strtol (argv[1], &tinp, 0);
if ((*tinp != '\0') || (n >= NUMPLAYERS )) {
fprintf (stderr, "Player process identification is wrong!\n");
return EXIT_FAILURE;
}
/* get logfile name - argv[2]*/
strcpy (nFic, argv[2]);
/* redirect stderr to error file - argv[3]*/
freopen (argv[3], "w", stderr);
setbuf(stderr,NULL);
/* getting key value */
if ((key = ftok (".", 'a')) == -1) {
perror ("error on generating the key");
exit (EXIT_FAILURE);
}
/* connection to the semaphore set and the shared memory region and mapping the shared region onto the
process address space */
if ((semgid = semConnect (key)) == -1) {
perror ("error on connecting to the semaphore set");
return EXIT_FAILURE;
}
if ((shmid = shmemConnect (key)) == -1) {
perror ("error on connecting to the shared memory region");
return EXIT_FAILURE;
}
if (shmemAttach (shmid, (void **) &sh) == -1) {
perror ("error on mapping the shared region on the process address space");
return EXIT_FAILURE;
}
/* initialize random generator */
srandom ((unsigned int) getpid ());
/* simulation of the life cycle of the player */
arrive(n);
if((team = playerConstituteTeam(n))!=0) {
waitReferee(n, team);
playUntilEnd(n, team);
}
/* unmapping the shared region off the process address space */
if (shmemDettach (sh) == -1) {
perror ("error on unmapping the shared region off the process address space");
return EXIT_FAILURE;;
}
return EXIT_SUCCESS;
}
/**
* \brief player takes some time to arrive
*
* Player updates state and takes some time to arrive
* The internal state should be saved.
*
*/
static void arrive(int id)
{
if (semDown (semgid, sh->mutex) == -1) { /* enter critical region */
perror ("error on the up operation for semaphore access (PL)");
exit (EXIT_FAILURE);
}
/* TODO: insert your code here */
sh->fSt.st.playerStat[id]=ARRIVING;
saveState(nFic,&sh->fSt);
if (semUp (semgid, sh->mutex) == -1) { /* exit critical region */
perror ("error on the down operation for semaphore access (PL)");
exit (EXIT_FAILURE);
}
usleep((200.0*random())/(RAND_MAX+1.0)+50.0);
}
/**
* \brief player constitutes team
*
* If player is late, it updates state and leaves.
* If there are enough free players and free goalies to form a team, player forms team allowing
* team members to proceed and waiting for them to acknowledge registration.
* Otherwise it updates state, waits for the forming teammate to "call" him, saves its team
* and acknowledges registration.
* The internal state should be saved.
*
* \param id player id
*
* \return id of player team (0 for late goalies; 1 for team 1; 2 for team 2)
*
*/
static int playerConstituteTeam (int id)
{
int ret = 0;
if (semDown (semgid, sh->mutex) == -1) { /* enter critical region */
perror ("error on the up operation for semaphore access (PL)");
exit (EXIT_FAILURE);
}
/* TODO: insert your code here */
sh->fSt.playersArrived++;
sh->fSt.playersFree++;
if (sh->fSt.playersFree>=4 && sh->fSt.goaliesFree>=1){
sh->fSt.st.playerStat[id]=FORMING_TEAM;
saveState(nFic,&sh->fSt);
sh->fSt.playersFree=sh->fSt.playersFree-4;
sh->fSt.goaliesFree--;
if (semUp (semgid, sh->goaliesWaitTeam) == -1) {
perror ("error on the down operation for semaphore access (GL)");
exit (EXIT_FAILURE);
}
for (int i=0; i< 3; i++){
if (semUp (semgid, sh->playersWaitTeam) == -1) {
perror ("error on the down operation for semaphore access (PL)");
exit (EXIT_FAILURE);
}
}
for (int i=0; i< 4; i++){
if (semDown (semgid, sh->playerRegistered) == -1) {
perror ("error on the down operation for semaphore access (PL)");
exit (EXIT_FAILURE);
}
}
ret=sh->fSt.teamId++;
}
else if ((sh->fSt.playersFree <4 || sh->fSt.goaliesFree < 1) && sh->fSt.playersArrived <= 2*NUMTEAMPLAYERS){
sh->fSt.st.playerStat[id]=WAITING_TEAM;
saveState(nFic,&sh->fSt);
}
else {
sh->fSt.st.playerStat[id]=LATE;
saveState(nFic,&sh->fSt);
}
if (semUp (semgid, sh->mutex) == -1) { /* exit critical region */
perror ("error on the down operation for semaphore access (PL)");
exit (EXIT_FAILURE);
}
/* TODO: insert your code here */
if (sh->fSt.st.playerStat[id]==FORMING_TEAM){
if (semUp (semgid, sh->refereeWaitTeams) == -1) { /* leave critical region */
perror ("error on the up operation for semaphore access (PL)");
exit (EXIT_FAILURE);
}
}
if (sh->fSt.st.playerStat[id]==WAITING_TEAM){
if (semDown (semgid, sh->playersWaitTeam) == -1) {
perror ("error on the up operation for semaphore access (PL)");
exit (EXIT_FAILURE);
}
ret=sh->fSt.teamId;
if (semUp (semgid, sh->playerRegistered) == -1) { /* leave critical region */
perror ("error on the up operation for semaphore access (PL)");
exit (EXIT_FAILURE);
}
}
return ret;
}
/**
* \brief player waits for referee to start match
*
* The player updates its state and waits for referee to end match.
* The internal state should be saved.
*
* \param id player id
* \param team player team
*/
static void waitReferee (int id, int team)
{
if (semDown (semgid, sh->mutex) == -1) { /* enter critical region */
perror ("error on the up operation for semaphore access (PL)");
exit (EXIT_FAILURE);
}
/* TODO: insert your code here */
if (team==1){
sh->fSt.st.playerStat[id]=WAITING_START_1;
saveState(nFic,&sh->fSt);
}
else {
sh->fSt.st.playerStat[id]=WAITING_START_2;
saveState(nFic,&sh->fSt);
}
if (semUp (semgid, sh->mutex) == -1) { /* exit critical region */
perror ("error on the down operation for semaphore access (PL)");
exit (EXIT_FAILURE);
}
/* TODO: insert your code here */
if (semDown (semgid, sh->playersWaitReferee) == -1) {
perror ("error on the down operation for semaphore access (PL)");
exit (EXIT_FAILURE);
}
}
/**
* \brief player waits for referee to end match
*
* The player updates its state and waits for referee to end match.
* The internal state should be saved.
*
* \param id player id
* \param team player team
*/
static void playUntilEnd (int id, int team)
{
if (semDown (semgid, sh->mutex) == -1) { /* enter critical region */
perror ("error on the up operation for semaphore access (PL)");
exit (EXIT_FAILURE);
}
/* TODO: insert your code here */
if (team==1){
sh->fSt.st.playerStat[id]=PLAYING_1;
saveState(nFic,&sh->fSt);
}
else{
sh->fSt.st.playerStat[id]=PLAYING_2;
saveState(nFic,&sh->fSt);
}
if (semUp (semgid, sh->mutex) == -1) { /* exit critical region */
perror ("error on the down operation for semaphore access (PL)");
exit (EXIT_FAILURE);
}
/* TODO: insert your code here */
if (semDown (semgid, sh->playersWaitEnd) == -1) { /* exit critical region */
perror ("error on the down operation for semaphore access (PL)");
exit (EXIT_FAILURE);
}
}