diff --git a/baboons.c b/baboons.c index 439bd38..b9a39f6 100644 --- a/baboons.c +++ b/baboons.c @@ -1,4 +1,3 @@ - #include #include #include @@ -11,48 +10,68 @@ #include ////Create semaphores//// -sem_t rope; // The rope sempahore ensures only one side has the rope at a time +sem_t rope; // The rope sempahore blocks baboons from entering the rope from both sides sem_t right_to_left_mutex; // The right_to_left_mutex ensures that the queue // stays in order while crossing right to left sem_t left_to_right_mutex; // The left_to_right_mutex ensures that the queue // stays in order while crossing left to right -sem_t mutex; // The mutex semaphore prevents deadlock -sem_t counter; // Ensure no more than 3 baboons are on the rope at any time +sem_t mutex; // The mutex semaphore prevents one side from holding the rope indefinently +sem_t capacity; // The capacity semaphore prevents more than 3 baboons from being on the rope at once -int left = 0; -int right = 0; +int left = 0; //The left and right variables are used to check if the rope is free +int right = 0; // to control if the rope semaphore needs to be posted or wait int cross_time; // Time to wait while baboons cross +//The function to run when a baboon is crossing from the left side to the right side void *left_to_right(void *baboonnum ) { - + //Wait until mutex and left_to_right_mutex are available sem_wait( &mutex ); sem_wait( &left_to_right_mutex ); + //Increment the value in left left++; + //If the value in left is 1, that means there are no + // baboons going from left_to_right currently, so the function + // must check that the rope is not being used by the other side if ( left == 1 ) { sem_wait( &rope ); } + // Create variables to hold the baboon number and + // how many baboons are currently on the rope int *num = (int*) baboonnum; int numonrope; + //Post to the mutex semaphores to allow another thread to start + // however because the rope semaphore was not posted, the other side + // cannot start until this side finishes sem_post( &left_to_right_mutex ); sem_post( &mutex ); - sem_wait( &counter ); - sem_getvalue( &counter, &numonrope ); + //Check that capacity is not empty, which keeps the rope + // from having more than 3 baboons on at once + sem_wait( &capacity ); + sem_getvalue( &capacity, &numonrope ); + //The print statement simulates the crossing beginning printf( "Baboon#%d has begun crossing, there are %d baboons on the rope going from left to right\n\n", *num, 3 - numonrope ); + //Sleep for the given amount time provided by the user sleep( cross_time ); - sem_getvalue( &counter, &numonrope ); + sem_getvalue( &capacity, &numonrope ); + //Print once the baboon has finished crossing printf( "Baboon#%d just finished crossing left to right, there are %d on the " "rope\n\n", *num, 2 - numonrope); - sem_post( &counter ); + //Post back to the capacity once a baboon leaves the rope + sem_post( &capacity ); + //Decrement the value in left sem_wait( &left_to_right_mutex ); left--; + //If left is 0, it means there are no baboons currently crossing from left_to_right + // and that the rope can be incremented to allow the other side to start crossing if ( left == 0 ) sem_post( &rope ); sem_post( &left_to_right_mutex ); pthread_exit( NULL ); } - +//This function is identical to the left_to_right function except +// going the other direction void *right_to_left( void *baboonnum ) { sem_wait( &mutex ); @@ -65,16 +84,16 @@ void *right_to_left( void *baboonnum ) { int numonrope; sem_post( &right_to_left_mutex ); sem_post( &mutex ); - sem_wait( &counter ); - sem_getvalue( &counter, &numonrope ); + sem_wait( &capacity ); + sem_getvalue( &capacity, &numonrope ); printf( "Baboon#%d has begun crossing, there are %d baboons on the rope going from right to left\n\n", *num, 3 - numonrope ); sleep( cross_time ); - sem_getvalue( &counter, &numonrope ); + sem_getvalue( &capacity, &numonrope ); printf( "Baboon#%d just finished crossing right to left, there are %d on the " "rope\n\n", *num, 2 - numonrope); - sem_post( &counter ); + sem_post( &capacity ); sem_wait( &right_to_left_mutex ); right--; if ( right == 0 ) @@ -92,7 +111,7 @@ int main( int argc, char **argv ) { int lcount = 0; // Used to determine how many baboons are going in each // direction in the queue int rcount = 0; - char direction; + char direction; //Used to hold the value of the character being read from the file // Check that arguments are provided if ( argc < 3 ) { printf( "Please enter a file name followed by an integer 1-10 which " @@ -103,7 +122,6 @@ int main( int argc, char **argv ) { file = fopen( argv[1], "r" ); // Open file to read order of baboons from int gettime = atoi( argv[2] ); cross_time = gettime; - *timetocross = gettime; // Set time to cross variable // Fill the queue of baboons based on the order provided from the file while ( ( fscanf( file, "%c", &direction ) != EOF ) ) { @@ -129,16 +147,16 @@ int main( int argc, char **argv ) { } printf("\n\nIt will take each baboon %d seconds to cross\n\n", cross_time); // Initialize correct number of threads for each side - pthread_t l_to_r[lcount]; + pthread_t l_to_r_thread[lcount]; int left_thread_count = 0; - pthread_t r_to_l[rcount]; + pthread_t r_to_l_thread[rcount]; int right_thread_count = 0; // Initialize semaphores sem_init( &rope, 0, 1 ); sem_init( &right_to_left_mutex, 0, 1 ); sem_init( &left_to_right_mutex, 0, 1 ); sem_init( &mutex, 0, 1 ); - sem_init( &counter, 0, 3 ); + sem_init( &capacity, 0, 3 ); // Create threads based on order of queue for ( int i = 0; i < babooncount; i++ ) { int *tharg = (int*)malloc(sizeof(*tharg)); @@ -147,22 +165,22 @@ int main( int argc, char **argv ) { if ( baboons[i] == 'L' ) { // printf("Left baboon\n"); printf( "Baboon#%d is waiting to cross from left to right\n\n", i ); - pthread_create( &l_to_r[left_thread_count], NULL, + pthread_create( &l_to_r_thread[left_thread_count], NULL, left_to_right, tharg); left_thread_count++; } else if ( baboons[i] == 'R' ) { printf( "Baboon#%d is waiting to cross from right to left\n\n", i ); - pthread_create( &r_to_l[right_thread_count], NULL, + pthread_create( &r_to_l_thread[right_thread_count], NULL, right_to_left, tharg); right_thread_count++; } } // Wait for threads to finish for ( int j = 0; j < left_thread_count; j++ ) { - pthread_join( l_to_r[j], NULL ); + pthread_join( l_to_r_thread[j], NULL ); } for ( int j = 0; j < right_thread_count; j++ ) { - pthread_join( r_to_l[j], NULL ); + pthread_join( r_to_l_thread[j], NULL ); } free( timetocross ); // Destroy semaphores @@ -170,6 +188,7 @@ int main( int argc, char **argv ) { sem_destroy( &right_to_left_mutex ); sem_destroy( &left_to_right_mutex ); sem_destroy( &mutex ); - sem_destroy( &counter ); + sem_destroy( &capacity ); return 0; } +