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
|
/* linux-dp.c --- dining philosophers, on LinuxThreads
Jim Blandy <jimb@cygnus.com> --- March 1999 */
/* It's okay to edit this file and shift line numbers around. The
tests use gdb_get_line_number to find source locations, so they
don't depend on having certain line numbers in certain places. */
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
/* The number of philosophers at the table. */
int num_philosophers;
/* Mutex ordering -
If you want to lock a mutex M, all the mutexes you have locked
already must appear before M on this list.
fork_mutex[0]
fork_mutex[1]
...
fork_mutex[num_philosophers - 1]
stdout_mutex
random_mutex
*/
/* You must hold this mutex while writing to stdout. */
pthread_mutex_t stdout_mutex;
/* You must hold this mutex while calling any of the random number
generation routines. */
pthread_mutex_t random_mutex;
/* array of mutexes, one for each fork; fork_mutex[i] is to the left
of philosopher i. A philosopher is holding fork i iff his/her
thread has locked fork_mutex[i]. */
pthread_mutex_t *fork_mutex;
/* array of threads, one representing each philosopher. */
pthread_t *philosophers;
void *
xmalloc (size_t n)
{
void *p = malloc (n);
if (! p)
{
fprintf (stderr, "out of memory\n");
exit (2);
}
return p;
}
void
shared_printf (char *format, ...)
{
va_list ap;
va_start (ap, format);
pthread_mutex_lock (&stdout_mutex);
vprintf (format, ap);
pthread_mutex_unlock (&stdout_mutex);
va_end (ap);
}
int
shared_random ()
{
int result;
pthread_mutex_lock (&random_mutex);
result = rand ();
pthread_mutex_unlock (&random_mutex);
return result;
}
void
my_usleep (long usecs)
{
struct timeval timeout;
timeout.tv_sec = usecs / 1000000;
timeout.tv_usec = usecs % 1000000;
select (0, 0, 0, 0, &timeout);
}
void
random_delay ()
{
my_usleep ((shared_random () % 2000) * 100);
}
void
print_philosopher (int n, char left, char right)
{
int i;
shared_printf ("%*s%c %d %c\n", (n * 4) + 2, "", left, n, right);
}
void *
philosopher (void *data)
{
int n = * (int *) data;
print_philosopher (n, '_', '_');
#if 1
if (n == num_philosophers - 1)
for (;;)
{
/* The last philosopher is different. He goes for his right
fork first, so there is no cycle in the mutex graph. */
/* Grab the right fork. */
pthread_mutex_lock (&fork_mutex[(n + 1) % num_philosophers]);
print_philosopher (n, '_', '!');
random_delay ();
/* Then grab the left fork. */
pthread_mutex_lock (&fork_mutex[n]);
print_philosopher (n, '!', '!');
random_delay ();
print_philosopher (n, '_', '_');
pthread_mutex_unlock (&fork_mutex[n]);
pthread_mutex_unlock (&fork_mutex[(n + 1) % num_philosophers]);
random_delay ();
}
else
#endif
for (;;)
{
/* Grab the left fork. */
pthread_mutex_lock (&fork_mutex[n]);
print_philosopher (n, '!', '_');
random_delay ();
/* Then grab the right fork. */
pthread_mutex_lock (&fork_mutex[(n + 1) % num_philosophers]);
print_philosopher (n, '!', '!');
random_delay ();
print_philosopher (n, '_', '_');
pthread_mutex_unlock (&fork_mutex[n]);
pthread_mutex_unlock (&fork_mutex[(n + 1) % num_philosophers]);
random_delay ();
}
return (void *) 0;
}
int
main (int argc, char **argv)
{
num_philosophers = 5;
/* Set up the mutexes. */
{
pthread_mutexattr_t ma;
int i;
pthread_mutexattr_init (&ma);
pthread_mutex_init (&stdout_mutex, &ma);
pthread_mutex_init (&random_mutex, &ma);
fork_mutex = xmalloc (num_philosophers * sizeof (fork_mutex[0]));
for (i = 0; i < num_philosophers; i++)
pthread_mutex_init (&fork_mutex[i], &ma);
pthread_mutexattr_destroy (&ma);
}
/* Set off the threads. */
{
int i;
int *numbers = xmalloc (num_philosophers * sizeof (*numbers));
pthread_attr_t ta;
philosophers = xmalloc (num_philosophers * sizeof (*philosophers));
pthread_attr_init (&ta);
for (i = 0; i < num_philosophers; i++)
{
numbers[i] = i;
/* linuxthreads.exp: create philosopher */
pthread_create (&philosophers[i], &ta, philosopher, &numbers[i]);
}
pthread_attr_destroy (&ta);
}
/* linuxthreads.exp: info threads 2 */
sleep (1000000);
/* Drink yourself into oblivion. */
for (;;)
sleep (1000000);
return 0;
}
|