// Cup boy simulation void* cup_boy(void){ void* ret_val = 0; cup_boy_lock = binary_semaphore_create(0); int i,n; for(;;){ if(finished_shift){ thread_exit(ret_val); } n =DBB->full->value; for(i = 1; i < n; i++){ struct Cup* current_cup = wash_dirty(); sleep(1); add_clean_cup(current_cup); //need to write to file intsead of screen TODO printf(1,"Cup boy added clean cup %d\n",current_cup->id); } printf(1,"Clean Boy- sleep\n"); binary_semaphore_down(cup_boy_lock); printf(1,"Clean Boy- awake\n"); } return ret_val; }
int main(int argc, char *argv[]) { int i; if(argc != 2) { printf(2, "You must pass the number of soldiers\n"); exit(); } n = atoi(argv[1]); if(n < 1) { printf(2, "Number of soldiers must be positive\n"); exit(); } uthread_init(); for(i = 0; i < n; ++i) { binary_semaphore_init(&soldier_ready[i], 0); binary_semaphore_init(&soldier_waiting[i], 0); binary_semaphore_init(&soldier_prepared[i], 0); binary_semaphore_init(&soldier_update[i], 0); } for(i = 0; i < n; ++i) { uthread_create(run_soldier, (void*)i); } for(;;) { for(i = 0; i < n; ++i) { binary_semaphore_down(&soldier_ready[i]); } print_cells(); if(all_firing()) { exit(); } for(i = 0; i < n; ++i) { binary_semaphore_up(&soldier_waiting[i]); } for(i = 0; i < n; ++i) { binary_semaphore_down(&soldier_prepared[i]); } for(i = 0; i < n; ++i) { binary_semaphore_up(&soldier_update[i]); } } }
void foo(void* sem) { int k; binary_semaphore_down((struct binary_semaphore*)sem); for (k=0; k<20; k++) { printf (1, "foo: tid: %d\n", uthread_self()); } binary_semaphore_up((struct binary_semaphore*)sem); }
void up() { binary_semaphore_down(&departure); counter--; if(counter > 0) { binary_semaphore_up(&departure); } else { binary_semaphore_up(&printing); } }
void down() { binary_semaphore_down(&arrival); counter++; if(counter < soldiersAmount) { binary_semaphore_up(&arrival); } else { binary_semaphore_up(&departure); } }
void run_soldier(void* data) { int i = (int)data; int next; // Set initial state: if (i == 0) { // The general cells[i] = R; } else { // Regular soldiers cells[i] = Q; } binary_semaphore_up(&soldier_ready[i]); while(cells[i] != F) { binary_semaphore_down(&soldier_waiting[i]); // Calculate the next state: next = next_state(i); // Sanity checking: if(next == X) { printf(2, "Error, invalid state transition!\n"); exit(); } binary_semaphore_up(&soldier_prepared[i]); binary_semaphore_down(&soldier_update[i]); cells[i] = next; binary_semaphore_up(&soldier_ready[i]); } }
int main(int argc,char** argv) { int i; if(argc!=2) { printf(1,"please write how much soldiers do you want (LESS THEN 64)\n"); goto out_err; } soldiersAmount=atoi(argv[1]); if(soldiersAmount>64 || soldiersAmount<2) { printf(1,"please write how much soldiers do you want (LESS THEN 64)\n"); goto out_err; } currentState[0][0]=AS; currentState[1][0]=AS; currentState[0][soldiersAmount+1]=AS; currentState[1][soldiersAmount+1]=AS; state=0; counter=0; binary_semaphore_init(&arrival,1); binary_semaphore_init(&departure,0); binary_semaphore_init(&printing,0); fatherPID=getpid(); signal(4,printArray); currentState[0][1]=P; for(i=2; i<=soldiersAmount;i++) { currentState[0][i]=Q; } uthread_init(); int tid; for(i=1;i<=soldiersAmount;i++) { tid = uthread_create(run, (void *) i); if (!tid) goto out_err; } while (currentState[0][i]!=F && currentState[1][i]!=F) { binary_semaphore_down(&printing); printArray(); } printArray(); printArray(); uthread_exit(); out_err: printf(1,"Faild to create thread, we go bye bye\n"); exit(); }