logo

Multithread Application: Simulation of a Pizza Cafe with Synchronization

2 Pages606 Words131 Views
   

Added on  2019-09-16

About This Document

Simulate a pizza cafe with a chef and multiple customers using multithreading in a synchronized manner. The program stops when all customers finish eating their pizzas and no pizza is left in the kitchen. The number of customers, unsold pizzas, and seats are input by the user. Each customer buys one pizza and eats for a random time between 1 to 10 time units. The chef takes 5 time units to make a pizza and only makes enough pizzas for all customers. The output shows the arrival of customers, chef making pizzas, customers buying pizzas, customers getting seats, and customers leaving after eating.

Multithread Application: Simulation of a Pizza Cafe with Synchronization

   Added on 2019-09-16

ShareRelated Documents
Multithread Application: Consider a pizza cafe where there are only N seats. When a customer enters the store, (s)he buys a pizza and grabs a seat to eat. When the customer finishes eating, (s)he leaves the store. If there is any unsold pizza left, (s)he can get it immediately. Otherwise, (s)he need wait for the chef to make one. A chef in the kitchen just keeps making pizzas until there are M unsold pizzas left in the kitchen. (S)he resumes the work when a customer comes to buy one. After the customer gets the pizza, (s)he tries to find a seat. If there is any available seat, (s)he grabs the seat and eats the pizza. Otherwise, (s)he waits until another customer finishes eating and leaves the café.. You are asked to simulate this problem as following:1.Your program should first ask user to input M (the number of plates to hold unsold pizza) andN (the number of seats). You can also pass these two numbers through command line parameters. The maximum number should be 10 for both parameters. (10%)2.You need create two types of threads besides the main thread: chef and customer. There is only one chef thread but multiple customer threads. The number of customers should be randomly generated. Assuming the maximum number is 20. (For example, you can use random()/srandom() or drand48()/srand48() to generate a random number in C) (10%)3.Suppose each customer only buys one pizza. Their eating time is randomized between 1 to 10time units. Assume it takes the chef 5 time units to make a pizza and the chef only makes enough pizzas for all customers. Implement the chef and the customer threads. The program should stop when all customers finish eating their pizzas, and no pizza should be left in the kitchen at the end. (You can define a time unit by yourself, e.g. a second, or a microsecond, or a fixed number of loops) (30%)4.Synchronize the chef and customers so that at any time, there are no more than M unsold pizza and no more than N customers sitting and eating the pizza. (50%)5.You can choose any programming language (e.g. C/C++/Java/python) to implement this program. You can find some sample C and Java code on the blackboard.6.Your output may look like the following: Please input M: 5Please input N: 8The total number of customers is 17Create a chef threadCreate 17 customer threadsCustomer 1 arrivesChef makes pizza 1. 1 pizza unsoldChef makes pizza 2. 2 pizzas unsold.Customer 1 buys a pizza. 1 pizza unsold.Customer 2 arrivesCustomer 2 buys a pizza, 0 pizza unsold.Customer1 gets a seat, 7 seats left.Chef makes pizza 3. 1 pizza unsold.Customer 1 finishes eating and leaves. 8 seats left.Chef makes pizza 4. 2 pizzas unsold.Customer 3 arrives
Multithread Application: Simulation of a Pizza Cafe with Synchronization_1

End of preview

Want to access all the pages? Upload your documents or become a member.