LukasHuhn

SI - Blatt9 A2

Oct 10th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <traffic.h>
  4. #include <semaphore.h>
  5. #include <pthread.h>
  6.  
  7. #define map_width 12
  8. #define map_height 6
  9.  
  10. /* Nun die Kartendaten */
  11. static map_shorts map_def[]=
  12. {
  13. S2,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,S3,
  14. SV,__,__,__,__,__,__,__,__,__,__,SV,
  15. SV,__,__,__,__,__,__,__,__,__,__,SV,
  16. SV,__,__,__,__,__,__,__,__,__,__,SV,
  17. SV,__,__,__,__,__,__,__,__,__,__,SV,
  18. S1,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,S4
  19. };
  20.  
  21.  
  22. void carOutside(int arg[]);
  23. void carInside(int arg[]);
  24.  
  25. int outside[32];
  26. int inside[32];
  27.  
  28. sem_t outerSem[12], innerSem[12];
  29. pthread_t outerCars[12], innerCars[12];
  30.  
  31. int mainloop(void* arg){
  32. int outer[5] = {0, 0, 1, 0, -1};
  33. int inner[5] = {1, 0, 3, 0, -1};
  34. int i;
  35. for(i=0; i<12; i++){
  36. outer[4] = inner[4] =i;
  37. outer[3] = inner[3] = 40*((i+1)%2);
  38. pthread_create(&outerCars[i], NULL, &carOutside, &outer);
  39. pthread_create(&innerCars[i], NULL, &carInside, &inner);
  40. sleep(3);
  41. }
  42. waitAndClose();
  43. return 0;
  44. }
  45.  
  46. void carOutside(int arg[]){
  47. int myIndex = arg[4];
  48. int frontIndex = (myIndex-1)%12;
  49.  
  50. directions opt[] = {N, S, W, O};
  51. directions last = opt[arg[2]];
  52. int id = putCar(arg[0], arg[1], last, arg[3]);
  53. sem_init(&outerSem[myIndex], 0, 0);
  54.  
  55. int i, j;
  56. for(i=0; i<32*2; i++){
  57. j = i%32;
  58.  
  59. if(myIndex > 0)
  60. sem_wait(&outerSem[frontIndex]);
  61.  
  62. if(j < 4)
  63. moveCar(id, S);
  64. else if(j == 4)
  65. moveCar(id, SSO);
  66. else if(j<15)
  67. moveCar(id, O);
  68. else if(j==15)
  69. moveCar(id, OON);
  70. else if(j<20)
  71. moveCar(id, N);
  72. else if(j == 20)
  73. moveCar(id, NNW);
  74. else if(j < 31)
  75. moveCar(id, W);
  76. else
  77. moveCar(id, WWS);
  78.  
  79. if(i > 0)
  80. sem_post(&outerSem[myIndex]);
  81. }
  82. }
  83. void carInside(int arg[]){
  84. int myIndex = arg[4];
  85. int frontIndex = (myIndex-1)%12;
  86. sem_init(&innerSem[myIndex], 0, 0);
  87.  
  88. directions opt[] = {N, S, W, O};
  89. directions last = opt[arg[2]];
  90. int id = putCar(arg[0], arg[1], last, arg[3]);
  91. int i, j;
  92. for(i=1; i<32*2; i++){
  93. if(myIndex > 0)
  94. sem_wait(&innerSem[frontIndex]);
  95.  
  96. j = i%32;
  97. if(j < 10)
  98. moveCar(id, O);
  99. else if(j == 10)
  100. moveCar(id, SO);
  101. else if(j<15)
  102. moveCar(id, S);
  103. else if(j==15)
  104. moveCar(id, SW);
  105. else if(j<26)
  106. moveCar(id, W);
  107. else if(j == 26)
  108. moveCar(id, NW);
  109. else if(j < 31)
  110. moveCar(id, N);
  111. else
  112. moveCar(id, NO);
  113.  
  114. if(i>1)
  115. sem_post(&innerSem[myIndex]);
  116. }
  117. }
  118.  
  119.  
  120. int main(){
  121. createMap(map_def, map_width, map_height, mainloop);
  122. return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment