/*1* 08 Feb 2012 : GWA : Please make any changes necessary to test your code to2* the drivers in this file. However, the automated testing suite *will3* replace this file in its entirety* with driver code intented to stress4* test your synchronization problem solutions.5*/67#include <types.h>8#include <lib.h>9#include <thread.h>10#include <test.h>11#include <current.h>12#include <synch.h>1314/*15* 08 Feb 2012 : GWA : Driver code for the whalemating problem.16*/1718/*19* 08 Feb 2012 : GWA : The following functions are for you to use when each20* whale starts and completes either mating (if it is a male or female) or21* matchmaking. We will use the output from these functions to verify the to22* verify the correctness of your solution. These functions may spin for23* arbitrary periods of time or yield.24*/2526void27male_start(void)28{29kprintf("male %s starting\n", curthread->t_name);30}31void32male_end(void)33{34kprintf("male %s ending\n", curthread->t_name);35}36void37female_start(void)38{39kprintf("female %s starting\n", curthread->t_name);40}41void42female_end(void)43{44kprintf("female %s ending\n", curthread->t_name);45}46void47matchmaker_start(void)48{49kprintf("matchmaker %s starting\n", curthread->t_name);50}51void52matchmaker_end(void)53{54kprintf("matchmaker %s ending\n", curthread->t_name);55}5657/*58* 08 Feb 2012 : GWA : The following function drives the entire whalemating59* process. Feel free to modify at will, but make no assumptions about the60* order or timing of threads launched by our testing suite.61*/6263#define NMATING 106465struct semaphore * whalematingMenuSemaphore;6667int68whalemating(int nargs, char **args)69{7071int i, j, err=0;7273(void)nargs;74(void)args;7576whalematingMenuSemaphore = sem_create("Whalemating Driver Semaphore", 0);77if (whalematingMenuSemaphore == NULL) {7879// 08 Feb 2012 : GWA : Probably out of memory, or you broke our80// semaphores! Panicing might be an overreaction, but why not?8182panic("whalemating: sem_create failed.\n");83}8485// 13 Feb 2012 : GWA : Students are smarter than me.86whalemating_init();8788for (i = 0; i < 3; i++) {89for (j = 0; j < NMATING; j++) {90switch(i) {91case 0:92err = thread_fork("Male Whale Thread",93male, whalematingMenuSemaphore, j, NULL);94break;95case 1:96err = thread_fork("Female Whale Thread",97female, whalematingMenuSemaphore, j, NULL);98break;99case 2:100err = thread_fork("Matchmaker Whale Thread",101matchmaker, whalematingMenuSemaphore, j, NULL);102break;103}104if (err) {105panic("whalemating: thread_fork failed: (%s)\n",106strerror(err));107}108}109}110for (i = 0; i < 3; i++) {111for (j = 0; j < NMATING; j++) {112P(whalematingMenuSemaphore);113}114}115sem_destroy(whalematingMenuSemaphore);116117// 13 Feb 2012 : GWA : Students are WAY smarter than me, including Nikhil118// Londhe.119whalemating_cleanup();120121return 0;122}123124/*125* 08 Feb 2012 : GWA : Driver code for the stoplight problem.126*/127128/*129* 08 Feb 2012 : GWA : The following functions should be called by your130* stoplight solution when a car is in an intersection quadrant. The131* semantics of the problem are that once a car enters any quadrant it has to132* be somewhere in the intersection until it call leaveIntersection(), which133* it should call while in the final quadrant.134*135* As an example, let's say a car approaches the intersection and needs to136* pass through quadrants 0, 3 and 2. Once you call inQuadrant(0), the car is137* considered in quadrant 0 until you call inQuadrant(3). After you call138* inQuadrant(2), the car is considered in quadrant 2 until you call139* leaveIntersection().140*141* As in the whalemating example, we will use the output from these functions142* to verify the correctness of your solution. These functions may spin for143* arbitrary periods of time or yield.144*/145146void147inQuadrant(int quadrant)148{149kprintf("car %s in quadrant %d\n", curthread->t_name, quadrant);150return;151}152153void leaveIntersection()154{155kprintf("car %s left the intersection\n", curthread->t_name);156return;157}158159#define NCARS 10160161struct semaphore * stoplightMenuSemaphore;162163int164stoplight(int nargs, char **args)165{166(void)nargs;167(void)args;168int i, direction, turn, err=0;169char name[32];170171stoplightMenuSemaphore = sem_create("Stoplight Driver Semaphore", 0);172if (stoplightMenuSemaphore == NULL) {173174// 08 Feb 2012 : GWA : Probably out of memory, or you broke our175// semaphores! Panicing might be an overreaction, but why not?176177panic("stoplight: sem_create failed.\n");178}179180// 13 Feb 2012 : GWA : Students are smarter than me.181stoplight_init();182183for (i = 0; i < NCARS; i++) {184185direction = random() % 4;186turn = random() % 3;187188snprintf(name, sizeof(name), "Car Thread %d", i);189190switch(turn) {191case 0:192err = thread_fork(name, gostraight, stoplightMenuSemaphore,193direction, NULL);194break;195case 1:196err = thread_fork(name, turnleft, stoplightMenuSemaphore, direction,197NULL);198break;199case 2:200err = thread_fork(name, turnright, stoplightMenuSemaphore, direction,201NULL);202break;203}204}205206for (i = 0; i < NCARS; i++) {207P(stoplightMenuSemaphore);208}209210sem_destroy(stoplightMenuSemaphore);211212// 13 Feb 2012 : GWA : Students are WAY smarter than me, including Nikhil213// Londhe.214stoplight_cleanup();215216return 0;217}218219220