Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <semaphore.h>
  6. #include <time.h>
  7. #include <sys/types.h>
  8. #include <sys/syscall.h>
  9. #include <sys/ipc.h>
  10.  
  11.  
  12. //n kufli
  13. //kufli < klientow
  14. //kazdy 2l
  15. //losowy czas picia
  16. //po 2 piwach out
  17. //do ost klienta
  18.  
  19. int n;
  20. int clients;
  21. int t;
  22. sem_t s;
  23.  
  24. void drink()
  25. {
  26. for(int i=0; i<2; i++)
  27. {
  28. sem_wait(&s);
  29. printf("Klient nr %d zamawia piwo w kuflu nr %d\n", syscall(SYS_gettid), i);
  30. int x = rand()%t;
  31. sleep(x);
  32. printf("Klient nr %d oddaje kufel nr %d po czasie %d\n", syscall(SYS_gettid), i, x);
  33. sem_post(&s);
  34. }
  35. }
  36.  
  37. int main(int argc, char** argv)
  38. {
  39. int result;
  40. pthread_t threads[clients];
  41.  
  42. if (argc !=4)
  43. {
  44. exit(EXIT_FAILURE);
  45. }
  46. srand(time(NULL));
  47.  
  48. clients = atoi(argv[1]);
  49. n = atoi(argv[2]);
  50. t = atoi(argv[3]);
  51.  
  52. sem_init(&s, 0, n);
  53.  
  54. for(int i=0; i<clients; i++)
  55. {
  56. pthread_create(&threads[i], NULL, drink, NULL);
  57. }
  58.  
  59. for(int j=0; j<clients; j++)
  60. {
  61. pthread_join(threads[j], &result);
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement