Write a simulation program with Synchronization for the fruit market. The
farmer will be able to produce different types of fruits (apple, orange, grape,
and watermelon), and put them in the market to sell. The market has limited
capacity and farmers have to stand in a queue if the capacity is exceeded to
sell their fruits. Consumers can come to the market any time and purchase
their desired fruits; and if the fruits they want to buy runs out, they are
willing to wait until the supply of that kind is ready. (Hint: implementing
this market will encounter the producer and consumer problem, and it
probably needs multiple buffers for different kinds of fruits).
SOLUTION CODE FOR THE ABOVE QUESTION
import queue
import threading
import random
# Create a queue for raw messages
rawQueue = queue.Queue();
# Create a queue for farmers to put their products in the market to sell
farmers_Queue = queue.Queue();
# Create a queue for consumers to purchase a fruit
consumers_Queue = queue.Queue();
#add consumers in the queue
consumers_Queue.put("Customer 1");
consumers_Queue.put("Customer 3");
consumers_Queue.put("Customer 4");
consumers_Queue.put("Customer 2");
consumers_Queue.put("Customer 7");
consumers_Queue.put("Customer 8");
consumers_Queue.put("Customer 5");
consumers_Queue.put("Customer 6");
consumers_Queue.put("Customer 9");
#add farmers in the queue
farmers_Queue.put("farmer 1");
farmers_Queue.put("farmer 3");
farmers_Queue.put("farmer 5");
farmers_Queue.put("farmer 7");
farmers_Queue.put("farmer 9");
farmers_Queue.put("farmer 2");
farmers_Queue.put("farmer 6");
farmers_Queue.put("farmer 10");
farmers_Queue.put("farmer 4");
farmers_Queue.put("farmer 8");
#declare a list to store the number of apples, oranges grape and watermelon
#apples index 0, oranges index 1, grape index 2 and watermelon index 3;
myfruits_list = [0,0,0,0]
#we will define a thread simulating 20 operations
def market_operation():
for i in range(0,20):
#generate a random number
n = random.randint(1, 2)
#if 1 is generated allow a farmer to place fruits in the market
if n == 1:
farmers = farmers_Queue.get();
fruit = random.randint(1,4)
if fruit==1:
#get the farmer infront of the quee and allow him/her
print(farmers+" put one apple for sale")
#increment the number of apples in our list
myfruits_list[0] = myfruits_list[0]+1
elif fruit == 2:
# get the farmer infront of the quee and allow him/her
print(farmers+" put one orange for sale")
# increment the number of oranges in our list
myfruits_list[1] = myfruits_list[1] + 1
elif fruit == 3:
# get the farmer infront of the quee and allow him/her
print(farmers+" put one grape for sale")
# increment the number of grapes in our list
myfruits_list[2] = myfruits_list[2] + 1
else:
# get the farmer infront of the quee and allow him/her
print(farmers+" put one watermelon for sale")
# increment the number of watermelons in our list
myfruits_list[3] = myfruits_list[3] + 1
#if option is 2, allow a customer to buy a fruit
else:
customer_fruit = random.randint(1,4);
if customer_fruit == 1:
#Get the customer infront of the key and enable him/her to buy a fruit
customer = consumers_Queue.get();
#check if there is any apple
if myfruits_list[0]>0:
print(customer+" bought one apple")
#decrement the number of apples
myfruits_list[0] = myfruits_list[0]-1
#if this is not the case add, him or her in the queue for waiting
else:
consumers_Queue.put(customer)
print(customer +" is waiting to buy one apple")
elif customer_fruit == 2:
#Get the customer infront of the key and enable him/her to buy a fruit
customer = consumers_Queue.get();
#check if there is an orange
if myfruits_list[1]>0:
print(customer+" bought one orange")
# decrement the number of oranges
myfruits_list[1] = myfruits_list[1] - 1
#if this is not the case add, him or her in the queue for waiting
else:
consumers_Queue.put(customer)
print(customer +" is waiting to buy one orange")
elif customer_fruit == 3:
#Get the customer infront of the key and enable him/her to buy a fruit
customer = consumers_Queue.get();
#check if there is an grape
if myfruits_list[2]>0:
print(customer+" bought one grape")
# decrement the number of grapes
myfruits_list[2] = myfruits_list[2] - 1
#if this is not the case add, him or her in the queue for waiting
else:
consumers_Queue.put(customer)
print(customer +" is waiting to buy one grape")
else:
#Get the customer infront of the key and enable him/her to buy a fruit
customer = consumers_Queue.get();
#check if there is an orange
if myfruits_list[3]>0:
print(customer+" bought one watermelon")
# decrement the number of watermelons
myfruits_list[3] = myfruits_list[3] - 1
#if this is not the case add, him or her in the queue for waiting
else:
consumers_Queue.put(customer)
print(customer +" is waiting to buy one watermelon")
th1 = threading.Thread(target=market_operation, args=( ))
th1.start()
th1.join()
SAMPLE PROGRAM OUTPUT
Comments
Leave a comment