-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdp_serial.c
134 lines (118 loc) · 3.67 KB
/
dp_serial.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
/*******************************************************************************
* File: serial.c
* Serializer. CSE 511 Project
*
* All parallel code are summed up in these two words: sem_wait and sem_post!
*
* Copyright (C) Oct.2015 Haibo, Chen
*
* This program is not free software: you can not redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, neither version 3 of the License, nor
* (at your option) any later version.
*
* This program is not 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 General Public License for more details.
*
* You will not receive a copy of the GNU General Public License
* along with this program.
*
* Contact Information:
* Haibo <[email protected]>
* 111 IST, University Park, PA, 16302
*
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <assert.h>
#include "dp_serial.h"
enum FORKSTATUS{
kIDLE = 0,
kBUSY = 1
};
int np;
/*extern serial_t* gs;*/
void Init_dp(int nphilosophers)
{
np = nphilosophers;
serializer = Create_Serial();
waiting_q = Create_Queue(serializer);
eating_crowd = Create_Crowd(serializer);
thinking_crowd = Create_Crowd(serializer);
eating_crowd->fork = (int *) malloc (np * sizeof(int));
memset(eating_crowd->fork, '\0', np *sizeof(int)); // fork is kIDLEing;
/* gs = serializer;
gs->rd_crowd = eating_crowd;
gs->wr_crowd = thinking_crowd; */
return ;
}
cond_t eat_cond(int tid, crowd_t* c)
{
int left = ((tid - 1) + np) % np;
int right = ((tid) + np) % np;
pthread_mutex_lock(&(eating_crowd->s_mutex));
if ((c->fork)[left] != kBUSY && (c->fork)[right] != kBUSY)
{
(c->fork)[left] = kBUSY; (c->fork)[right] = kBUSY;
pthread_mutex_unlock(&(eating_crowd->s_mutex));
return 1;
}
else
{
pthread_mutex_unlock(&(eating_crowd->s_mutex));
return 0;
}
}
cond_t think_cond(int tid, crowd_t* c)
{
return 1;
}
void Eat(int phil_id, void *(*model_eat)())
{
cond_t eat_wrapper()
{
return eat_cond(phil_id, eating_crowd);
}
void* meat_wrapper()
{
model_eat(phil_id);
return NULL;
}
Serial_Enter(serializer);
Serial_Enqueue(serializer, waiting_q, &eat_wrapper, 0);
Serial_Join_Crowd(serializer, eating_crowd, &meat_wrapper);
int left = ((phil_id - 1) + np) % np;
int right = ((phil_id) + np) % np;
int nbusy = 0;
int forkId = 0;
pthread_mutex_lock(&(eating_crowd->s_mutex));
for (forkId = 0; forkId < np; forkId++)
if ((eating_crowd->fork)[forkId] == kBUSY)
nbusy++;
assert(nbusy <= (np - 1));
(eating_crowd->fork)[left] = kIDLE; (eating_crowd->fork)[right] = kIDLE;
pthread_mutex_unlock(&(eating_crowd->s_mutex));
Serial_Exit(serializer);
return ;
}
void Think(int phil_id, void *(*model_think)())
{
cond_t think_wrapper()
{
return think_cond(phil_id, thinking_crowd);;
}
void* mthink_wrapper()
{
model_think(phil_id);
return NULL;
}
Serial_Enter(serializer);
Serial_Enqueue(serializer, waiting_q, &think_wrapper, 0);
Serial_Join_Crowd(serializer, thinking_crowd, &mthink_wrapper);
Serial_Exit(serializer);
return ;
}