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
|
/*
* Copyright (c) 1998, 1999 University of Utah and the Flux Group.
* All rights reserved.
*
* This file is part of the Flux OSKit. The OSKit is free software, also known
* as "open source;" you can redistribute it and/or modify it under the terms
* of the GNU General Public License (GPL), version 2, as published by the Free
* Software Foundation (FSF). To explore alternate licensing terms, contact
* the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
*
* The OSKit 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 GPL for more details. You should have
* received a copy of the GPL along with the OSKit; see the file COPYING. If
* not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
*/
/*
* Dining Philosophers. This example program solves the dining philosophers
* problem using monitors. It is intended as a pthread demonstration, and
* a small torture test of the core pthreads constructs like mutexes,
* conditions, sleep, preemption, thread switching. It also turns on
* priority inheritance in the monitor mutex to test that part of the
* pthreads scheduling code.
*
* This was written by Jamie Painter a long, long time ago ...
*/
#include <stdlib.h>
#include <stdio.h>
#include <oskit/startup.h>
#include <oskit/clientos.h>
#include <oskit/dev/osenv_intr.h>
#include <oskit/threads/pthread.h>
#define mutex_lock pthread_mutex_lock
#define mutex_unlock pthread_mutex_unlock
#define condition_wait pthread_cond_wait
#define condition_signal pthread_cond_signal
#define condition_broadcast pthread_cond_broadcast
#define PHILOSOPHERS 7
/* This is used to ensure each monitor procedure is mutually exclusive*/
pthread_mutex_t monitor_protect;
pthread_cond_t self[PHILOSOPHERS];
enum {THINKING,HUNGRY,EATING} state[PHILOSOPHERS];
char *state_string[] = {"thinking", "hungry ", "eating "};
void random_spin(int max);
/*
* These are to guarantee mutual exclusion to stderr for output
* and to the random clibrary function.
*/
pthread_mutex_t random_protect, stderr_protect;
void test(int k)
{
/*
* Not a monitor entry so we DON'T lock the monitor mutex
* presumably it is already held by our caller.
*/
if (state[(k+(PHILOSOPHERS - 1))%PHILOSOPHERS] != EATING &&
state[k] == HUNGRY &&
state[(k+1)%PHILOSOPHERS] != EATING) {
state[k] = EATING;
condition_signal(&self[k]);
}
}
/* Philospher i picks up a pair of forks */
void pickup(int i)
{
mutex_lock(&monitor_protect);
random_spin(1000);
state[i] = HUNGRY;
test(i);
while (state[i] != EATING)
condition_wait(&self[i], &monitor_protect);
mutex_unlock(&monitor_protect);
}
/* Philospher i puts down his forks */
void putdown(int i)
{
mutex_lock(&monitor_protect);
random_spin(1000);
state[i] = THINKING;
test((i+(PHILOSOPHERS - 1)) %PHILOSOPHERS);
test((i+1) %PHILOSOPHERS);
mutex_unlock(&monitor_protect);
}
void cycle_soaker(void ) {}
/*
* A random wait time mixes things up a bit.
*/
void random_spin(int max)
{
int spin_time;
int i;
max = max * 100;
mutex_lock(&random_protect);
spin_time = rand() % max;
mutex_unlock(&random_protect);
for(i=0; i<spin_time; i++)
cycle_soaker();
}
/*
* Check that current state makes sense.
* i must be EATING. The neighbors must not be.
*
*/
void validate_state(int i)
{
mutex_lock(&monitor_protect);
if (state[i] != EATING ||
state[(i+1)%PHILOSOPHERS] == EATING ||
state[(i+(PHILOSOPHERS - 1))%PHILOSOPHERS] == EATING) {
mutex_lock(&stderr_protect);
printf("Invalid state detect by philosopher %d\n", i );
mutex_unlock(&stderr_protect);
abort();
}
mutex_unlock(&monitor_protect);
}
void *
philosopher(void *arg)
{
int i = (int) arg;
int j;
oskit_pthread_sleep((long long) 1000);
/* a year in a life of a philospher ... */
for(j=0;j<150;j++) {
pickup(i); /* get forks */
validate_state(i);
/* mutex_lock(&stderr_protect);
printf("%d Eating\n", i );
mutex_unlock(&stderr_protect); */
if ((j % 10) == 0) {
int k;
mutex_lock(&stderr_protect);
printf("p:%d(%d) j:%d ", i, oskit_pthread_whichcpu(), j);
for (k = 0; k < PHILOSOPHERS; k++) {
printf("%s ", state_string[state[k]]);
}
printf("\n");
mutex_unlock(&stderr_protect);
}
oskit_pthread_sleep((long long) 100);
/* mutex_lock(&stderr_protect);
printf("%d Thinking\n", i );
mutex_unlock(&stderr_protect); */
putdown(i); /* release forks */
random_spin(500); /* hungry fellow: eats more than he thinks */
}
return 0;
}
int
main()
{
int i;
void *stat;
pthread_t foo[PHILOSOPHERS];
pthread_mutexattr_t mutexattr;
oskit_clientos_init_pthreads();
start_clock();
start_pthreads();
#ifdef GPROF
start_fs_bmod();
start_gprof();
#endif
srand(1);
pthread_mutexattr_init(&mutexattr);
pthread_mutexattr_setprotocol(&mutexattr, 1);
/* pthread_mutex_init(&stderr_protect, 0);
pthread_mutex_init(&random_protect, 0); */
pthread_mutex_init(&monitor_protect, &mutexattr);
for(i=0; i<PHILOSOPHERS; i++) {
state[i] = THINKING;
pthread_cond_init(&self[i], 0);
}
for(i=0; i<PHILOSOPHERS; i++) {
pthread_create(&foo[i], 0, philosopher, (void *) i);
if ((unsigned) i & 1)
oskit_pthread_setprio(foo[i], PRIORITY_NORMAL + 1);
}
for(i=0; i<PHILOSOPHERS; i++) {
pthread_join(foo[i], &stat);
}
oskit_pthread_sleep((long long) 100);
printf("exiting ...\n");
exit(0);
return 0;
}
|