-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_philo.c
214 lines (186 loc) · 6.52 KB
/
test_philo.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
/*
** GNU Pth - The GNU Portable Threads
** Copyright (c) 1999-2006 Ralf S. Engelschall <rse@engelschall.com>
**
** This file is part of GNU Pth, a non-preemptive thread scheduling
** library which can be found at http://www.gnu.org/software/pth/.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
**
** test_philo.c: Pth test application showing the 5 philosopher problem
*/
/* ``It's not enough to be a great programmer;
you have to find a great problem.''
-- Charles Simonyi */
/*
* Reference: E.W. Dijkstra,
* ``Hierarchical Ordering of Sequential Processes'',
* Acta Informatica 1, 1971, 115-138.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include "pth.h"
#include "test_common.h"
#define PHILNUM 5
typedef enum {
thinking,
hungry,
eating
} philstat;
static const char *philstatstr[3] = {
"thinking",
"hungry ",
"EATING "
};
typedef struct tablestruct {
pth_t tid[PHILNUM];
int self[PHILNUM];
pth_mutex_t mutex;
pth_cond_t condition[PHILNUM];
philstat status[PHILNUM];
} table;
static table *tab;
static void printstate(void)
{
int i;
for (i = 0; i < PHILNUM; i++)
printf("| %s ", philstatstr[(int)(tab->status)[i]]);
printf("|\n");
return;
}
static int test(unsigned int i)
{
if ( ((tab->status)[i] == hungry)
&& ((tab->status)[(i + 1) % PHILNUM] != eating)
&& ((tab->status)[(i - 1 + PHILNUM) % PHILNUM] != eating)) {
(tab->status)[i] = eating;
pth_cond_notify(&((tab->condition)[i]), FALSE);
return TRUE;
}
return FALSE;
}
static void pickup(unsigned int k)
{
pth_mutex_acquire(&(tab->mutex), FALSE, NULL);
(tab->status)[k] = hungry;
printstate();
if (!test(k))
pth_cond_await(&((tab->condition)[k]), &(tab->mutex), NULL);
printstate();
pth_mutex_release(&(tab->mutex));
return;
}
static void putdown(unsigned int k)
{
pth_mutex_acquire(&(tab->mutex), FALSE, NULL);
(tab->status)[k] = thinking;
printstate();
test((k + 1) % PHILNUM);
test((k - 1 + PHILNUM) % PHILNUM);
pth_mutex_release(&(tab->mutex));
return;
}
static void *philosopher(void *_who)
{
unsigned int *who = (unsigned int *)_who;
/* For simplicity, all philosophers eat for the same amount of time
and think for a time that is simply related to their position at
the table. The parameter who identifies the philosopher: 0,1,2,.. */
for (;;) {
pth_sleep((*who) + 1);
pickup((*who));
pth_sleep(1);
putdown((*who));
}
return NULL;
}
int main(int argc, char *argv[])
{
int i;
sigset_t ss;
int sig;
pth_event_t ev;
/* initialize Pth library */
pth_init();
/* display test program header */
printf("This is TEST_PHILO, a Pth test showing the Five Dining Philosophers\n");
printf("\n");
printf("This is a demonstration showing the famous concurrency problem of the\n");
printf("Five Dining Philosophers as analysed 1965 by E.W.Dijkstra:\n");
printf("\n");
printf("Five philosophers are sitting around a round table, each with a bowl of\n");
printf("Chinese food in front of him. Between periods of talking they may start\n");
printf("eating whenever they want to, with their bowls being filled frequently.\n");
printf("But there are only five chopsticks available, one each to the left of\n");
printf("each bowl - and for eating Chinese food one needs two chopsticks. When\n");
printf("a philosopher wants to start eating, he must pick up the chopstick to\n");
printf("the left of his bowl and the chopstick to the right of his bowl. He\n");
printf("may find, however, that either one (or even both) of the chopsticks is\n");
printf("unavailable as it is being used by another philosopher sitting on his\n");
printf("right or left, so he has to wait.\n");
printf("\n");
printf("This situation shows classical contention under concurrency (the\n");
printf("philosophers want to grab the chopsticks) and the possibility of a\n");
printf("deadlock (all philosophers wait that the chopstick to their left becomes\n");
printf("available).\n");
printf("\n");
printf("The demonstration runs max. 60 seconds. To stop before, press CTRL-C.\n");
printf("\n");
printf("+----P1----+----P2----+----P3----+----P4----+----P5----+\n");
/* initialize the control table */
tab = (table *)malloc(sizeof(table));
if (!pth_mutex_init(&(tab->mutex))) {
perror("pth_mutex_init");
exit(1);
}
for (i = 0; i < PHILNUM; i++) {
(tab->self)[i] = i;
(tab->status)[i] = thinking;
if (!pth_cond_init(&((tab->condition)[i]))) {
perror("pth_cond_init");
exit(1);
}
}
/* spawn the philosopher threads */
for (i = 0; i < PHILNUM; i++) {
if (((tab->tid)[i] =
pth_spawn(PTH_ATTR_DEFAULT, philosopher,
&((tab->self)[i]))) == NULL) {
perror("pth_spawn");
exit(1);
}
}
/* wait until 60 seconds have elapsed or CTRL-C was pressed */
sigemptyset(&ss);
sigaddset(&ss, SIGINT);
ev = pth_event(PTH_EVENT_TIME, pth_timeout(60,0));
pth_sigwait_ev(&ss, &sig, ev);
pth_event_free(ev, PTH_FREE_ALL);
/* cancel and join the philosopher threads */
for (i = 0; i < PHILNUM; i++)
pth_cancel((tab->tid)[i]);
while (pth_join(NULL, NULL));
/* finish display */
printf("+----------+----------+----------+----------+----------+\n");
/* free the control table */
free(tab);
/* shutdown Pth library */
pth_kill();
return 0;
}