Advertisement
Guest User

croadcproject

a guest
Feb 17th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.07 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <unistd.h>
  3. #include <semaphore.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/sem.h>
  7. #include <signal.h>
  8. #include <unistd.h>
  9. #include <sys/ipc.h>
  10. #include <sys/shm.h>
  11. #include <string.h>
  12.  
  13. #define N 10
  14. #define limit 5
  15. #define wait 10
  16. #define lenght 5
  17. //sem_t lightLeft, lightRight;
  18. sem_t lights[2];
  19. //int carsLeft=0, carsRight=0;
  20. sem_t road, change;
  21. int side=0;
  22. char dir[2][6] = {"left", "right"};
  23. struct car_info
  24. {
  25.     int way;
  26.     int id;
  27. };
  28.  
  29. void * timerf()
  30. {
  31.     printf("Timer start\n");
  32.     int waitingtime = wait + rand()%wait;
  33.     while (1)
  34.     {
  35.         sleep(waitingtime);
  36.         side=(side+1)%2;
  37.         sem_post(&change);
  38.         printf("Prosba o zmiane swiatel wyslana\n");
  39.     }
  40. }
  41.  
  42. void * car (void *arg)
  43. {
  44.     struct car_info *info = arg;
  45.     int tid = info->way;
  46.     int id = info->id;
  47.     while (1)
  48.     {
  49.         printf("Auto stoi w kolejce [%d][%s]\n",id, dir[tid]);
  50.         sem_wait(&road);
  51.         sem_post(&road);
  52.         printf("Auto czeka za swiatlami [%d][%s]\n",id, dir[tid]);
  53.         sem_wait(&lights[tid]);
  54.         printf ("Auto wjezdza na droge [%d][%s]\n",id, dir[tid]);
  55.         sleep(lenght + rand()%lenght);
  56.         printf("Auto wyjezdza z drogi [%d][%s]\n",id, dir[tid]);
  57.         sem_post(&lights[tid]);
  58.         tid = (tid+1)%2;
  59.         sleep(lenght + rand()%lenght);
  60.  
  61.     }
  62.  
  63. }
  64.  
  65. void * signalsystem()
  66. {
  67.     int i=0;
  68.     int now=side;
  69.     //int carsRoad=5;
  70.     while (1)
  71.     {
  72.             printf("Swiatla zielone [%s]\n", dir[now]);
  73.             for (i=0;i<limit;i++)
  74.                 sem_post(&lights[now]);
  75.             sem_post(&road);
  76.             //sem_post(&lights[now]);
  77.             sem_wait(&change);
  78.             printf("Prosba o zmiane swiatel otrzymana\n");
  79.             sem_wait(&road);
  80.             printf("Swiatla czerwone [%s]\n", dir[now]);
  81.             for (i=0;i<limit;i++)
  82.                 sem_wait(&lights[now]);
  83.             printf("Droga pusta\n");
  84.             now=side;
  85.     }
  86.  
  87. }
  88. void addCar(pthread_t thread, struct car_info info)
  89. {
  90.     pthread_create(&thread, NULL, &car, &info);
  91. }
  92.  
  93. int main()
  94. {
  95.     pthread_t thread_id[N], timer;
  96.  
  97.  
  98.  
  99.     //init
  100.  
  101.     sem_init(&lights[0], 0, 0);
  102.     sem_init(&lights[1], 0, 0);
  103.     sem_init(&road, 0, 0);
  104.     sem_init(&change, 0, 0);
  105.     int carAmount=1;
  106.     char temp;
  107.     int continious=1;
  108.     //petla glowna programu
  109.  
  110.  
  111.     pthread_create(&thread_id[0], NULL,
  112.                     &signalsystem, NULL);
  113.     pthread_create(&timer, NULL,
  114.                     &timerf, NULL);
  115.     while (continious)
  116.     {
  117.         scanf("%c", &temp);
  118.         switch(temp)
  119.         {
  120.             case 'l':
  121.             ;
  122.                 struct car_info infol = {0, carAmount};
  123.                 addCar(thread_id[carAmount++], infol);
  124.                 break;
  125.             case 'r':
  126.             ;
  127.                 struct car_info infor = {1, carAmount};
  128.                 addCar(thread_id[carAmount++], infor);
  129.                 break;
  130.             case 'e':
  131.                 continious=0;
  132.                 break;
  133.  
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement