Mercurial > hg > CbC > old > DPP
comparison tableau3.cbc @ 0:d4bc23cb728b
Import from CVS (CVS_DB/member/atsuki/cbc/DPP)
author | Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 16 Dec 2015 15:16:11 +0900 |
parents | |
children | a15437a1e94c |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d4bc23cb728b |
---|---|
1 /* | |
2 ** Dining Philosophers Problem's scheduler | |
3 ** with state developper as a tableau method | |
4 | |
5 ** 連絡先: 琉球大学情報工学科 河野 真治 | |
6 ** (E-Mail Address: kono@ie.u-ryukyu.ac.jp) | |
7 ** | |
8 ** このソースのいかなる複写,改変,修正も許諾します。ただし、 | |
9 ** その際には、誰が貢献したを示すこの部分を残すこと。 | |
10 ** 再配布や雑誌の付録などの問い合わせも必要ありません。 | |
11 ** 営利利用も上記に反しない範囲で許可します。 | |
12 ** バイナリの配布の際にはversion messageを保存することを条件とします。 | |
13 ** このプログラムについては特に何の保証もしない、悪しからず。 | |
14 ** | |
15 ** Everyone is permitted to do anything on this program | |
16 ** including copying, modifying, improving, | |
17 ** as long as you don't try to pretend that you wrote it. | |
18 ** i.e., the above copyright notice has to appear in all copies. | |
19 ** Binary distribution requires original version messages. | |
20 ** You don't have to ask before copying, redistribution or publishing. | |
21 ** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. | |
22 | |
23 */ | |
24 #include <stdlib.h> | |
25 #include <time.h> | |
26 #include "dpp2.h" | |
27 #include "queue.h" | |
28 #include "memory.h" | |
29 #include "state_db.h" | |
30 #include "ltl.h" | |
31 | |
32 int NUM_PHILOSOPHER = 5; /* A number of philosophers must be more than 2. */ | |
33 | |
34 static code (*ret)(int); | |
35 static void *env; | |
36 | |
37 static PhilsPtr phils_list = NULL; | |
38 | |
39 static int max_step = 100; | |
40 | |
41 static StateDB state_db; | |
42 static MemoryPtr mem; | |
43 static StateNode st; | |
44 static int always_flag; // for LTL | |
45 | |
46 int | |
47 list_length(TaskPtr list) | |
48 { | |
49 int length; | |
50 TaskPtr t; | |
51 | |
52 if (!list) return 0; | |
53 t = list->next; | |
54 | |
55 for (length = 1; t && t != list; length++) { | |
56 t = t->next; | |
57 } | |
58 return length; | |
59 } | |
60 | |
61 TaskPtr | |
62 get_task(int num, TaskPtr list) | |
63 { | |
64 while (num-- > 0) { | |
65 list = list->next; | |
66 } | |
67 return list; | |
68 } | |
69 | |
70 | |
71 static TaskIteratorPtr task_iter; | |
72 static int depth,count; | |
73 | |
74 /* | |
75 Performe depth frist search | |
76 Possible task iterleave is generated by TaskIterator | |
77 (using task ring) | |
78 State are recorded in StateDB | |
79 all memory fragments are regsitered by add_memory_range() | |
80 including task queue | |
81 */ | |
82 | |
83 | |
84 code tableau(TaskPtr list) | |
85 { | |
86 StateDB out; | |
87 | |
88 st.hash = get_memory_hash(mem,0); | |
89 if (lookup_StateDB(&st, &state_db, &out)) { | |
90 // found in the state database | |
91 //printf("found %d\n",count); | |
92 while(!(list = next_task_iterator(task_iter))) { | |
93 // no more branch, go back to the previous one | |
94 TaskIteratorPtr prev_iter = task_iter->prev; | |
95 if (!prev_iter) { | |
96 printf("All done count %d\n",count); | |
97 printf("Number of unique states %d\n", state_count()); | |
98 memory_usage(); | |
99 show_result(always_flag); | |
100 goto ret(0),env; | |
101 } | |
102 //printf("no more branch %d\n",count); | |
103 depth--; | |
104 free_task_iterator(task_iter); | |
105 task_iter = prev_iter; | |
106 } | |
107 // return to previous state | |
108 // here we assume task list is fixed, we don't have to | |
109 // recover task list itself | |
110 restore_memory(task_iter->state->memory); | |
111 //printf("restore list %x next %x\n",(int)list,(int)(list->next)); | |
112 } else { | |
113 // one step further | |
114 depth++; | |
115 task_iter = create_task_iterator(list,out,task_iter); | |
116 } | |
117 //printf("depth %d count %d\n", depth, count++); | |
118 count++; | |
119 goto list->phils->next(list->phils,list); | |
120 } | |
121 | |
122 code get_next_task_fifo(TaskPtr list) | |
123 { | |
124 TaskPtr t = list; | |
125 TaskPtr e; | |
126 | |
127 if (max_step--<0) goto die("Simuration end."); | |
128 | |
129 list = list->next; | |
130 goto list->phils->next(list->phils,list); | |
131 } | |
132 | |
133 code scheduler(PhilsPtr phils, TaskPtr list) | |
134 { | |
135 goto check(&always_flag, phils, list); | |
136 // goto next_next_task_fifo(list); | |
137 } | |
138 | |
139 code task_entry1(int count, PhilsPtr self, TaskPtr list, TaskPtr last); | |
140 | |
141 code task_entry2(int count,PhilsPtr self, TaskPtr list,TaskPtr last, TaskPtr q) | |
142 { | |
143 if (!q) { | |
144 goto die("Can't allocate Task\n"); | |
145 } else { | |
146 add_memory_range(q,sizeof(Task),&mem); | |
147 goto enqueue(count, self, list, last, q, task_entry1); | |
148 } | |
149 } | |
150 | |
151 code task_entry1(int count, PhilsPtr self, TaskPtr list, TaskPtr last) | |
152 { | |
153 StateDB out; | |
154 /* | |
155 printf("int count %d, PhilsPtr self %x, TaskPtr list %x, TaskPtr last %x\n", | |
156 count, self, list, last); | |
157 */ | |
158 | |
159 if (count++ < NUM_PHILOSOPHER) { | |
160 self = self->left; | |
161 goto create_queue(count,self,list,last,task_entry2); | |
162 } else { | |
163 // make circular task list | |
164 last->next = list; | |
165 st.memory = mem; | |
166 st.hash = get_memory_hash(mem,0); | |
167 lookup_StateDB(&st, &state_db, &out); | |
168 task_iter = create_task_iterator(list,out,0); | |
169 // start first task | |
170 goto list->phils->next(list->phils,list); | |
171 } | |
172 } | |
173 | |
174 code task_entry0(int count, PhilsPtr self, TaskPtr list, TaskPtr last, TaskPtr q) | |
175 { | |
176 add_memory_range(q,sizeof(Task),&mem); | |
177 goto task_entry1(count, self, q, q); | |
178 } | |
179 | |
180 code init_final(PhilsPtr self) | |
181 { | |
182 self->right = phils_list; | |
183 phils_list->left = self; | |
184 self->right_fork = phils_list->left_fork; | |
185 always_flag = 1; | |
186 //add_memory_range(&always_flag, sizeof(int), &mem); | |
187 //printf("init all\n"); | |
188 | |
189 goto create_queue(1, self, 0, 0, task_entry0); | |
190 } | |
191 | |
192 code init_phils2(PhilsPtr self, int count, int id) | |
193 { | |
194 PhilsPtr tmp_self; | |
195 | |
196 tmp_self = (PhilsPtr)malloc(sizeof(Phils)); | |
197 if (!tmp_self) { | |
198 goto die("Can't allocate Phils\n"); | |
199 } | |
200 self->right = tmp_self; | |
201 tmp_self->id = id; | |
202 tmp_self->right_fork = NULL; | |
203 tmp_self->left_fork = self->right_fork; | |
204 tmp_self->right = NULL; | |
205 tmp_self->left = self; | |
206 tmp_self->next = pickup_lfork; | |
207 add_memory_range(tmp_self,sizeof(Phils),&mem); | |
208 | |
209 count--; | |
210 id++; | |
211 | |
212 if (count == 0) { | |
213 goto init_final(tmp_self); | |
214 } else { | |
215 goto init_fork2(tmp_self, count, id); | |
216 } | |
217 } | |
218 | |
219 code init_fork2(PhilsPtr self, int count, int id) | |
220 { | |
221 ForkPtr tmp_fork; | |
222 | |
223 tmp_fork = (ForkPtr)malloc(sizeof(Fork)); | |
224 if (!tmp_fork) { | |
225 goto die("Can't allocate Fork\n"); | |
226 } | |
227 tmp_fork->id = id; | |
228 tmp_fork->owner = NULL; | |
229 self->right_fork = tmp_fork; | |
230 add_memory_range(tmp_fork,sizeof(Fork),&mem); | |
231 | |
232 goto init_phils2(self, count, id); | |
233 } | |
234 | |
235 code init_phils1(ForkPtr fork, int count, int id) | |
236 { | |
237 PhilsPtr self; | |
238 | |
239 self = (PhilsPtr)malloc(sizeof(Phils)); | |
240 if (!self) { | |
241 goto die("Can't allocate Phils\n"); | |
242 } | |
243 phils_list = self; | |
244 self->id = id; | |
245 self->right_fork = NULL; | |
246 self->left_fork = fork; | |
247 self->right = NULL; | |
248 self->left = NULL; | |
249 self->next = pickup_lfork; | |
250 add_memory_range(self,sizeof(Phils),&mem); | |
251 | |
252 count--; | |
253 id++; | |
254 | |
255 goto init_fork2(self, count, id); | |
256 } | |
257 | |
258 code init_fork1(int count) | |
259 { | |
260 ForkPtr fork; | |
261 int id = 1; | |
262 | |
263 fork = (ForkPtr)malloc(sizeof(Fork)); | |
264 if (!fork) { | |
265 goto die("Can't allocate Fork\n"); | |
266 } | |
267 fork->id = id; | |
268 fork->owner = NULL; | |
269 add_memory_range(fork,sizeof(Fork),&mem); | |
270 | |
271 goto init_phils1(fork, count, id); | |
272 } | |
273 | |
274 code die(char *err) | |
275 { | |
276 printf("%s\n", err); | |
277 goto ret(1), env; | |
278 } | |
279 | |
280 int main(int ac, char *av[]) | |
281 { | |
282 ret = return; | |
283 env = environment; | |
284 // srand((unsigned)time(NULL)); | |
285 // srandom((unsigned long)time(NULL)); | |
286 reset_state_count(); | |
287 srandom(555); | |
288 | |
289 if (ac==2) { | |
290 NUM_PHILOSOPHER = atoi(av[1]); | |
291 if (NUM_PHILOSOPHER >10 ||NUM_PHILOSOPHER < 2) { | |
292 printf("illegal number of philosopher = %d\n", NUM_PHILOSOPHER ); | |
293 return 1; | |
294 } | |
295 //printf("number of philosopher = %d\n", NUM_PHILOSOPHER ); | |
296 } | |
297 | |
298 goto init_fork1(NUM_PHILOSOPHER); | |
299 } | |
300 | |
301 /* end */ |