-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphilosopher.c
141 lines (133 loc) · 2.69 KB
/
philosopher.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
#include <stdio.h>
#include <stdlib.h>
#ifdef WIN32
#include <windows.h>
#else
#include <pthread.h>
#include <semaphore.h>
#endif //WIN32
#define N 5
#define L(p) (p)
#define R(p) (((p)+1)%N)
#ifdef WIN32
//#pragma comment( lib, "Kernel32.lib")
#endif //WIN32
#ifdef WIN32
CRITICAL_SECTION chopstick[N];
HANDLE max_into_room;
#else
pthread_mutex_t chopstick[N];
sem_t max_into_room;
#endif //WIN32
void get_stick(int which)
{
#ifdef WIN32
EnterCriticalSection(&chopstick[L(which)]);
EnterCriticalSection(&chopstick[R(which)]);
#else
pthread_mutex_lock(&chopstick[L(which)]);
pthread_mutex_lock(&chopstick[R(which)]);
#endif //WIN32
}
void put_stick(int which)
{
#ifdef WIN32
LeaveCriticalSection(&chopstick[L(which)]);
LeaveCriticalSection(&chopstick[R(which)]);
#else
pthread_mutex_unlock(&chopstick[L(which)]);
pthread_mutex_unlock(&chopstick[R(which)]);
#endif //WIN32
}
#ifdef WIN32
unsigned int WINAPI philosopher(void* arg)
#else
void *philosopher(void *arg)
#endif //WIN32
{
int me = *(int*)arg;
while(1)
{
printf("%s %d", __FUNCTION__, __LINE__);
#ifdef WIN32
WaitForSingleObject(max_into_room, INFINITE);
printf("%s %d", __FUNCTION__, __LINE__);
#else
sem_wait(&max_into_room);
#endif //WIN32
get_stick(me);
printf("philosopher %d eating\n", me);
#ifdef WIN32
Sleep(100000);
#else
usleep(100000);
#endif //WIN32
put_stick(me);
#ifdef WIN32
ReleaseSemaphore(max_into_room, 1, NULL);
#else
sem_post(&max_into_room);
#endif //WIN32
printf("philosopher %d thinking\n", me);
#ifdef WIN32
Sleep(100000);
#else
usleep(100000);
#endif //WIN32
}
#ifdef WIN32
return 0;
#endif //WIN32
}
int main(int argc, char*argv[])
{
int i,arg[N];
#ifdef WIN32
HANDLE pid[N];
#else
pthread_t pid[N];
#endif //WIN32
#ifdef WIN32
max_into_room = CreateSemaphore(NULL, 0, N-1, NULL);
#else
sem_init(&max_into_room, 0, N-1);
#endif //WIN32
for(i = 0; i < N; i+=1)
#ifdef WIN32
InitializeCriticalSection(&chopstick[i]);
#else
pthread_mutex_init(&chopstick[i], NULL);
#endif //WIN32
for(i = 0; i < N; i+=1)
{
arg[i] = i;
#ifdef WIN32
pid[i] = CreateThread(NULL, 0, philosopher, &arg[i], 0/*CREATE_SUSPENDED*/, NULL);
#else
pthread_create(&pid[i], NULL, philosopher, &arg[i]);
#endif //WIN32
}
for(i = 0; i < N; i+=1)
#ifdef WIN32
ResumeThread(pid[i]);
#else
pthread_join(pid[i], NULL);
#endif //WIN32
#ifdef WIN32
while(1)
{
Sleep(100000);
}
#endif //WIN32
#ifdef WIN32
CloseHandle(max_into_room);
#else
sem_destroy(&max_into_room);
#endif //WIN32
#ifdef WIN32
for (i = 0; i < N; i += 1)
{
DeleteCriticalSection(&chopstick[i]);
}
#endif //WIN32
}