int stoplight(int nargs, char **args) { (void)nargs; (void)args; int i, direction, turn, err=0; char name[32]; stoplightMenuSemaphore = sem_create("Stoplight Driver Semaphore", 0); if (stoplightMenuSemaphore == NULL) { // 08 Feb 2012 : GWA : Probably out of memory, or you broke our // semaphores! Panicing might be an overreaction, but why not? panic("stoplight: sem_create failed.\n"); } // 13 Feb 2012 : GWA : Students are smarter than me. stoplight_init(); for (i = 0; i < NCARS; i++) { direction = random() % 4; turn = random() % 3; snprintf(name, sizeof(name), "Car Thread %d", i); switch(turn) { case 0: err = thread_fork(name, gostraight, stoplightMenuSemaphore, direction, NULL); break; case 1: err = thread_fork(name, turnleft, stoplightMenuSemaphore, direction, NULL); break; case 2: err = thread_fork(name, turnright, stoplightMenuSemaphore, direction, NULL); break; } } for (i = 0; i < NCARS; i++) { P(stoplightMenuSemaphore); } sem_destroy(stoplightMenuSemaphore); // 13 Feb 2012 : GWA : Students are WAY smarter than me, including Nikhil // Londhe. stoplight_cleanup(); return 0; }
int stoplight(int nargs, char **args) { (void) nargs; (void) args; int i, direction, turn, err = 0; char name[32]; stoplightMenuSemaphore = sem_create("Stoplight Driver Semaphore", 0); if (stoplightMenuSemaphore == NULL ) { panic("stoplight: sem_create failed.\n"); } stoplight_init(); for (i = 0; i < NCARS; i++) { direction = random() % 4; turn = random() % 3; snprintf(name, sizeof(name), "Car Thread %d", i); switch (turn) { random_yielder(PROBLEMS_MAX_YIELDER); case 0: err = thread_fork(name, gostraight, stoplightMenuSemaphore, direction, NULL ); break; case 1: err = thread_fork(name, turnleft, stoplightMenuSemaphore, direction, NULL ); break; case 2: err = thread_fork(name, turnright, stoplightMenuSemaphore, direction, NULL ); break; } } for (i = 0; i < NCARS; i++) { P(stoplightMenuSemaphore); } sem_destroy(stoplightMenuSemaphore); stoplight_cleanup(); return 0; }