2013-03-13 31 views
-1

您好,我是C的新手,想模擬電梯並將線程作爲乘客使用。 到目前爲止,我能夠正常運行電梯,但這是乘客的問題。 目標是讓所有的乘客(線程)選擇一個隨機的目的地樓層(使用隨機數字發生器),電梯停在目的地,並選擇該樓層的相應乘客將走出電梯 這裏是代碼。請幫忙!使用線程C進行電梯仿真

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

    #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(&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]>current_floor) 
     { 
      printf("Direction ^\n"); 
      printf(" Up  |\n"); 
      while(current_floor!=df[i]) 
      { 
       sleep(2); 
       current_floor++; 
       printf(" floor --> %d\n",current_floor); 
      } 
       printf("\n\n Door Open <--[ || ]-->\n\n"); 
       sleep(1); 
       printf(" Passenger %d Walks out....\n",t); 
       sleep(1); 
       printf("\n\n Door Close --> [|] <--\n\n"); 
     } 
     else if(df[i]<current_floor) 
     { 
      printf(" Direction |\n"); 
      printf(" Down V\n"); 

      while (df[i]!=current_floor) 
      { 
       sleep(1); 
       current_floor--; 
       printf(" floor --> %d\n", current_floor); 
      } 
      printf("\n\n Door Open <--[ || ]-->\n\n"); 
      sleep(1); 
      printf(" Passenger %d Walks out....\n",t); 
      sleep(1); 
      printf("\n\n Door Close --> [|] <--\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 <--[ || ]-->\n\n"); 
      sleep(1); 
      printf(" Passenger %d Walks out....\n",t); 
      sleep(1); 
      printf("\n\n Door Close --> [|] <--\n\n"); 
     } 
    } 


    void Thread_creats() 
    { 
     pthread_t thread[NUM_THREAD]; 
     int th; 
      sleep(1); 
      //printf("Creating Passenger %d \n",t); 
      for(t=0;t<NUM_THREAD;t++) 
      { 
       th=pthread_create(&thread[NUM_THREAD],NULL,user,NULL); 
       if(th) 
       { 
        printf("ERROR Creating Thread\n"); 
        exit(-1); 
       } 
      } 
    } 

    void *user() 
    { 

     for (i=0;i<flr;i++) 
     { 
      pthread_mutex_lock(&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(&mymutex); 
     } 

      pthread_exit(NULL); 
      return NULL; 
    } 
+1

這聽起來像家庭作業。你的問題是什麼? – greydet 2013-03-13 17:03:51

+0

我想讓線程運行並選擇隨機目標。是的,這是一項家庭作業。 – 2013-03-13 17:07:26

回答

0

我前段時間寫過電梯模擬器,面臨同樣的問題。

對我來說,最好的辦法是:

1電梯主題:

callElevator(int level); 
calcMovement(); 
openDoors(int level); 

1線程使所有乘客在結構(服務員和乘客):

createNew(int orig, int dest); 
exchange(int level); // called from elevator thread 

但最大的問題不是一般的組織。運動算法將是最困難的事情。