jaeSun 0 Light Poster

ok, im trying to figure out this simultaneous process assignment ...

i got it finished, runs fine for the most part, but 2 of the processes dont process....

the code:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

int numof_bridgecars = 0;
int northID, southID;

//semaphore declarations
sem_t north_s, south_s, bridge_s;

//pthread declarations (23 total)
void *fnorth1 (void *arg);
void *fnorth2 (void *arg);
void *fnorth3 (void *arg);
void *fnorth4 (void *arg);
void *fnorth5 (void *arg);
void *fnorth6 (void *arg);
void *fnorth7 (void *arg);
void *fnorth8 (void *arg);
void *fnorth9 (void *arg);
void *fnorth10 (void *arg);
void *fsouth1 (void *arg);
void *fsouth2 (void *arg);
void *fsouth3 (void *arg);
void *fsouth4 (void *arg);
void *fsouth5 (void *arg);
void *fsouth6 (void *arg);
void *fsouth7 (void *arg);
void *fsouth8 (void *arg);
void *fsouth9 (void *arg);
void *fsouth10 (void *arg);
void *fsouth11 (void *arg);
void *fsouth12 (void *arg);
void *fships (void *arg);

int main(int argc, char** argv)
{

  //pthread declarations
  pthread_t tnorth1, tnorth2, tnorth3, tnorth4, tnorth5, tnorth6, tnorth7, tnorth8, tnorth9, tnorth10;
  pthread_t tsouth1, tsouth2, tsouth3, tsouth4, tsouth5, tsouth6, tsouth7, tsouth8, tsouth9, tsouth10, tsouth11, tsouth12;
  pthread_t tships;
  void *thread_result;

  int tot_ships;
  int r1, r2, r3, r4, r5, r6, r7, r8, r9, r10,
    r11, r12, r13, r14, r15, r16, r17, r18, r19,
    r20, r21, r22;
  int ifSemErr;

  //initialize semaphores and check for errors
  ifSemErr = sem_init(&north_s, 0, 1);
  if (ifSemErr)
    {
      printf("sem_init() has failed\n");
    }

  ifSemErr = sem_init(&south_s, 0, 1);
  if (ifSemErr)
    {
      printf("sem_init() has failed\n");
    }

  ifSemErr = sem_init(&bridge_s, 0, 1);
  if (ifSemErr)
    {
      printf("sem_init() has failed\n");
    }

  northID = 0;
  southID = 0;

  tot_ships = atoi(argv[1]);  //gets total ships

  printf("Total Ships: %i\n\n", tot_ships);

  //create random numbers
  r1=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r2=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r3=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r4=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r5=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r6=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r7=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r8=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r9=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r10=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r11=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r12=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r13=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r14=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r15=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r16=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r17=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r18=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r19=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r20=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r21=1+(int) (10.0*rand()/(RAND_MAX+1.0));
  r22=1+(int) (10.0*rand()/(RAND_MAX+1.0));

  sem_wait(&south_s);

  //create all threads
  pthread_create (&tnorth1, NULL, (void*)fnorth1, (void*)r1);
  pthread_create (&tsouth1, NULL, (void*)fsouth1, (void*)r2);
  pthread_create (&tships, NULL, (void*)fships, (void*)tot_ships);
  pthread_create (&tnorth2, NULL, (void*)fnorth2, (void*)r3);
  pthread_create (&tnorth3, NULL, (void*)fnorth3, (void*)r4);
  pthread_create (&tsouth2, NULL, (void*)fsouth2, (void*)r5);
  pthread_create (&tnorth4, NULL, (void*)fnorth4, (void*)r6);
  pthread_create (&tsouth3, NULL, (void*)fsouth3, (void*)r7);
  pthread_create (&tnorth5, NULL, (void*)fnorth5, (void*)r8);
  pthread_create (&tnorth6, NULL, (void*)fnorth6, (void*)r9);
  pthread_create (&tsouth4, NULL, (void*)fsouth4, (void*)r10);
  pthread_create (&tnorth7, NULL, (void*)fnorth7, (void*)r11);
  pthread_create (&tsouth5, NULL, (void*)fsouth5, (void*)r12);
  pthread_create (&tnorth8, NULL, (void*)fnorth8, (void*)r13);
  pthread_create (&tnorth9, NULL, (void*)fnorth9, (void*)r14);
  pthread_create (&tsouth6, NULL, (void*)fsouth6, (void*)r15);
  pthread_create (&tnorth10, NULL, (void*)fnorth10, (void*)r16);
  pthread_create (&tsouth7, NULL, (void*)fsouth7, (void*)r17);
  pthread_create (&tsouth8, NULL, (void*)fsouth8, (void*)r18);
  pthread_create (&tsouth9, NULL, (void*)fsouth9, (void*)r19);
  pthread_create (&tsouth10, NULL, (void*)fsouth10, (void*)r20);
  pthread_create (&tsouth11, NULL, (void*)fsouth11, (void*)r21);
  pthread_create (&tsouth12, NULL, (void*)fsouth12, (void*)r22);

  //join all threads
  pthread_join (tships, &thread_result);
  pthread_join (tnorth1, &thread_result);
  pthread_join (tnorth2, &thread_result);
  pthread_join (tnorth3, &thread_result);
  pthread_join (tnorth4, &thread_result);
  pthread_join (tnorth5, &thread_result);
  pthread_join (tnorth6, &thread_result);
  pthread_join (tnorth7, &thread_result);
  pthread_join (tnorth8, &thread_result);
  pthread_join (tnorth9, &thread_result);
  pthread_join (tnorth10, &thread_result);
  pthread_join (tsouth1, &thread_result);
  pthread_join (tsouth2, &thread_result);
  pthread_join (tsouth3, &thread_result);
  pthread_join (tsouth4, &thread_result);
  pthread_join (tsouth5, &thread_result);
  pthread_join (tsouth6, &thread_result);
  pthread_join (tsouth7, &thread_result);
  pthread_join (tsouth8, &thread_result);
  pthread_join (tsouth9, &thread_result);
  pthread_join (tsouth10, &thread_result);
  pthread_join (tsouth11, &thread_result);
  pthread_join (tsouth12, &thread_result);

  //delete the semaphores
  sem_destroy(&north_s);
  sem_destroy(&south_s);
  sem_destroy(&bridge_s);

  return 0;
}

void *fships(void *arg)
{

  //var declarations
  int numof_ships, i;

  numof_ships = (int)arg;

  //lock bridge
  sem_wait(&bridge_s);

  //*************************
  //begin CS

  printf("Drawbridge is up.\n");
  for (i=1; i<=numof_ships; i++)
    {
      printf("Ship #%i is currently crossing under the drawbridge.\n", i);
    }
  printf("Drawbridge is down.\n");

  //end CS
  //*************************

  //unlock bridge
  sem_post(&bridge_s);

  pthread_exit ((void*)NULL);
}


void *fnorth1(void *arg)
{

  //var declarations
  int factor, pausetime;

  factor = (int)arg;
  pausetime = factor * 50000;

  //lock north
  sem_wait(&north_s);
  //lock bridge
  sem_wait(&bridge_s);

  //*************************
  //begin CS

  northID++;
  numof_bridgecars++;
  printf("Northbound car #%i is currently crossing the drawbridge.\n", northID);
  usleep(pausetime);

  //end CS
  //*************************

  //unlock bridge
  sem_post(&bridge_s);
  //check who's turn it is
  if (numof_bridgecars == 5)
    {
      numof_bridgecars = 0;
      sem_post(&south_s);
    }
  else
    {
      sem_post(&north_s);
    }

  pthread_exit ((void*)NULL);
}

void *fsouth1(void *arg)
{

  //var declarations
  int factor, pausetime;

  factor = (int)arg;
  pausetime = factor * 50000;

  //lock south
  sem_wait(&south_s);
  //lock bridge
  sem_wait(&bridge_s);

  //*************************
  //begin CS

  southID++;
  numof_bridgecars++;
  printf("Southbound car #%i is currently crossing the drawbridge.\n", southID);
  usleep(pausetime);

  //end CS
  //*************************

  //unlock bridge
  sem_post(&bridge_s);
  //check who's turn it is
  if (numof_bridgecars == 5 && northID < 10)
    {
      numof_bridgecars = 0;
      sem_post(&north_s);
    }
  else if (numof_bridgecars == 5 && northID >= 10)
    {
      numof_bridgecars = 0;
      sem_post(&south_s);
      sem_post(&south_s);
    }
  else
    {
      sem_post(&south_s);
    }

  pthread_exit ((void*)NULL);
}

the north[x] and south[x] are duplicated appropriately (10 total north functions, 12 total south functions)

output should look like this:

-bash-2.05b$ ./a.out 9
Northbound car 1 is currently crossing drawbridge
Drawbridge is up
Ship 1 is crossing under the drawbridge
Ship 2 is crossing under the drawbridge
Ship 3 is crossing under the drawbridge
Ship 4 is crossing under the drawbridge
Ship 5 is crossing under the drawbridge
Ship 6 is crossing under the drawbridge
Ship 7 is crossing under the drawbridge
Ship 8 is crossing under the drawbridge
Ship 9 is crossing under the drawbridge
Drawbridge is down
Northbound car 2 is currently crossing drawbridge
Northbound car 3 is currently crossing drawbridge
Northbound car 4 is currently crossing drawbridge
Northbound car 5 is currently crossing drawbridge
Southbound car 1 is currently crossing drawbridge
Southbound car 2 is currently crossing drawbridge
Southbound car 3 is currently crossing drawbridge
Southbound car 4 is currently crossing drawbridge
Southbound car 5 is currently crossing drawbridge
Northbound car 6 is currently crossing drawbridge
Northbound car 7 is currently crossing drawbridge
Northbound car 8 is currently crossing drawbridge
Northbound car 9 is currently crossing drawbridge
Northbound car 10 is currently crossing drawbridge
Southbound car 6 is currently crossing drawbridge
Southbound car 7 is currently crossing drawbridge
Southbound car 8 is currently crossing drawbridge
Southbound car 9 is currently crossing drawbridge
Southbound car 10 is currently crossing drawbridge
Southbound car 11 is currently crossing drawbridge
Southbound car 12 is currently crossing drawbridge
-bash-2.05b$

mine looks like this:

-bash-2.05b$ ./a.out 9
Northbound car 1 is currently crossing drawbridge
Drawbridge is up
Ship 1 is crossing under the drawbridge
Ship 2 is crossing under the drawbridge
Ship 3 is crossing under the drawbridge
Ship 4 is crossing under the drawbridge
Ship 5 is crossing under the drawbridge
Ship 6 is crossing under the drawbridge
Ship 7 is crossing under the drawbridge
Ship 8 is crossing under the drawbridge
Ship 9 is crossing under the drawbridge
Drawbridge is down
Northbound car 2 is currently crossing drawbridge
Northbound car 3 is currently crossing drawbridge
Northbound car 4 is currently crossing drawbridge
Northbound car 5 is currently crossing drawbridge
Southbound car 1 is currently crossing drawbridge
Southbound car 2 is currently crossing drawbridge
Southbound car 3 is currently crossing drawbridge
Southbound car 4 is currently crossing drawbridge
Southbound car 5 is currently crossing drawbridge
Northbound car 6 is currently crossing drawbridge
Northbound car 7 is currently crossing drawbridge
Northbound car 8 is currently crossing drawbridge
Northbound car 9 is currently crossing drawbridge
Northbound car 10 is currently crossing drawbridge
Southbound car 6 is currently crossing drawbridge
Southbound car 7 is currently crossing drawbridge
Southbound car 8 is currently crossing drawbridge
Southbound car 9 is currently crossing drawbridge
Southbound car 10 is currently crossing drawbridge
-bash-2.05b$

the last 2 south cars (threads) arent being processed ... i dont know why ..... at the end of each south thread, i have it checking if all of north has gone through (if northID=10), and if so, to run more south threads.....

i can put print statements in those locations, and it prints...it just doesnt seem to find anything in the south semaphore wait queue ...

anyone?