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]); } } }
//-----------------------------------PART 3 int main(){ uthread_init(); struct binary_semaphore semaphore; binary_semaphore_init(&semaphore, 1); uthread_create(&foo, &semaphore); uthread_create(&foo, &semaphore); // uthread_join(2); uthread_exit(); exit(); }
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(); }