Advertisement
Guest User

Assignment

a guest
Nov 12th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. pthread_mutex_t mutex;
  5. // Variables tray, maximum and minimum
  6. int tray;
  7. int max = 4;
  8. int min = 0;
  9. char choice[10];
  10.  
  11.  
  12.  
  13. void*chiefCook(void*arg){
  14.     int pthread_mutex_lock(pthread_mutex_t*mutex);
  15.     while (1)
  16.     {
  17.         if(tray<max){
  18.             tray=tray+1;
  19.             printf("the chef is adding PIZZA. The trial is: %d\n", tray);
  20.         }
  21.         else if(tray == max){
  22.             printf("Client, the tray is full you can eat\n");
  23.             return NULL;
  24.         }
  25.  
  26.     }
  27.     int pthread_mutex_unlock(pthread_mutex_t*mutex);
  28.    
  29. }
  30.  
  31. void*clientEat(void*arg){
  32.     int pthread_mutex_lock(pthread_mutex_t*mutex);
  33.  
  34.     while (1)
  35.     {
  36.         if(tray>min){
  37.             tray=tray-1;
  38.             printf("The remaining trial is : %d\n", tray);
  39.         }
  40.         else if (tray == min)
  41.         {
  42.             printf("Chief tray is empty make some PIZZA \n");
  43.             return NULL;
  44.         }
  45.  
  46.     }
  47.     int pthread_mutex_unlock(pthread_mutex_t*mutex);
  48.    
  49. }
  50.  
  51.  
  52.  
  53.  
  54. int main(){
  55.  
  56.     pthread_t client, chief;
  57.  
  58.     printf("Do you want to eat or cook PIZZA, Enter \n - cook ...if you want to be the chief cooker\n - eat ...if you are a client\n");
  59.     fgets(choice, 10, stdin);
  60.     // scanf("%s", choice);
  61.  
  62.     char comp[4] = "cook";
  63.     int counter=0;
  64.  
  65.     for (int i = 0; i < 4; i++)
  66.     {
  67.        if(choice[i] != comp[i]){
  68.            counter = counter + 1;
  69.        }
  70.     }
  71.  
  72.     if (counter > 0)
  73.     {
  74.         printf("He chose to eat\n");
  75.     }else
  76.     {
  77.         printf("He want to Cook\n");
  78.     }
  79.  
  80.     printf("How many PIZZA is in the tray: ");
  81.     scanf("%d", &tray);
  82.     printf("The number in tray is %d\n", tray);
  83.  
  84.     if (tray == max && (counter > 0 || counter ==0))
  85.     {
  86.         pthread_create(&client,NULL, clientEat, NULL);
  87.         pthread_create(&chief,NULL, chiefCook, NULL);
  88.     }
  89.     else if(tray == min && (counter > 0 || counter == 0))
  90.       {
  91.         pthread_create(&chief,NULL, chiefCook, NULL);
  92.         pthread_create(&client,NULL, clientEat, NULL);
  93.       }
  94.    
  95.    
  96.    
  97.  
  98.     pthread_join(client,NULL);
  99.     pthread_join(chief, NULL);
  100.    
  101.    
  102.    
  103.  
  104.  
  105.  
  106.    
  107.  
  108.    
  109.  
  110.    
  111.    
  112.  
  113.  
  114.        
  115.  
  116.     return 0;
  117.  
  118.  
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement