-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaltrain.c
60 lines (52 loc) · 1.25 KB
/
caltrain.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
#include "pintos_thread.h"
struct lock mux;//global lock
struct condition wait_c;//waiting condition
struct condition board_c;//boarding condition
struct station {
int n_passengers;//passengers count
int avail_seat;//seat remaining
};
void
station_init(struct station *station)
{
station->n_passengers = 0;
station->avail_seat = 0;
lock_init(&mux);
cond_init(&board_c);
cond_init(&wait_c);
}
void
station_load_train(struct station *station, int count)
{
lock_acquire(&mux);
station->avail_seat = count;
// printf("Passenger boarding!\n");
//when no passenger or no seats, end while loop
while((station->n_passengers != 0)&&(station->avail_seat != 0)){
cond_signal(&wait_c,&mux);
cond_wait(&board_c,&mux);
}
lock_release(&mux);//
return 0;
}
void
station_wait_for_train(struct station *station)
{
lock_acquire(&mux);//lock
station->n_passengers++;//exclusive session
// printf("new passenger!\n");
cond_wait(&wait_c, &mux);
lock_release(&mux);//release
return 0;
}
void
station_on_board(struct station *station)
{
lock_acquire(&mux);
cond_signal(&board_c,&mux);
station->n_passengers--;
station->avail_seat--;
// printf("%d passengers remaining, %d seats available\n",station->n_passengers, station->avail_seat);
lock_release(&mux);
return 0;
}