Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. /*Antonis Mpelantis 3583*/
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <pthread.h>
  6. #include <semaphore.h>
  7. #include <time.h>
  8. #include <unistd.h>
  9.  
  10. pthread_mutex_t mutex0, mutex1, mutex2;
  11. pthread_t chef, cooks[3];
  12. int materials[3] = {0, 0, 0}; //materials[0] = meat, materials[1] = potatoes, materials[2] = pita
  13.  
  14. //Function used from chef to select the materials
  15. void select_materials(){
  16. int random[2];
  17. int i = (rand() % (2 - 0 + 1)) + 0, z;
  18. materials[i]++;
  19. while(1){
  20. z = (rand() % (2 - 0 + 1)) + 0;
  21. if(z != i){
  22. materials[z]++;
  23. break;
  24. }
  25. }
  26. }
  27.  
  28. void createANDconsume(){
  29. printf("Finishing and consuming the meal.\n");
  30. materials[0] == 0;
  31. materials[1] == 0;
  32. materials[2] == 0;
  33. sleep(2);
  34. }
  35.  
  36. void *cook_function(void *t){
  37. pthread_mutex_lock(&mutex1);
  38. int i;
  39. for(i = 0; i < 3; i++){
  40. printf("\n\n materiala %d: %d", i , materials[i]);
  41. }
  42.  
  43. if(materials[0] == 0){
  44. printf("Chef added potatoes and pita.\n");
  45. materials[0]++;
  46. printf("Cook 0 added meat.\n");
  47. createANDconsume();
  48. printf("The meal is over.\n\n");
  49. }
  50. else if(materials[1] == 0){
  51. printf("Chef added meat and pita.\n");
  52. materials[1]++;
  53. printf("Cook 1 added potatoes.\n");
  54. createANDconsume();
  55. printf("The meal is over.\n\n");
  56. }
  57. else if(materials[2] == 0){
  58. printf("Chef added meat and potatoes.\n");
  59. materials[2]++;
  60. printf("Cook 2 added pita.\n");
  61. createANDconsume();
  62. printf("The meal is over.\n\n");
  63. }
  64.  
  65. printf("\n\n materialb %d: %d", 0 , materials[0]);
  66. printf("\n\n materialb %d: %d", 1 , materials[1]);
  67. printf("\n\n materialb %d: %d", 2 , materials[2]);
  68. pthread_mutex_unlock(&mutex1);
  69. }
  70.  
  71.  
  72. void *chef_function(void *t){
  73. while(1){
  74. pthread_mutex_lock(&mutex0);
  75. int i;
  76. select_materials();
  77. if(materials[0] == 0){
  78. pthread_join(cooks[0], NULL);
  79. }
  80. else if(materials[1] == 0){
  81. pthread_join(cooks[1], NULL);
  82. }
  83. else if(materials[2] == 0){
  84. pthread_join(cooks[2], NULL);
  85. }
  86. pthread_mutex_unlock(&mutex0);
  87. }
  88. }
  89.  
  90. int main(){
  91. pthread_mutex_init(&mutex1, NULL);
  92. int i, c_thread;
  93. for(i = 0; i < 3; i++){
  94. c_thread = pthread_create(&(cooks[i]), NULL, &cook_function, NULL);
  95. }
  96. // pthread_mutex_destroy(&mutex1);
  97. pthread_mutex_init(&mutex0, NULL);
  98. c_thread = pthread_create(&chef, NULL, &chef_function, NULL);
  99. int j_thread;
  100. j_thread = pthread_join(chef, NULL);
  101. // pthread_mutex_destroy(&mutex0);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement