LukasHuhn

SI - Blatt10 A2

Oct 10th, 2016
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 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 23
  7. #define map_height 6
  8.  
  9. static map_shorts map_def[] =
  10. {
  11. __,__,__,__,__,__,__,__,__,__,__,__,__,__,AU,__,__,__,__,__,__,__,__,
  12. __,SG,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SE,__,
  13. __,SG,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SE,__,
  14. __,SG,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SE,__,
  15. __,SG,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,SE,__,
  16. __,__,__,__,__,__,__,__,__,__,__,__,__,__,AU,__,__,__,__,__,__,__,__
  17. };
  18.  
  19. pthread_t cars[5];
  20. void driving(int arg[]);
  21. void singalLights();
  22.  
  23. pthread_barrier_t barriere;
  24.  
  25. int mainloop(void* args){
  26.     int a[3] = {21, 1, 5};
  27.     int b[3] = {20, 2, 6};
  28.     int c[3] = {19, 3, 7};
  29.     int d[3] = {18, 4, 3};
  30.  
  31.     pthread_barrier_init(&barriere, NULL, 5);
  32.  
  33.     pthread_create(&cars[0], NULL, &driving, &a);
  34.     pthread_create(&cars[1], NULL, &driving, &b);
  35.     pthread_create(&cars[2], NULL, &driving, &c);
  36.     pthread_create(&cars[3], NULL, &driving, &d);
  37.     pthread_create(&cars[4], NULL, &singalLights, NULL);
  38.  
  39.  
  40.     waitAndClose();
  41.     return 0;
  42. }
  43.  
  44. void driving(int arg[]){
  45.     int id = putCar(arg[0], arg[1], W, arg[2]);
  46.     int i;
  47.     for(i=arg[0]; i>1; i--){
  48.         if(i == 14)
  49.             pthread_barrier_wait(&barriere);
  50.         moveCar(id, W);
  51.     }
  52. }
  53.  
  54. void singalLights(){
  55.     int id1 = putSignal(13, 1, O);
  56.     int id2 = putSignal(13, 2, O);
  57.     int id3 = putSignal(13, 3, O);
  58.     int id4 = putSignal(13, 4, O);
  59.     pthread_barrier_wait(&barriere);
  60.     switchOn(id4);
  61.     switchOn(id1);
  62.     switchOn(id2);
  63.     switchOn(id3);
  64. }
  65.  
  66. int main(){
  67.     createMap(map_def, map_width, map_height, mainloop);
  68.     printf("Hi");
  69.     return 0;
  70. }
Add Comment
Please, Sign In to add comment