-
Notifications
You must be signed in to change notification settings - Fork 1
/
stoplight.c
556 lines (493 loc) · 12.1 KB
/
stoplight.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* stoplight.c
*
* 31-1-2003 : GWA : Stub functions created for CS161 Asst1.
*
* NB: You can use any synchronization primitives available to solve
* the stoplight problem in this file.
*/
/*
*
* Includes
*
*/
#include <types.h>
#include <lib.h>
#include <test.h>
#include <thread.h>
#include <synch.h>
/*
*
* Constants
*
*/
/*
* Number of cars created.
*/
#define NCARS 20
static struct lock *lock0;
static struct lock *lock1;
static struct lock *lock2;
static struct lock *lock3;
int count;
/*
*
* Function Definitions
*
*/
static const char *directions[] = { "N", "E", "S", "W" };
static const char *msgs[] = {
"APPROACHING:",
"region0: ",
"region1: ",
"region2: ",
"region3: ",
"LEAVING: "
};
/* use these constants for the first parameter of message */
enum { APPROACHING, REGION1, REGION2, REGION3, LEAVING };
static void
message(int msg_no, int carnumber, int cardirection, int destdirection)
{
kprintf("%s car = %2d, direction = %s, destination = %s\n",
msgs[msg_no], carnumber,
directions[cardirection], directions[destdirection]);
}
/*
* gostraight()
*
* Arguments:
* unsigned long cardirection: the direction from which the car
* approaches the intersection.
* unsigned long carnumber: the car id number for printing purposes.
*
* Returns:
* nothing.
*
* Notes:
* This function should implement passing straight through the
* intersection from any direction.
* Write and comment this function.
*/
static
void
gostraight(unsigned long cardirection,
unsigned long carnumber,
unsigned long destdirection)
{
int lockrequired[2];
int acquiredAll;
//Checking the number of locks required
lockrequired[0]=cardirection;
if (cardirection==0){
lockrequired[1]=cardirection+3;
}
else{
lockrequired[1]=cardirection-1;
}
message(0,carnumber,cardirection,destdirection);
//if N to S
do{
if(lockrequired[0]==0 && lockrequired[1]==3)
{
if(lock0->owner==NULL)
{
lock_acquire(lock0);
assert(lock_do_i_hold(lock0));
if(lock3->owner==NULL)
{
lock_acquire(lock3);
assert(lock_do_i_hold(lock3));
message(1,carnumber,cardirection,destdirection);
message(4,carnumber,cardirection,destdirection);
lock_release(lock0);
lock_release(lock3);
acquiredAll=2;
}
else lock_release(lock0);
}}
//if S to N
else if(lockrequired[0]==2 && lockrequired[1]==1)
{if(lock2->owner==NULL)
{
lock_acquire(lock2);
assert(lock_do_i_hold(lock2));
if(lock1->owner==NULL)
{
lock_acquire(lock1);
assert(lock_do_i_hold(lock1));
message(3,carnumber,cardirection,destdirection);
message(2,carnumber,cardirection,destdirection);
lock_release(lock2);
lock_release(lock1);
acquiredAll=2;
}
else lock_release(lock2);
}
}
//if W to E
else if(lockrequired[0]==0 && lockrequired[1]==1)
{
if(lock0->owner==NULL)
{
lock_acquire(lock0);
assert(lock_do_i_hold(lock0));
if(lock1->owner==NULL)
{
lock_acquire(lock1);
assert(lock_do_i_hold(lock1));
message(1,carnumber,cardirection,destdirection);
message(2,carnumber,cardirection,destdirection);
lock_release(lock0);
lock_release(lock1);
acquiredAll=2;
}
else lock_release(lock0);
}}
// if E to W
else
{if(lock2->owner==NULL)
{
lock_acquire(lock2);
assert(lock_do_i_hold(lock2));
if(lock3->owner==NULL)
{
lock_acquire(lock3);
assert(lock_do_i_hold(lock3));
message(3,carnumber,cardirection,destdirection);
message(4,carnumber,cardirection,destdirection);
lock_release(lock2);
lock_release(lock3);
acquiredAll=2;
}
else lock_release(lock2);
}
}
}while(acquiredAll!=2);
message(5,carnumber,cardirection,destdirection);
count--;
}
/*
* turnleft()
*
* Arguments:
* unsigned long cardirection: the direction from which the car
* approaches the intersection.
* unsigned long carnumber: the car id number for printing purposes.
*
* Returns:
* nothing.
*
* Notes:
* This function should implement making a left turn through the
* intersection from any direction.
* Write and comment this function.
*/
static
void
turnleft(unsigned long cardirection,
unsigned long carnumber,
unsigned long destdirection)
{
/*
* Avoid unused variable warnings.
*/
int lockrequired[3];
int acquiredAll;
lockrequired[0]=cardirection;
//Checking the number of locks required
if (cardirection==0){
lockrequired[1]=3;
}
else{
lockrequired[1]=cardirection-1;
}
if (lockrequired[1]==0){
lockrequired[2]=3;
}
else{
lockrequired[2]=lockrequired[1]-1;
}
message(0,carnumber,cardirection,destdirection);
do{
// if N to E
if(lockrequired[0]==0 && lockrequired[1]==3 && lockrequired[2]==2)
{
if(lock0->owner==NULL)
{
lock_acquire(lock0);
assert(lock_do_i_hold(lock0));
if(lock3->owner==NULL)
{
lock_acquire(lock3);
assert(lock_do_i_hold(lock3));
if(lock2->owner==NULL)
{
lock_acquire(lock2);
assert(lock_do_i_hold(lock2));
message(1,carnumber,cardirection,destdirection);
message(4,carnumber,cardirection,destdirection);
message(3,carnumber,cardirection,destdirection);
lock_release(lock0);
lock_release(lock3);
lock_release(lock2);
acquiredAll=3;
}
else{
lock_release(lock0);
lock_release(lock3);
}
}
else lock_release(lock0);
}
}
// if S to W
else if(lockrequired[0]==2 && lockrequired[1]==1&& lockrequired[2]==0)
{if(lock2->owner==NULL)
{
lock_acquire(lock2);
assert(lock_do_i_hold(lock2));
if(lock1->owner==NULL)
{
lock_acquire(lock1);
assert(lock_do_i_hold(lock1));
if(lock0->owner==NULL)
{
lock_acquire(lock0);
assert(lock_do_i_hold(lock0));
message(3,carnumber,cardirection,destdirection);
message(2,carnumber,cardirection,destdirection);
message(1,carnumber,cardirection,destdirection);
lock_release(lock2);
lock_release(lock1);
lock_release(lock0);
acquiredAll=3;
}
else{
lock_release(lock2);
lock_release(lock1);
}
}
else lock_release(lock2);
}
}
//if E to S
else if(lockrequired[0]==1 && lockrequired[1]==0&& lockrequired[2]==3)
{
if(lock1->owner==NULL)
{
lock_acquire(lock1);
assert(lock_do_i_hold(lock1));
if(lock0->owner==NULL)
{
lock_acquire(lock0);
assert(lock_do_i_hold(lock0));
if(lock3->owner==NULL)
{
lock_acquire(lock3);
assert(lock_do_i_hold(lock3));
message(2,carnumber,cardirection,destdirection);
message(1,carnumber,cardirection,destdirection);
message(4,carnumber,cardirection,destdirection);
lock_release(lock1);
lock_release(lock0);
lock_release(lock3);
acquiredAll=3;
}
else{
lock_release(lock1);
lock_release(lock0);
}
}
else lock_release(lock1);
}
}
//if W to N
else
{
if(lock3->owner==NULL)
{
lock_acquire(lock3);
assert(lock_do_i_hold(lock3));
if(lock2->owner==NULL)
{
lock_acquire(lock2);
assert(lock_do_i_hold(lock2));
if(lock1->owner==NULL)
{
lock_acquire(lock1);
assert(lock_do_i_hold(lock1));
message(4,carnumber,cardirection,destdirection);
message(3,carnumber,cardirection,destdirection);
message(2,carnumber,cardirection,destdirection);
lock_release(lock3);
lock_release(lock2);
lock_release(lock1);
acquiredAll=3;
}
else{
lock_release(lock3);
lock_release(lock2);
}
}
else lock_release(lock3);
}
}
}while(acquiredAll!=3); //while loop till you get all three locks else release all locks
message(5,carnumber,cardirection,destdirection);
count--;
} //end of turnleft
/*
* turnright()
*
* Arguments:
* unsigned long cardirection: the direction from which the car
* approaches the intersection.
* unsigned long carnumber: the car id number for printing purposes.
*
* Returns:
* nothing.
*
* Notes:
* This function should implement making a right turn through the
* intersection from any direction.
* Write and comment this function.
*/
static void turnright(unsigned long cardirection, unsigned long carnumber, unsigned long destdirection){
/*
* Avoid unused variable warnings.
*/
int lockrequired;
lockrequired=cardirection;
message(0,carnumber,cardirection,destdirection);
//checking for locks required and then acquiring them, after use releasing them
if(lockrequired==0)
{
lock_acquire(lock0);
assert(lock_do_i_hold(lock0)==1);
message(1,carnumber,cardirection,destdirection);
lock_release(lock0);
}
else if(lockrequired==1)
{
lock_acquire(lock1);
assert(lock_do_i_hold(lock1)==1);
message(2,carnumber,cardirection,destdirection);
lock_release(lock1);
}
else if(lockrequired==2)
{
lock_acquire(lock2);
assert(lock_do_i_hold(lock2)==1);
message(3,carnumber,cardirection,destdirection);
lock_release(lock2);
}
else
{
lock_acquire(lock3);
assert(lock_do_i_hold(lock3)==1);
message(4,carnumber,cardirection,destdirection);
lock_release(lock3);
}
message(5,carnumber,cardirection,destdirection);
count--;
}
/*
* approachintersection()
*
* Arguments:
* void * unusedpointer: currently unused.
* unsigned long carnumber: holds car id number.
*
* Returns:
* nothing.
*
* Notes:
* Change this function as necessary to implement your solution. These
* threads are created by createcars(). Each one must choose a direction
* randomly, approach the intersection, choose a turn randomly, and then
* complete that turn. The code to choose a direction randomly is
* provided, the rest is left to you to implement. Making a turn
* or going straight should be done by calling one of the functions
* above.
*/
static void approachintersection(void * unusedpointer, unsigned long carnumber)
{
int cardirection;
int destdirection;
int car_no=carnumber;
/*
* Avoid unused variable and function warnings.
*/
(void) unusedpointer;
/*
* cardirection is set randomly.
*/
cardirection = random() % 4;
destdirection = random() % 3;
destdirection = (cardirection + destdirection + 1) % 4;
if(cardirection-destdirection==2 || destdirection-cardirection==2) //if car wants to go straight
gostraight(cardirection,car_no,destdirection);
else if(destdirection-cardirection==1 || cardirection-destdirection==3) //if car wants to turn left
turnleft(cardirection,car_no,destdirection);
else //else the car wants to turn right
turnright(cardirection,car_no,destdirection);
}
/*
* createcars()
*
* Arguments:
* int nargs: unused.
* char ** args: unused.
*
* Returns:
* 0 on success.
*
* Notes:
* Driver code to start up the approachintersection() threads. You are
* free to modiy this code as necessary for your solution.
*/
int createcars(int nargs, char ** args)
{
int index, error;
count=NCARS;
lock0 = lock_create("lock0");
lock1=lock_create("lock1");
lock2=lock_create("lock2");
lock3=lock_create("lock3");
/*
* Avoid unused variable warnings.
*/
(void) nargs;
(void) args;
/*
* Start NCARS approachintersection() threads.
*/
for (index = 0; index < NCARS; index++) {
error = thread_fork("approachintersection thread",
NULL,
index,
approachintersection,
NULL
);
/*
* panic() on error.
*/
if (error) {
panic("approachintersection: thread_fork failed: %s\n",strerror(error));
}
}
while(1)
{
if(count==0)
{
lock_destroy(lock0);
lock_destroy(lock1);
lock_destroy(lock2);
lock_destroy(lock3);
kprintf("All locks destroyed. \n");
break;
}
}
return 0;
}