#include #include #include #include #include "cheer.h" int wTeam = 0; TeamInfo *Bufp; int BufSize = 1; int nMics = 2; int nextYankee = 0; int nextRedSox = 0; int nextCub = 0; int nextMarlin = 0; int Seqno = 0; pthread_mutex_t Mutex; pthread_cond_t MoreMics; void *Yankees(void *arg) { static int x = 0; int whoami = (int) arg; while (!wTeam) { pthread_mutex_lock(&Mutex); if (nMics < BufSize) { char *t_stamp = ctime(&(Bufp[nextYankee].time = time(0))); Bufp[nextYankee].seqno = Seqno; cout << "Go Yankees!" << endl; pthread_cond_wait(&MoreMics, &Mutex); nextYankee = (nextYankee + 1) % BufSize; Seqno++; nMics++; sleep((rand() >= 3) + 1);//sleep random amount pthread_mutex_unlock(&Mutex); } else { sleep((rand() >= 3) + 1);//sleep random amount pthread_mutex_unlock(&Mutex); } } return (NULL); } void *RedSox(void *arg) { static int x = 0; int whoami = (int) arg; while (!wTeam) { pthread_mutex_lock(&Mutex); if (nMics != 0) { const char *t_stamp = ctime(&(Bufp[nextRedSox].time)); cout <<"Go Red Sox!" << endl; pthread_cond_wait(&MoreMics, &Mutex); nextRedSox = (nextRedSox + 1) % BufSize; nMics--; sleep((rand() >= 3) + 1);//sleep random amount pthread_mutex_unlock(&Mutex); } else { sleep((rand() >= 3) + 1);//sleep random amount pthread_mutex_unlock(&Mutex); } } return (NULL); } void *Cubs(void *arg) { static int x = 0; int whoami = (int) arg; while (!wTeam) { pthread_mutex_lock(&Mutex); if (nMics < BufSize) { char *t_stamp = ctime(&(Bufp[nextCub].time = time(0))); Bufp[nextCub].seqno = Seqno; cout << "Go Cubs!" << endl; pthread_cond_wait(&MoreMics, &Mutex); nextCub = (nextCub + 1) % BufSize; Seqno++; nMics++; sleep((rand() >= 3) + 1);//sleep random amount pthread_mutex_unlock(&Mutex); } else { sleep((rand() >= 3) + 1);//sleep random amount pthread_mutex_unlock(&Mutex); } } return (NULL); } void *Marlins(void *arg)//offset here { static int x = 0; int whoami = (int) arg; while (!wTeam) { pthread_mutex_lock(&Mutex); if (nMics != 0) { const char *t_stamp = ctime(&(Bufp[nextMarlin].time)); cout <<"Go Marlins!" << endl; nextMarlin = (nextMarlin + 1) % BufSize; nMics--; pthread_mutex_unlock(&Mutex); } else { pthread_mutex_unlock(&Mutex); sleep((rand() >= 3) + 1);//sleep random amount } } return (NULL); } int main(int argc, char **argv) { int i; int numYankees = 4; int numRedSox = 4; int numCubs = 4; int numMarlins = 4; time_t execTime = 3; cout << "Yankees 1, Red Sox 4, Cubs 0, Marlins 2" << endl; //Parse the command line options prc_args(argc, argv, numYankees, numRedSox, numCubs, numMarlins, execTime); Bufp = new TeamInfo[BufSize];//buffer pthread_mutex_init(&Mutex, NULL); pthread_t ptid[100]; for (i = 0; i < numYankees; i++) { int rtn = pthread_create(&ptid[i], NULL, Yankees, (void *)i); } for (i = 0; i < numRedSox; i++) { int rtn = pthread_create(&ptid[i], NULL, RedSox, (void *)i); } for (i = 0; i < numCubs; i++) { pthread_create(&ptid[i], NULL, Cubs, (void *)i); } for (i = 0; i < numMarlins; i++) { pthread_create(&ptid[i], NULL, Marlins, (void *)i); } sleep(execTime); wTeam = 1; for (i = 0; i < numYankees + numRedSox + numCubs + numMarlins; i++) pthread_join(ptid[i], NULL); cout << "All threads Completed" << endl; exit(0); } /* * Function: prc_args * * Description: Parse and process the command level arguments. */ void prc_args(int argc, char *argv[], int& numYankees, int& numRedSox, int& numCubs, int& numMarlins, time_t& execTime) { register int cc; extern char *optarg; extern int optind; while ( (cc = getopt(argc, argv, "p:r:c:t:n:")) != EOF ) { switch(cc) { case 'y': numYankees = atoi(optarg); break; case 'r': numRedSox = atoi(optarg); break; case 'c': numCubs = atoi(optarg); break; case 'm': numCubs = atoi(optarg); break; case 't': execTime= atoi(optarg); break; case 'n': BufSize = atoi(optarg); break; } } argc -= optind; argv += optind; if (argc != 0) { cerr << "Exit" << endl; exit(2); } }