Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <string.h>
- pthread_mutex_t mutex;
- // Variables tray, maximum and minimum
- int tray;
- int max = 4;
- int min = 0;
- char choice[10];
- void*chiefCook(void*arg){
- int pthread_mutex_lock(pthread_mutex_t*mutex);
- while (1)
- {
- if(tray<max){
- tray=tray+1;
- printf("the chef is adding PIZZA. The trial is: %d\n", tray);
- }
- else if(tray == max){
- printf("Client, the tray is full you can eat\n");
- return NULL;
- }
- }
- int pthread_mutex_unlock(pthread_mutex_t*mutex);
- }
- void*clientEat(void*arg){
- int pthread_mutex_lock(pthread_mutex_t*mutex);
- while (1)
- {
- if(tray>min){
- tray=tray-1;
- printf("The remaining trial is : %d\n", tray);
- }
- else if (tray == min)
- {
- printf("Chief tray is empty make some PIZZA \n");
- return NULL;
- }
- }
- int pthread_mutex_unlock(pthread_mutex_t*mutex);
- }
- int main(){
- pthread_t client, chief;
- 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");
- fgets(choice, 10, stdin);
- // scanf("%s", choice);
- char comp[4] = "cook";
- int counter=0;
- for (int i = 0; i < 4; i++)
- {
- if(choice[i] != comp[i]){
- counter = counter + 1;
- }
- }
- if (counter > 0)
- {
- printf("He chose to eat\n");
- }else
- {
- printf("He want to Cook\n");
- }
- printf("How many PIZZA is in the tray: ");
- scanf("%d", &tray);
- printf("The number in tray is %d\n", tray);
- if (tray == max && (counter > 0 || counter ==0))
- {
- pthread_create(&client,NULL, clientEat, NULL);
- pthread_create(&chief,NULL, chiefCook, NULL);
- }
- else if(tray == min && (counter > 0 || counter == 0))
- {
- pthread_create(&chief,NULL, chiefCook, NULL);
- pthread_create(&client,NULL, clientEat, NULL);
- }
- pthread_join(client,NULL);
- pthread_join(chief, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement