title: Elevator Simulation in C using threads
body: <p>Hello I am a newbie in C and want to Simulate an elevator and Use threads as Passengers.
  So far I am able to run the elevator normally but it is the problem with the passengers.
  The aim is to make all the passengers (Threads) select a random destination floor(using random Number Generator) and the elevator stops at the destination and the respective passengers that have selected that floor will walk out of the elevator
  Here is the code. Please Help!</p>
  
  <pre><code> #include&lt;stdio.h&gt;
      #include&lt;stdlib.h&gt;
      #include&lt;time.h&gt;
      #include&lt;pthread.h&gt;
      #include&lt;unistd.h&gt;
  
      #define NUM_THREAD 2
      #define flr 7
  
      int current_floor;
      int dest_floor;
      int t,i;
      int df[flr];
      void *user();
      void elevator();
      void Thread_creats();
      pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER; /*Mutex Initializer*/
  
      int main()
      {
          /*int el;   
          pthread_t elev1;
          //printf("Creating Elevator\n");
          el=pthread_create(&amp;elev1,NULL, elevator, NULL);
          if(el)
          {
              printf("ERROR Creating Thread\n");
              exit(-1);
          }*/
          Thread_creats(); /*Calls the function that creates all the threads*/
  
          return 0;
      }
      /*Elevator Operation*/
  
      void elevator()
      {
          if(df[i]&gt;current_floor)
          {
              printf("Direction ^\n");
              printf("   Up     |\n");
              while(current_floor!=df[i])
              {
                  sleep(2);
                  current_floor++;
                  printf(" floor --&gt; %d\n",current_floor);
              }
                  printf("\n\n Door Open &lt;--[ || ]--&gt;\n\n");
                  sleep(1);
                  printf(" Passenger %d  Walks out....\n",t);
                  sleep(1);
                  printf("\n\n Door Close --&gt; [|] &lt;--\n\n");
          }
          else if(df[i]&lt;current_floor)
          {
              printf(" Direction |\n");
              printf("   Down    V\n");
  
              while (df[i]!=current_floor)
              {
                  sleep(1);
                  current_floor--;
                  printf(" floor --&gt; %d\n", current_floor);
              }
              printf("\n\n Door Open &lt;--[ || ]--&gt;\n\n");
              sleep(1);
              printf(" Passenger %d Walks out....\n",t);
              sleep(1);
              printf("\n\n Door Close --&gt; [|] &lt;--\n\n");
          }
          else
          {
              sleep(1);
              printf(" You are at your destination floor...Please step out of the elevator\n\n");
              sleep(1);
              printf("\n\n Door Open &lt;--[ || ]--&gt;\n\n");
              sleep(1);
              printf(" Passenger %d Walks out....\n",t);
              sleep(1);
              printf("\n\n Door Close --&gt; [|] &lt;--\n\n");
          }   
      }
  
  
      void Thread_creats()
      {
          pthread_t thread[NUM_THREAD];
          int th;
              sleep(1);
              //printf("Creating Passenger %d \n",t);
              for(t=0;t&lt;NUM_THREAD;t++)
              {
                  th=pthread_create(&amp;thread[NUM_THREAD],NULL,user,NULL);
                  if(th)
                  {
                      printf("ERROR Creating Thread\n");
                      exit(-1);
                  }
              }
      }
  
      void *user()
      {
  
          for (i=0;i&lt;flr;i++)
          {   
              pthread_mutex_lock(&amp;mymutex);
              /*Initialize Seed Time*/
              srand(time(NULL));
              dest_floor=rand()%flr; /*Generates a random Floor number*/
              sleep(1);
              df[i]=dest_floor;
              printf("Passenger %d Selects floor: %d\n",t,df[i]);
              pthread_mutex_unlock(&amp;mymutex);
          }
  
              pthread_exit(NULL);
              return NULL;
      }
  </code></pre>