Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <unistd.h>
  6.  
  7. #define NUM_THREADS 500
  8.  
  9. pthread_t pts[NUM_THREADS+1];
  10. int freiePlaetze = 100;
  11.  
  12. void * status(){
  13.     int z = 0;
  14.     while (z == 0){
  15.         sleep(1);
  16.         printf("Freie Plaetze: %d", freiePlaetze);
  17.     }
  18. }
  19.  
  20. void * parke(){
  21.     freiePlaetze--;
  22.     srand(time(NULL));
  23.     int parkdauer = rand() % 4 + 1;
  24.     sleep(parkdauer);
  25.     freiePlaetze++;
  26.     pthread_exit(NULL);
  27. }
  28.  
  29. int main() {
  30.  
  31.     // Statusthread erzeugen
  32.     pthread_create(&pts[500], NULL, status, NULL);
  33.  
  34.     // Autos erzeugen
  35.     int i;
  36.     for (i=0; i < NUM_THREADS; i++) {
  37.         if (freiePlaetze > 0){
  38.             pthread_create(&pts[i], NULL, parke, NULL);
  39.         }
  40.     }
  41.  
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement