Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <pthread.h>
  8. #include <semaphore.h> // The maximum number of customer threads.
  9. #define MAX_CUSTOMERS 25 // Function prototypes...
  10.  
  11. void *customer(void *num);
  12. void *barber(void *);
  13. void randwait(int secs);
  14.  
  15. //Define the semaphores.
  16. // waitingRoom Limits the # of customers allowed to enter the waiting room at one time.
  17. sem_t waitingRoom;
  18. // barberChair ensures mutually exclusive access to the barber chair.
  19. sem_t barberChair;
  20. // barberPillow is used to allow the barber to sleep until a customer arrives.
  21. sem_t barberPillow;
  22. // seatBelt is used to make the customer to wait until the barber is done cutting his/her hair.
  23. sem_t seatBelt;
  24. // Flag to stop the barber thread when all customers have been serviced.
  25. int allDone = 0;
  26.  
  27. int main(int argc, char *argv[])
  28. {
  29. pthread_t btid;
  30. pthread_t tid[MAX_CUSTOMERS];
  31. int i, x, numCustomers, numChairs; int Number[MAX_CUSTOMERS];
  32. printf("[Maximum number of customers can only be 25]\n");
  33. printf("Enter number of chairs and customers : ");
  34. scanf("%d",&x);
  35. numChairs = x;
  36. scanf("%d",&x);
  37. numCustomers = x;
  38. if (numCustomers > MAX_CUSTOMERS) {
  39. printf("The maximum number of Customers is %d.\n", MAX_CUSTOMERS);
  40. system("PAUSE");
  41. return 0;
  42. }
  43. printf("\n");
  44. printf("A solution to the sleeping barber problem using semaphores.\n");
  45. for (i = 0; i < MAX_CUSTOMERS; i++) {
  46. Number[i] = i;
  47. }
  48. // Initialize the semaphores with initial values...
  49. sem_init(&waitingRoom, 0, numChairs);
  50. sem_init(&barberChair, 0, 1);
  51. sem_init(&barberPillow, 0, 0);
  52. sem_init(&seatBelt, 0, 0);
  53.  
  54. // Create the barber.
  55. pthread_create(&btid, NULL, barber, NULL);
  56.  
  57. // Create the customers.
  58. for (i = 0; i < numCustomers; i++) {
  59. pthread_create(&tid[i], NULL, customer, (void *)&Number[i]);
  60. }
  61. // Join each of the threads to wait for them to finish.
  62. for (i = 0; i < numCustomers; i++) {
  63. pthread_join(tid[i],NULL);
  64. }
  65. // When all of the customers are finished, kill the barber thread.
  66. allDone = 1;
  67. sem_post(&barberPillow); // Wake the barber so he will exit.
  68. pthread_join(btid,NULL);
  69. system("PAUSE");
  70. return 0;
  71. }
  72.  
  73. void *customer(void *number) {
  74. int num = *(int *)number; // Leave for the shop and take some random amount of time to arrive.
  75. printf("Customer %d leaving for barber shop.\n", num);
  76. randwait(5);
  77. printf("Customer %d arrived at barber shop.\n", num); // Wait for space to open up in the waiting room...
  78. sem_wait(&waitingRoom);
  79. printf("Customer %d entering waiting room.\n", num); // Wait for the barber chair to become free.
  80. sem_wait(&barberChair); // The chair is free so give up your spot in the waiting room.
  81. sem_post(&waitingRoom); // Wake up the barber...
  82. printf("Customer %d waking the barber.\n", num);
  83. sem_post(&barberPillow); // Wait for the barber to finish cutting your hair.
  84. sem_wait(&seatBelt); // Give up the chair.
  85. sem_post(&barberChair);
  86. printf("Customer %d leaving barber shop.\n", num);
  87. }
  88.  
  89. void *barber(void *junk)
  90. {
  91. // While there are still customers to be serviced... Our barber is omnicient and can tell if there are customers still on the way to his shop.
  92.  
  93. while (!allDone) { // Sleep until someone arrives and wakes you..
  94. printf("The barber is sleeping\n");
  95. sem_wait(&barberPillow); // Skip this stuff at the end...
  96. if (!allDone)
  97. { // Take a random amount of time to cut the customer's hair.
  98. printf("The barber is cutting hair\n");
  99. randwait(3);
  100. printf("The barber has finished cutting hair.\n"); // Release the customer when done cutting...
  101. sem_post(&seatBelt);
  102. }
  103. else {
  104. printf("The barber is going home for the day.\n");
  105. }
  106. }
  107. }
  108.  
  109. void randwait(int secs) {
  110. int len = 1; // Generate an arbit number...
  111. sleep(len);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement