How to make a taxi simulation with using semaphores?












0














We have 10 drivers, 10 cars and 100 students. Each driver has a car and these cars can take up to four students. The cars will leave the taxi stop only if its full. In an empty car, the driver will always sleep. If a student enters the car, student will wake the driver and driver will make a call for empty seats.



In my code, i can't seem to make driver wait for the car to be full. When a student wakes driver, driver leaves immediately without waiting other students.



We want drivers to wait for cars to be full to leave the taxi stop. How can it be done?



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

// The maximum number of customer threads.
#define MAX_STUDENTS 100


void *student(void *num);
void *driver(void *);
void randwait(int secs);




//Define the semaphores.
// waitingDriver Limits the # of drivers allowed to drive a taxi.
sem_t waitingDriver;
// taxiChair ensures mutually exclusive access to the taxi chair.
sem_t taxiChair;
// driverPillow is used to allow the driver to sleep until a student arrives.
sem_t driverPillow;
// seatBelt is used to make the students to wait until the driver is done driving.
sem_t seatBelt;
// Flag to stop the driver thread when all students have been serviced.
int allDone = 0;


int main(int argc, char *argv)
{
pthread_t dtid[10];
pthread_t stid[MAX_STUDENTS];
int i, x, numStudents, numDrivers = 1, numChairs = 4; int Number[MAX_STUDENTS];
printf("Maximum number of customers can only be 100. Enter number of students.n");
scanf("%d",&x);
numStudents = x;
if (numStudents > MAX_STUDENTS) {
printf("The maximum number of Students is %d.n", MAX_STUDENTS);
system("PAUSE");
return 0;
}
printf("A solution to the taxi problem using semaphores.n");
for (i = 0; i < MAX_STUDENTS; i++) {
Number[i] = i;
}
// Initialize the semaphores with initial values...
sem_init(&waitingDriver, 0, 10);
sem_init(&taxiChair, 0, 4);
sem_init(&driverPillow, 0, 0);
sem_init(&seatBelt, 0, 4);

// Create the taxis.
for (i = 0; i < numDrivers; i++) {
pthread_create(&dtid[i], NULL, driver, (void *)&Number[i+1]);
}

// Create the customers.
for (i = 0; i < numStudents; i++) {
pthread_create(&stid[i], NULL, student, (void *)&Number[i+1]);
}
// Join each of the threads to wait for them to finish.
for (i = 0; i < numStudents; i++) {
pthread_join(stid[i],NULL);
}
// When all of the customers are finished, kill the barber thread.
allDone = 1;
sem_post(&driverPillow); // Wake the barber so he will exit.
for (i = 0; i < numDrivers; i++) {
pthread_join(dtid[i],NULL);
}
system("PAUSE");
return 0;
}


void *student(void *number) {
int num = *(int *)number; // Leave for the taxispot and take some random amount of time to arrive.
printf("Student %d leaving for taxi.n", num);
randwait(5);
printf("Student %d arrived at taxi.n", num); // Wait for space to open up in the waiting room...
sem_wait(&waitingDriver);
printf("Student %d entering taxi.n", num); // Wait for the taxi driver to become free.
sem_wait(&taxiChair); // The chair is free so give up your spot in the taxispot.
sem_post(&waitingDriver); // Wake up the driver...
printf("Student %d waking the driver.n", num);
sem_post(&driverPillow); // Wait for the driver to finish circuit.
sem_wait(&seatBelt); // Give up the chair.
sem_post(&taxiChair);
printf("Student %d leaving taxi.n", num);
}

void *driver(void *number)
{
int num = *(int *)number; // Leave for the taxi and take some random amount of time to arrive.
while (!allDone) { // Sleep until someone arrives and wakes you..
printf("The driver %d is sleepingn", num);
sem_wait(&driverPillow); // Skip this stuff at the end...
if (!allDone)
{ // Take a random amount of time to drive the students.
printf("The driver %d is drivingn", num);
randwait(3);
printf("The driver %d has finished circuit.n", num); // Release the students when done driving...
sem_post(&seatBelt);
}
else {
printf("The driver %d is going home for the day.n", num);
}
}
}


void randwait(int secs) {
int len = 1; // Generate an arbit number...
sleep(len);
}









share|improve this question



























    0














    We have 10 drivers, 10 cars and 100 students. Each driver has a car and these cars can take up to four students. The cars will leave the taxi stop only if its full. In an empty car, the driver will always sleep. If a student enters the car, student will wake the driver and driver will make a call for empty seats.



    In my code, i can't seem to make driver wait for the car to be full. When a student wakes driver, driver leaves immediately without waiting other students.



    We want drivers to wait for cars to be full to leave the taxi stop. How can it be done?



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

    // The maximum number of customer threads.
    #define MAX_STUDENTS 100


    void *student(void *num);
    void *driver(void *);
    void randwait(int secs);




    //Define the semaphores.
    // waitingDriver Limits the # of drivers allowed to drive a taxi.
    sem_t waitingDriver;
    // taxiChair ensures mutually exclusive access to the taxi chair.
    sem_t taxiChair;
    // driverPillow is used to allow the driver to sleep until a student arrives.
    sem_t driverPillow;
    // seatBelt is used to make the students to wait until the driver is done driving.
    sem_t seatBelt;
    // Flag to stop the driver thread when all students have been serviced.
    int allDone = 0;


    int main(int argc, char *argv)
    {
    pthread_t dtid[10];
    pthread_t stid[MAX_STUDENTS];
    int i, x, numStudents, numDrivers = 1, numChairs = 4; int Number[MAX_STUDENTS];
    printf("Maximum number of customers can only be 100. Enter number of students.n");
    scanf("%d",&x);
    numStudents = x;
    if (numStudents > MAX_STUDENTS) {
    printf("The maximum number of Students is %d.n", MAX_STUDENTS);
    system("PAUSE");
    return 0;
    }
    printf("A solution to the taxi problem using semaphores.n");
    for (i = 0; i < MAX_STUDENTS; i++) {
    Number[i] = i;
    }
    // Initialize the semaphores with initial values...
    sem_init(&waitingDriver, 0, 10);
    sem_init(&taxiChair, 0, 4);
    sem_init(&driverPillow, 0, 0);
    sem_init(&seatBelt, 0, 4);

    // Create the taxis.
    for (i = 0; i < numDrivers; i++) {
    pthread_create(&dtid[i], NULL, driver, (void *)&Number[i+1]);
    }

    // Create the customers.
    for (i = 0; i < numStudents; i++) {
    pthread_create(&stid[i], NULL, student, (void *)&Number[i+1]);
    }
    // Join each of the threads to wait for them to finish.
    for (i = 0; i < numStudents; i++) {
    pthread_join(stid[i],NULL);
    }
    // When all of the customers are finished, kill the barber thread.
    allDone = 1;
    sem_post(&driverPillow); // Wake the barber so he will exit.
    for (i = 0; i < numDrivers; i++) {
    pthread_join(dtid[i],NULL);
    }
    system("PAUSE");
    return 0;
    }


    void *student(void *number) {
    int num = *(int *)number; // Leave for the taxispot and take some random amount of time to arrive.
    printf("Student %d leaving for taxi.n", num);
    randwait(5);
    printf("Student %d arrived at taxi.n", num); // Wait for space to open up in the waiting room...
    sem_wait(&waitingDriver);
    printf("Student %d entering taxi.n", num); // Wait for the taxi driver to become free.
    sem_wait(&taxiChair); // The chair is free so give up your spot in the taxispot.
    sem_post(&waitingDriver); // Wake up the driver...
    printf("Student %d waking the driver.n", num);
    sem_post(&driverPillow); // Wait for the driver to finish circuit.
    sem_wait(&seatBelt); // Give up the chair.
    sem_post(&taxiChair);
    printf("Student %d leaving taxi.n", num);
    }

    void *driver(void *number)
    {
    int num = *(int *)number; // Leave for the taxi and take some random amount of time to arrive.
    while (!allDone) { // Sleep until someone arrives and wakes you..
    printf("The driver %d is sleepingn", num);
    sem_wait(&driverPillow); // Skip this stuff at the end...
    if (!allDone)
    { // Take a random amount of time to drive the students.
    printf("The driver %d is drivingn", num);
    randwait(3);
    printf("The driver %d has finished circuit.n", num); // Release the students when done driving...
    sem_post(&seatBelt);
    }
    else {
    printf("The driver %d is going home for the day.n", num);
    }
    }
    }


    void randwait(int secs) {
    int len = 1; // Generate an arbit number...
    sleep(len);
    }









    share|improve this question

























      0












      0








      0







      We have 10 drivers, 10 cars and 100 students. Each driver has a car and these cars can take up to four students. The cars will leave the taxi stop only if its full. In an empty car, the driver will always sleep. If a student enters the car, student will wake the driver and driver will make a call for empty seats.



      In my code, i can't seem to make driver wait for the car to be full. When a student wakes driver, driver leaves immediately without waiting other students.



      We want drivers to wait for cars to be full to leave the taxi stop. How can it be done?



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

      // The maximum number of customer threads.
      #define MAX_STUDENTS 100


      void *student(void *num);
      void *driver(void *);
      void randwait(int secs);




      //Define the semaphores.
      // waitingDriver Limits the # of drivers allowed to drive a taxi.
      sem_t waitingDriver;
      // taxiChair ensures mutually exclusive access to the taxi chair.
      sem_t taxiChair;
      // driverPillow is used to allow the driver to sleep until a student arrives.
      sem_t driverPillow;
      // seatBelt is used to make the students to wait until the driver is done driving.
      sem_t seatBelt;
      // Flag to stop the driver thread when all students have been serviced.
      int allDone = 0;


      int main(int argc, char *argv)
      {
      pthread_t dtid[10];
      pthread_t stid[MAX_STUDENTS];
      int i, x, numStudents, numDrivers = 1, numChairs = 4; int Number[MAX_STUDENTS];
      printf("Maximum number of customers can only be 100. Enter number of students.n");
      scanf("%d",&x);
      numStudents = x;
      if (numStudents > MAX_STUDENTS) {
      printf("The maximum number of Students is %d.n", MAX_STUDENTS);
      system("PAUSE");
      return 0;
      }
      printf("A solution to the taxi problem using semaphores.n");
      for (i = 0; i < MAX_STUDENTS; i++) {
      Number[i] = i;
      }
      // Initialize the semaphores with initial values...
      sem_init(&waitingDriver, 0, 10);
      sem_init(&taxiChair, 0, 4);
      sem_init(&driverPillow, 0, 0);
      sem_init(&seatBelt, 0, 4);

      // Create the taxis.
      for (i = 0; i < numDrivers; i++) {
      pthread_create(&dtid[i], NULL, driver, (void *)&Number[i+1]);
      }

      // Create the customers.
      for (i = 0; i < numStudents; i++) {
      pthread_create(&stid[i], NULL, student, (void *)&Number[i+1]);
      }
      // Join each of the threads to wait for them to finish.
      for (i = 0; i < numStudents; i++) {
      pthread_join(stid[i],NULL);
      }
      // When all of the customers are finished, kill the barber thread.
      allDone = 1;
      sem_post(&driverPillow); // Wake the barber so he will exit.
      for (i = 0; i < numDrivers; i++) {
      pthread_join(dtid[i],NULL);
      }
      system("PAUSE");
      return 0;
      }


      void *student(void *number) {
      int num = *(int *)number; // Leave for the taxispot and take some random amount of time to arrive.
      printf("Student %d leaving for taxi.n", num);
      randwait(5);
      printf("Student %d arrived at taxi.n", num); // Wait for space to open up in the waiting room...
      sem_wait(&waitingDriver);
      printf("Student %d entering taxi.n", num); // Wait for the taxi driver to become free.
      sem_wait(&taxiChair); // The chair is free so give up your spot in the taxispot.
      sem_post(&waitingDriver); // Wake up the driver...
      printf("Student %d waking the driver.n", num);
      sem_post(&driverPillow); // Wait for the driver to finish circuit.
      sem_wait(&seatBelt); // Give up the chair.
      sem_post(&taxiChair);
      printf("Student %d leaving taxi.n", num);
      }

      void *driver(void *number)
      {
      int num = *(int *)number; // Leave for the taxi and take some random amount of time to arrive.
      while (!allDone) { // Sleep until someone arrives and wakes you..
      printf("The driver %d is sleepingn", num);
      sem_wait(&driverPillow); // Skip this stuff at the end...
      if (!allDone)
      { // Take a random amount of time to drive the students.
      printf("The driver %d is drivingn", num);
      randwait(3);
      printf("The driver %d has finished circuit.n", num); // Release the students when done driving...
      sem_post(&seatBelt);
      }
      else {
      printf("The driver %d is going home for the day.n", num);
      }
      }
      }


      void randwait(int secs) {
      int len = 1; // Generate an arbit number...
      sleep(len);
      }









      share|improve this question













      We have 10 drivers, 10 cars and 100 students. Each driver has a car and these cars can take up to four students. The cars will leave the taxi stop only if its full. In an empty car, the driver will always sleep. If a student enters the car, student will wake the driver and driver will make a call for empty seats.



      In my code, i can't seem to make driver wait for the car to be full. When a student wakes driver, driver leaves immediately without waiting other students.



      We want drivers to wait for cars to be full to leave the taxi stop. How can it be done?



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

      // The maximum number of customer threads.
      #define MAX_STUDENTS 100


      void *student(void *num);
      void *driver(void *);
      void randwait(int secs);




      //Define the semaphores.
      // waitingDriver Limits the # of drivers allowed to drive a taxi.
      sem_t waitingDriver;
      // taxiChair ensures mutually exclusive access to the taxi chair.
      sem_t taxiChair;
      // driverPillow is used to allow the driver to sleep until a student arrives.
      sem_t driverPillow;
      // seatBelt is used to make the students to wait until the driver is done driving.
      sem_t seatBelt;
      // Flag to stop the driver thread when all students have been serviced.
      int allDone = 0;


      int main(int argc, char *argv)
      {
      pthread_t dtid[10];
      pthread_t stid[MAX_STUDENTS];
      int i, x, numStudents, numDrivers = 1, numChairs = 4; int Number[MAX_STUDENTS];
      printf("Maximum number of customers can only be 100. Enter number of students.n");
      scanf("%d",&x);
      numStudents = x;
      if (numStudents > MAX_STUDENTS) {
      printf("The maximum number of Students is %d.n", MAX_STUDENTS);
      system("PAUSE");
      return 0;
      }
      printf("A solution to the taxi problem using semaphores.n");
      for (i = 0; i < MAX_STUDENTS; i++) {
      Number[i] = i;
      }
      // Initialize the semaphores with initial values...
      sem_init(&waitingDriver, 0, 10);
      sem_init(&taxiChair, 0, 4);
      sem_init(&driverPillow, 0, 0);
      sem_init(&seatBelt, 0, 4);

      // Create the taxis.
      for (i = 0; i < numDrivers; i++) {
      pthread_create(&dtid[i], NULL, driver, (void *)&Number[i+1]);
      }

      // Create the customers.
      for (i = 0; i < numStudents; i++) {
      pthread_create(&stid[i], NULL, student, (void *)&Number[i+1]);
      }
      // Join each of the threads to wait for them to finish.
      for (i = 0; i < numStudents; i++) {
      pthread_join(stid[i],NULL);
      }
      // When all of the customers are finished, kill the barber thread.
      allDone = 1;
      sem_post(&driverPillow); // Wake the barber so he will exit.
      for (i = 0; i < numDrivers; i++) {
      pthread_join(dtid[i],NULL);
      }
      system("PAUSE");
      return 0;
      }


      void *student(void *number) {
      int num = *(int *)number; // Leave for the taxispot and take some random amount of time to arrive.
      printf("Student %d leaving for taxi.n", num);
      randwait(5);
      printf("Student %d arrived at taxi.n", num); // Wait for space to open up in the waiting room...
      sem_wait(&waitingDriver);
      printf("Student %d entering taxi.n", num); // Wait for the taxi driver to become free.
      sem_wait(&taxiChair); // The chair is free so give up your spot in the taxispot.
      sem_post(&waitingDriver); // Wake up the driver...
      printf("Student %d waking the driver.n", num);
      sem_post(&driverPillow); // Wait for the driver to finish circuit.
      sem_wait(&seatBelt); // Give up the chair.
      sem_post(&taxiChair);
      printf("Student %d leaving taxi.n", num);
      }

      void *driver(void *number)
      {
      int num = *(int *)number; // Leave for the taxi and take some random amount of time to arrive.
      while (!allDone) { // Sleep until someone arrives and wakes you..
      printf("The driver %d is sleepingn", num);
      sem_wait(&driverPillow); // Skip this stuff at the end...
      if (!allDone)
      { // Take a random amount of time to drive the students.
      printf("The driver %d is drivingn", num);
      randwait(3);
      printf("The driver %d has finished circuit.n", num); // Release the students when done driving...
      sem_post(&seatBelt);
      }
      else {
      printf("The driver %d is going home for the day.n", num);
      }
      }
      }


      void randwait(int secs) {
      int len = 1; // Generate an arbit number...
      sleep(len);
      }






      c semaphore






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 22:31









      jokeer_an

      12




      12





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438686%2fhow-to-make-a-taxi-simulation-with-using-semaphores%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438686%2fhow-to-make-a-taxi-simulation-with-using-semaphores%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)