LukasHuhn

SI - Blatt11 A3

Oct 10th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <traffic.h>
  4. #include <pthread.h>
  5.  
  6. #define map_width 10
  7. #define map_height 10
  8.  
  9. /* Nun die Kartendaten */
  10. static map_shorts map_def[]={
  11. R2,RH,RH,RH,RH,RH,RH,RH,RH,R3,
  12. RV,__,__,__,__,__,__,__,__,RV,
  13. RV,__,__,__,__,__,__,__,__,RV,
  14. RV,__,__,__,__,__,__,__,__,RV,
  15. RV,__,__,__,__,__,__,__,__,RV,
  16. RV,__,__,__,__,__,__,__,__,RV,
  17. RV,__,__,__,__,__,__,__,__,RV,
  18. RV,__,__,__,__,__,__,__,__,RV,
  19. RV,__,__,__,__,__,__,__,__,RV,
  20. R1,RH,RH,RH,RH,RH,RH,RH,RH,R4
  21. };
  22. pthread_cond_t bedingungsvariable;
  23. pthread_mutex_t mutex, mutex2;
  24.  
  25. int makeAStop[5] = {-1, -1, -1, -1, -1};
  26.  
  27. void drive(int arg[4]){
  28.     directions dir[4] = {N, W, S, O};
  29.     int x = arg[0], y = arg[1];
  30.     int id = putTrain(x, y, dir[arg[2]], 0);
  31.     int i = x<y?x+y:36-(x+y);
  32.  
  33.     int myIndex = arg[3];
  34.     pthread_mutex_lock(&mutex2);
  35.     makeAStop[myIndex] = (i-1)%36;
  36.     pthread_mutex_unlock(&mutex2);
  37.  
  38.     if(i != 4)
  39.         pthread_cond_wait(&bedingungsvariable, &mutex);
  40.     while(1){
  41.         for(; i<36; i++){
  42.             int j;
  43.             for(j=0;j<5;j++)
  44.                 if(i == makeAStop[j]){
  45.                     pthread_cond_signal(&bedingungsvariable);
  46.                     makeAStop[myIndex] = (i-1)%36;
  47.                     pthread_cond_wait(&bedingungsvariable, &mutex);
  48.                 }
  49.  
  50.  
  51.             if(i < 8)
  52.                 moveTrain(id, S);
  53.             else if(i == 8)
  54.                 moveTrain(id, SSO);
  55.             else if(i < 17)
  56.                 moveTrain(id, O);
  57.             else if(i == 17)
  58.                 moveTrain(id, OON);
  59.             else if(i < 26)
  60.                 moveTrain(id, N);
  61.             else if(i == 26)
  62.                 moveTrain(id, NNW);
  63.             else if(i < 35)
  64.                 moveTrain(id, W);
  65.             else
  66.                 moveTrain(id, WWS);
  67.         }
  68.         i = 0;
  69.     }
  70. }
  71.  
  72. int mainloop(void* args){
  73.     pthread_cond_init(&bedingungsvariable, NULL);
  74.     pthread_mutex_init(&mutex, NULL);
  75.     pthread_mutex_init(&mutex2, NULL);
  76.     int a[4] = {4, 0, 1, 0};
  77.     int b[4] = {9, 4, 0, 1};
  78.     int c[4] = {0, 4, 2, 2};
  79.     int d[4] = {8, 9, 3, 3};
  80.     int e[4] = {0, 3, 2, 4};
  81.     pthread_t z1, z2, z3, z4, z5;
  82.     pthread_create(&z5, NULL, &drive, &e);
  83.     pthread_create(&z1, NULL, &drive, &a);
  84.     pthread_create(&z2, NULL, &drive, &b);
  85.     pthread_create(&z3, NULL, &drive, &c);
  86.     pthread_create(&z4, NULL, &drive, &d);
  87.  
  88.  
  89.     waitAndClose();
  90.     return 0;
  91. }
  92.  
  93. int main(){
  94.     createMap(map_def, map_width, map_height, mainloop);
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment