-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphilosopher.c
More file actions
149 lines (124 loc) · 3.2 KB
/
philosopher.c
File metadata and controls
149 lines (124 loc) · 3.2 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
/*
* file: philosopher.c
* description: Dining philosophers, from CS 5600
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include "qthread.h"
int nforks;
qthread_mutex_t m;
qthread_cond_t C[10];
int fork_in_use[10];
double t0;
static void init_time(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
t0 = tv.tv_sec + tv.tv_usec/1.0e6;
}
/* timestamp - time since start - NOT adjusted for speedup
*/
double timestamp(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
double t1 = tv.tv_sec + tv.tv_usec/1.0e6;
return (t1-t0);
}
/* sleep_exp(T) - sleep for exp. dist. time with mean 10T msecs
* unlocks mutex while sleeping if provided.
*/
void sleep_exp(double T)
{
double t = -1 * T * log(drand48()); /* sleep time */
if (t > T*10)
t = T*10;
if (t < 0.5)
t = 0.5;
qthread_usleep((int)(1 * t));
}
/* get_forks() method - called before a philospher starts eating.
* 'i' identifies the philosopher 0..N-1
*/
void get_forks(int i)
{
int left = i, right = (i+1) % nforks;
qthread_mutex_lock(&m);
printf("DEBUG: %f philosopher %d tries for left fork\n", timestamp(), i);
if (fork_in_use[left])
qthread_cond_wait(&C[left], &m);
printf("DEBUG: %f philosopher %d gets left fork\n", timestamp(), i);
fork_in_use[left] = 1;
printf("DEBUG: %f philosopher %d tries for right fork\n", timestamp(), i);
if (fork_in_use[right])
qthread_cond_wait(&C[right], &m);
printf("DEBUG: %f philosopher %d gets left fork\n", timestamp(), i);
fork_in_use[right] = 1;
qthread_mutex_unlock(&m);
}
/* release_forks() - called when a philospher is done eating.
* 'i' identifies the philosopher 0..N-1
*/
void release_forks(int i)
{
int left = i, right = (i+1) % nforks;
qthread_mutex_lock(&m);
printf("DEBUG: %f philosopher %d puts down both forks\n", timestamp(), i);
fork_in_use[left] = 0;
qthread_cond_signal(&C[left]);
fork_in_use[right] = 0;
qthread_cond_signal(&C[right]);
qthread_mutex_unlock(&m);
}
/* the philosopher thread function - create N threads, each of which calls
* this function with its philosopher number 0..N-1
*/
void *philosopher_thread(void *context)
{
int philosopher_num = (int)context; /* hack... */
while (1) {
sleep_exp(4.0);
get_forks(philosopher_num);
sleep_exp(2.5);
release_forks(philosopher_num);
}
return 0;
}
/* handler - ^C will break out of usleep() below; set flag to stop
* loop
*/
static int done;
static void handler(int sig)
{
signal(SIGINT, SIG_DFL);
done = 1;
}
/* wait until ^C
*/
void wait_until_done(void)
{
while (!done)
qthread_usleep(1);
}
int main(int argc, char **argv)
{
int i;
qthread_t t;
signal(SIGINT, handler);
init_time();
nforks = 4;
qthread_mutex_init(&m, NULL);
for (i = 0; i < nforks; i++)
qthread_cond_init(&C[i], NULL);
for (i = 0; i < nforks; i++)
qthread_create(&t, NULL, philosopher_thread, (void*)i);
wait_until_done();
return 0;
}