-
Notifications
You must be signed in to change notification settings - Fork 0
/
DinningPhilosopher.c
269 lines (207 loc) · 6.19 KB
/
DinningPhilosopher.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <pthread.h>
#include "msecond.h"
#include "random_int.h"
#define NUM_PHILOSOPHERS 5
#define MEAN_THINK_TIME 1000
#define MEAN_EAT_TIME 750
float total_time_spent_waiting = 0.0;
int total_number_of_meals = 0;
/*--------------------------------------------------------------------------*/
#define semaphore_create(s,v) sem_init( &s, 0, v )
#define semaphore_wait(s) sem_wait( &s )
#define semaphore_signal(s) sem_post( &s )
#define semaphore_release(s) sem_destroy( &s )
typedef sem_t semaphore;
semaphore chopstick[NUM_PHILOSOPHERS];
semaphore screen;
semaphore mutex;
int screen_row[NUM_PHILOSOPHERS] = { 6, 2, 2, 6, 10 };
int screen_col[NUM_PHILOSOPHERS] = { 31, 36, 44, 49, 40 };
int chopstick_row[5] = { 9, 4, 3, 4, 9 };
int chopstick_col[5] = { 35, 33, 40, 47, 45 };
char chopstick_sym[5] = { '/', '\\', '|', '/', '\\' };
#define cls() printf( "\033[H\033[J" )
#define position(row,col) printf( "\033[%d;%dH", (row), (col) )
#define position_flush(row,col) printf( "\033[%d;%dH\n", (row), (col) )
void init_screen( void )
{
int i;
cls();
position( 6, 37 );
printf( "\\ /" );
position( 7, 38 );
printf( "\\___/" );
for ( i = 0; i < NUM_PHILOSOPHERS; i++ )
{
position( screen_row[i], screen_col[i] );
printf( "%d", i );
position( screen_row[i] + 1, screen_col[i] - 1 );
printf( "(%d)", 0 );
position( chopstick_row[i], chopstick_col[i] );
printf( "%c", chopstick_sym[i] );
}
position_flush( 13, 1 );
}
void draw_thinking( int n )
{
semaphore_wait( screen );
position( screen_row[n], screen_col[n] );
printf( "\033[33mT\033[0m" );
position_flush( 13, 1 );
semaphore_signal( screen );
}
void draw_hungry( int n )
{
semaphore_wait( screen );
position( screen_row[n], screen_col[n] );
printf( "\033[34mH\033[0m" );
position_flush( 13, 1 );
semaphore_signal( screen );
}
void draw_eating( int n, int eat_count )
{
semaphore_wait( screen );
position( screen_row[n], screen_col[n] );
printf( "\033[31mE\033[0m" );
position( screen_row[n] + 1, screen_col[n] - 1 );
printf( "(%d)", eat_count );
position_flush( 13, 1 );
semaphore_signal( screen );
}
void draw_chopstick_up( int n )
{
semaphore_wait( screen );
position( chopstick_row[n], chopstick_col[n] );
printf( "%c", ' ' );
position_flush( 13, 1 );
semaphore_signal( screen );
}
void draw_chopstick_down( int n )
{
semaphore_wait( screen );
position( chopstick_row[n], chopstick_col[n] );
printf( "\033[32m%c\033[0m", chopstick_sym[n] );
position_flush( 13, 1 );
semaphore_signal( screen );
}
void draw_done( int n )
{
semaphore_wait( screen );
position( screen_row[n], screen_col[n] );
printf( "D" );
position_flush( 13, 1 );
semaphore_signal( screen );
}
/*--------------------------------------------------------------------------*/
void obtain_chopsticks( int n )
{
if ( n % 2 == 0 ) {
/* Even number: Left, then right */
semaphore_wait( chopstick[(n+1) % NUM_PHILOSOPHERS] );
draw_chopstick_up( (n+1) % NUM_PHILOSOPHERS );
semaphore_wait( chopstick[n] );
draw_chopstick_up( n );
} else {
/* Odd number: Right, then left */
semaphore_wait( chopstick[n] );
draw_chopstick_up( n );
semaphore_wait( chopstick[(n+1) % NUM_PHILOSOPHERS] );
draw_chopstick_up( (n+1) % NUM_PHILOSOPHERS );
}
}
void release_chopsticks( int n )
{
draw_chopstick_down( n );
semaphore_signal( chopstick[n] );
draw_chopstick_down( (n+1) % NUM_PHILOSOPHERS );
semaphore_signal( chopstick[(n+1) % NUM_PHILOSOPHERS] );
}
/*--------------------------------------------------------------------------*/
void philosopher( int *philosopher_data )
{
int start_time;
int eat_count = 0;
int total_hungry_time = 0;
int became_hungry_time;
int n = philosopher_data[0];
int duration = philosopher_data[1];
start_time = msecond();
while( msecond() - start_time < duration * 1000 )
{
/* Hungry */
became_hungry_time = msecond();
draw_hungry( n );
obtain_chopsticks( n );
/* Eating */
total_hungry_time += ( msecond() - became_hungry_time );
eat_count++;
draw_eating( n, eat_count );
usleep( 1000L * random_int( MEAN_EAT_TIME ) ); /* microseconds */
release_chopsticks( n );
/* Think */
draw_thinking( n );
usleep( 1000L * random_int( MEAN_THINK_TIME ) );/* microseconds */
}
/* Done */
draw_done( n );
/* Update the shared variable database */
semaphore_wait( mutex );
total_number_of_meals += eat_count;
total_time_spent_waiting += ( total_hungry_time / 1000.0 );
semaphore_signal( mutex );
pthread_exit( NULL );
}
/*==========================================================================*/
int main( int argc, char *argv[] )
{
pthread_t phil[NUM_PHILOSOPHERS];
int philosopher_data[NUM_PHILOSOPHERS][2];
int duration;
int i;
duration = ( argc > 1 ? atoi( argv[1] ) : 10 );
for ( i = 0; i < NUM_PHILOSOPHERS; i++ )
{
if ( semaphore_create( chopstick[i], 1 ) < 0 ) {
fprintf( stderr, "cannot create chopstick semaphore\n" );
exit( 1 );
}
}
if ( semaphore_create( screen, 1 ) < 0 ) {
fprintf( stderr, "cannot create screen semaphore\n" );
exit( 1 );
}
if ( semaphore_create( mutex, 1 ) < 0 ) {
fprintf( stderr, "cannot create mutex semaphore\n" );
exit( 1 );
}
init_screen();
for ( i = 0; i < NUM_PHILOSOPHERS; i++ )
{
philosopher_data[i][0] = i;
philosopher_data[i][1] = duration;
if ( pthread_create( &phil[i], NULL, (void *(*)(void *)) &philosopher,
&philosopher_data[i] ) != 0 ) {
fprintf( stderr, "cannot create thread for philosopher %d\n", i );
exit( 1 );
}
}
for ( i = 0; i < NUM_PHILOSOPHERS; i++ )
{
pthread_join( phil[i], NULL );
}
for ( i = 0; i < NUM_PHILOSOPHERS; i++ )
{
semaphore_release( chopstick[i] );
}
semaphore_release( screen );
semaphore_release( mutex );
position( 13, 1 );
printf( "Total meals served = %d\n", total_number_of_meals );
printf( "Average hungry time = %f\n",
total_time_spent_waiting / total_number_of_meals );
return 0;
}