Mercurial > hg > CbC > old > DPP
diff scheduler.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 | b15128ab0324 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scheduler.cbc Wed Dec 16 15:16:11 2015 +0900 @@ -0,0 +1,217 @@ +/* +** Dining Philosophers Problem's scheduler +*/ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include "dpp2.h" +#include "queue.h" + +#define NUM_PHILOSOPHER 5 /* A number of philosophers must be more than 2. */ + +code (*ret)(int); +void *env; + +code (*get_next_task)(TaskPtr); +PhilsPtr phils_list = NULL; +int id = 1; /* This is a counter variable to detect a process. */ + +static int max_step = 100; + +/* 環状リストでもlengthを返す */ +int list_length(TaskPtr list) +{ + int length; + TaskPtr q; + if (!list) return 0; + q = list->next; + + for (length = 1; q && q != list; length++) { + q = q->next; + } + return length; +} + +TaskPtr get_task(int num, TaskPtr list) +{ + while (num-- > 0) { + list = list->next; + } + return list; +} + +code get_next_task_random(TaskPtr list) +{ + TaskPtr t = list; + TaskPtr e; + + if (max_step--<0) goto die("Simuration end."); + + // list = list->next; + list = get_task((random()%list_length(list)+1), list); + goto list->phils->next(list->phils,list); +} + +code get_next_task_fifo(TaskPtr list) +{ + TaskPtr t = list; + TaskPtr e; + + if (max_step--<0) goto die("Simuration end."); + + list = list->next; + goto list->phils->next(list->phils,list); +} + +code scheduler(PhilsPtr phils, TaskPtr list) +{ + goto get_next_task(list); +} + +code task_entry1(int count, PhilsPtr self, TaskPtr list, TaskPtr last); + +code task_entry2(int count,PhilsPtr self, TaskPtr list,TaskPtr last, TaskPtr q) +{ + if (!q) { + goto die("Can't allocate Task\n"); + } else { + goto enqueue(count, self, list, last, q, task_entry1); + } +} + +code task_entry1(int count, PhilsPtr self, TaskPtr list, TaskPtr last) +{ +printf("int count %d, PhilsPtr self %x, TaskPtr list %x, TaskPtr last %x\n", +count, self, list, last); + + if (count++ < NUM_PHILOSOPHER) { + self = self->left; + goto create_queue(count,self,list,last,task_entry2); + } else { + last->next = list; + goto scheduler(list->phils,list); + } +} + +code task_entry0(int count, PhilsPtr self, TaskPtr list, TaskPtr last, TaskPtr q) +{ + goto task_entry1(count, self, q, q); +} + +code init_final(PhilsPtr self) +{ + self->right = phils_list; + self->right_fork = phils_list->left_fork; + printf("init all\n"); + + goto create_queue(1, self, 0, 0, task_entry0); +} + +code init_phils2(PhilsPtr self, int count, int id) +{ + PhilsPtr tmp_self; + + tmp_self = (PhilsPtr)malloc(sizeof(Phils)); + if (!tmp_self) { + goto die("Can't allocate Phils\n"); + } + self->right = tmp_self; + tmp_self->id = id; + tmp_self->right_fork = NULL; + tmp_self->left_fork = self->right_fork; + tmp_self->right = NULL; + tmp_self->left = self; + tmp_self->next = thinking; + + count--; + id++; + + if (count == 0) { + goto init_final(tmp_self); + } else { + goto init_fork2(tmp_self, count, id); + } +} + +code init_fork2(PhilsPtr self, int count, int id) +{ + ForkPtr tmp_fork; + + tmp_fork = (ForkPtr)malloc(sizeof(Fork)); + if (!tmp_fork) { + goto die("Can't allocate Fork\n"); + } + tmp_fork->id = id; + tmp_fork->owner = NULL; + self->right_fork = tmp_fork; + + goto init_phils2(self, count, id); +} + +code init_phils1(ForkPtr fork, int count, int id) +{ + PhilsPtr self; + + self = (PhilsPtr)malloc(sizeof(Phils)); + if (!self) { + goto die("Can't allocate Phils\n"); + } + phils_list = self; + self->id = id; + self->right_fork = NULL; + self->left_fork = fork; + self->right = NULL; + self->left = NULL; + self->next = thinking; + + count--; + id++; + + goto init_fork2(self, count, id); +} + +code init_fork1(int count) +{ + ForkPtr fork; + int id = 1; + + fork = (ForkPtr)malloc(sizeof(Fork)); + if (!fork) { + goto die("Can't allocate Fork\n"); + } + fork->id = id; + fork->owner = NULL; + + goto init_phils1(fork, count, id); +} + +code die(char *err) +{ + printf("%s\n", err); + goto ret(1), env; +} + +int main(int argc, char **argv) +{ + ret = return; + env = environment; + // srand((unsigned)time(NULL)); + // srandom((unsigned long)time(NULL)); + get_next_task = get_next_task_fifo; + if (argc == 2) { + if (argv[1][0] == '-') { + if (argv[1][1] == 'r') { + printf("select random\n"); + get_next_task = get_next_task_random; + srandom(time(NULL)); + } else { + printf("FIFO\n"); + get_next_task = get_next_task_fifo; + } + } + } + + goto init_fork1(NUM_PHILOSOPHER); +} + +/* end */