diff --git a/05-JurrasicPark/README.md b/05-JurrasicPark/README.md index 7166614..413dc15 100644 --- a/05-JurrasicPark/README.md +++ b/05-JurrasicPark/README.md @@ -1,33 +1,5 @@ ## Introduction -For this lab you will create a program that add matrices and times the addition just as in lab 3, except in this lab you will do so with threads. +For this lab you will use threads, semaphores, and mutexes to implement a simulation of an ASCII art Jurassic Park! -## Details -You should determine the number of cores on your computer and write a program such that each thread (including the main thread) does and qual amount of computation. - -In other words if I have 4 cores (8 threads) on my computer and an 7x7 matrices (49 values) then each thread should do 49 / 8 (integer division) additions with the main thread picking up the any remainder. Thus, in this case, the main thread does 7 addtions and every other thread 6. - -Assume all matrices will require each thread to do at least 1 addition. - -## Testing and Debugging - -There are [several matrix files](matrices.zip) located provided along with this specification. You can use those to help get your programs working. You are also required to create some of your own and include those in your submission. Don't forget to test large matrices. - -## Deliverables - -You will need to include all your source files and any other resources you used to complete lab. - -A makefile is useful, but optional for this assignment. If you created a makefile, include it in your submission. - -All files should be well documented with function comment blocks and inline comments that explain complicated code routines. While no explicit style guidelines are set for programming in this course, code should be documented and readable. Include your name, section, and lab name in a comment block at the top of each file. - -NOTE: do ***NOT*** submit any IDE configuration files (.idea, project files, etc.). Only submit your source files. - -NOTE: You should ensure that this program compiles without warning (-Wall and -Wextra) prior to submitting. - -Prepare a zip file with all submitted files and upload the file to Canvas per your instructor's instructions. - -## Grading Criteria (100 Points) - -Demonstrate that your lab is working by compiling it, running it, and running any test code in person during lab hours. diff --git a/05-JurrasicPark/park.c b/05-JurrasicPark/park.c index 2b3f3f9..a4ff144 100644 --- a/05-JurrasicPark/park.c +++ b/05-JurrasicPark/park.c @@ -1,4 +1,4 @@ -// os45park.c - Jurassic Park 10/24/2013 +// os45park.c - Jurassic Park 10/24/2013 // *********************************************************************** // ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER ** // ** ** @@ -17,11 +17,13 @@ // *********************************************************************** #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -147,7 +149,7 @@ void *jurassicTask(void *args) // seat open sprintf(buf, "WAIT(seatFilled[%d])", c[i]); ParkDebug(buf); - pthread_mutex_unlock(&seatFilled[c[i]]); + pthread_mutex_lock(&seatFilled[c[i]]); // passenger in seat myPark.cars[c[i]].passengers++; } @@ -189,8 +191,8 @@ void *jurassicTask(void *args) //*********************************************************************** // Move car in Jurassic Park (if possible) // -// return -1 if location occupied -// 0 if move ok +// return -1 if location occupied +// 0 if move ok // 1 if no move // int makeMove(int car) @@ -239,7 +241,7 @@ int makeMove(int car) case 33: { // if there is someone in car and noone is in line, proceed // if ((myPark.cars[car].passengers == NUM_SEATS) || - // ((myPark.numInCarLine == 0) && myPark.cars[car].passengers)) + // ((myPark.numInCarLine == 0) && myPark.cars[car].passengers)) // if car is full, proceed into park if (myPark.cars[car].passengers == NUM_SEATS) { @@ -282,7 +284,6 @@ void *jurassicDisplayTask(void *args) while (!begin) { } - printf("Starting jurassicDisplayTask\n"); // display park every second do @@ -301,6 +302,7 @@ void *jurassicDisplayTask(void *args) } while (myPark.numExitedPark < NUM_VISITORS); // park done + pthread_mutex_unlock(&moveCars); printf("\nThank you for visiting Jurassic Park!!"); return NULL; } // end @@ -308,30 +310,30 @@ void *jurassicDisplayTask(void *args) // Draw Jurassic Park // 1 2 3 4 5 6 7 // 01234567890123456789012345678901234567890123456789012345678901234567890 -// _______________________________________________________ 0 -// Entrance / ++++++++++++++++++++++++++++++++++++++++++| 1 -// - +o0o-o1o-o2o-o3o-o4o-o5o-o6o +| 2 -// ## - /+ / \ +| 3 -// / o + o ************ o +| 4 -// |******** ##>>33 + 23 *Cntrl Room* 7 +| 5 -// |*Ticket* o + o * A=# D1=# * o +| 6 -// |*Booth * 32 + | * B=# D2=# * / \ +| 7 -// |* T=# * ## o + o * C=# D3=# * o o8o-o9o +| 8 -// |* P=## * 31 + 22 * D=# D4=# * 24 \ +| 9 -// |* S=# * o + o ************ o o +| 10 -// |******** ##<<30 + \ / 10 +| 11 -// | o + o21-o20-o27-o26-o25 o +| 12 -// | \+ / \ | +| 13 -// | +o29-o28 o o +| 14 -// | +++++++++++ 19 11 +| 15 -// | + o o +| 16 -// | ## ## + \ / +| 17 -// | ******\ /****\ /******** + o O\ o +| 18 -// | * * * + 18 \/|||\___ 12 +| 19 -// \ * Gifts * Museum * + o x x o +| 20 -// - * # * ## * + \ / +| 21 -// ## - * * * + o17-o16-o15-o14-o13 +| 22 -// \ ************************ ++++++++++++++++++++++++++++++++| 23 +// _______________________________________________________ 0 +// Entrance / ++++++++++++++++++++++++++++++++++++++++++| 1 +// - +o0o-o1o-o2o-o3o-o4o-o5o-o6o +| 2 +// ## - /+ / \ +| 3 +// / o + o ************ o +| 4 +// |******** ##>>33 + 23 *Cntrl Room* 7 +| 5 +// |*Ticket* o + o * A=# D1=# * o +| 6 +// |*Booth * 32 + | * B=# D2=# * / \ +| 7 +// |* T=# * ## o + o * C=# D3=# * o o8o-o9o +| 8 +// |* P=## * 31 + 22 * D=# D4=# * 24 \ +| 9 +// |* S=# * o + o ************ o o +| 10 +// |******** ##<<30 + \ / 10 +| 11 +// | o + o21-o20-o27-o26-o25 o +| 12 +// | \+ / \ | +| 13 +// | +o29-o28 o o +| 14 +// | +++++++++++ 19 11 +| 15 +// | + o o +| 16 +// | ## ## + \ / +| 17 +// | ******\ /****\ /******** + o O\ o +| 18 +// | * * * + 18 \/|||\___ 12 +| 19 +// \ * Gifts * Museum * + o x x o +| 20 +// - * # * ## * + \ / +| 21 +// ## - * * * + o17-o16-o15-o14-o13 +| 22 +// \ ************************ ++++++++++++++++++++++++++++++++| 23 // char *myTime(char *svtime) { diff --git a/05-JurrasicPark/park.h b/05-JurrasicPark/park.h index 9a613b2..45879d9 100644 --- a/05-JurrasicPark/park.h +++ b/05-JurrasicPark/park.h @@ -5,7 +5,7 @@ #define NUM_DRIVERS 4 #define NUM_SEATS 3 -#define NUM_VISITORS (NUM_SEATS * 15) +#define NUM_VISITORS (NUM_SEATS * 20) #define MAX_IN_PARK 20 #define MAX_TICKETS (NUM_CARS * NUM_SEATS) @@ -15,7 +15,7 @@ #define ParkDebug(s) printf("\n%s: %s", "Debug", s); -int begin; +volatile int begin; typedef struct car {