Question

PLEASE DO NOT COPY CODE FROM OTHER QUESTIONS POSTED WITH THIS. THAT IS C++ NOT PYTHON...

PLEASE DO NOT COPY CODE FROM OTHER QUESTIONS POSTED WITH THIS. THAT IS C++ NOT PYTHON

Please follow the step below.

1. You need to develop a multithread program in PYTHON to simulate a checkout in supermarket. The process of simulation will last for T1 seconds. You do not need to consider thread safety.

2. An integer sequence t is used to record the remaining waiting time for each customer.

3. One thread which runs every one second is used to simulate the processing of the checkout. (a) Reduce the remaining processing time of the first customer by one. (b) Remove the first customer if his remaining processing time reaches zero. (c) Clean the screen and show the new remaining processing time.

4. Another thread is used to generate one new customer every T2 seconds. (a) The new customer will leave the supermarket if he or she needs to join a queue with more than R customers. (b) The processing time for the new customer is a random integer in [Tmin; Tmax)

5. An example program run is shown as follows.

Checkout: 3 12 45 5 61, after one second, we have:

Checkout: 2 12 45 5 61, after one second, we have:

Checkout: 1 12 45 5 61 22, after one second, we have:

Checkout: 12 45 5 61 22, after one second, we have:

Checkout: 11 45 5 61 22

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please upvote if you like the answer, as it helps the community a lot.

Also if you have any doubts, feel free to ask in comments, we will reach you ASAP.

  • Note: for windows users
  • USE os.system('cls') instead of os.system('clear')

SOLUTION:

Code in Python3 ThreadQueueHandle.py

CODE(Be careful with indentation):

#!/usr/bin/python3

import threading

import time

import random

import os

exitFlag = 0

t=[6,5,7,3,8]

Tmin=1

Tmax=8

R=5

T1=20

T2=8

tsec=0


class myThread (threading.Thread):

def __init__(self, threadID,delay):

threading.Thread.__init__(self)

self.threadID = threadID

self.delay = delay

def run(self):

if self.threadID == 1:

inc_time(self.delay)

else:

add_customer()

def inc_time(delay):

global t,T1,tsec

while t!=[] and tsec<=T1:

if exitFlag:

threadName.exit()

time.sleep(delay)

os.system('clear')

print("Checkout: "+" ".join(map(str,t))+", after one second, we have")

t[0] -= 1

tsec+=1

if t[0]==0:

t.pop(0)

def add_customer():

global t,tsec,T2

flag=1

while t!=[] and tsec<=T1:

if tsec and tsec%T2==0:

if flag:

t.append(random.randrange(Tmin,Tmax))

flag=0

else:

flag=1


# Create new threads

thread1 = myThread(1,1)

thread2 = myThread(2,1)

# Start new Threads

thread1.start()

thread2.start()

thread1.join()

thread2.join()

SAMPLE I/O:

Checkout: 6 5 7 3 8, after one second, we have
Checkout: 5 5 7 3 8, after one second, we have
Checkout: 4 5 7 3 8, after one second, we have
Checkout: 3 5 7 3 8, after one second, we have
Checkout: 2 5 7 3 8, after one second, we have
Checkout: 1 5 7 3 8, after one second, we have
Checkout: 5 7 3 8, after one second, we have
Checkout: 4 7 3 8, after one second, we have
Checkout: 3 7 3 8 1, after one second, we have
Checkout: 2 7 3 8 1, after one second, we have
Checkout: 1 7 3 8 1, after one second, we have
Checkout: 7 3 8 1, after one second, we have
Checkout: 6 3 8 1, after one second, we have
Checkout: 5 3 8 1, after one second, we have
Checkout: 4 3 8 1, after one second, we have
Checkout: 3 3 8 1, after one second, we have
Checkout: 2 3 8 1 2, after one second, we have
Checkout: 1 3 8 1 2, after one second, we have
Checkout: 3 8 1 2, after one second, we have
Checkout: 2 8 1 2, after one second, we have
Checkout: 1 8 1 2, after one second, we have

Add a comment
Know the answer?
Add Answer to:
PLEASE DO NOT COPY CODE FROM OTHER QUESTIONS POSTED WITH THIS. THAT IS C++ NOT PYTHON...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is Python programming to develop a multithread program. Please follow the step below. 1. You...

    This is Python programming to develop a multithread program. Please follow the step below. 1. You need to develop a multithread program in Python to simulate a checkout in supermarket. The process of simulation will last for T1 seconds. You do not need to consider thread safety. 2. An integer sequence t is used to record the remaining waiting time for each customer. 3. One thread which runs every one second is used to simulate the processing of the checkout....

  • Python Please 3. Specification -Write a program that accepts as input a height in feet (or...

    Python Please 3. Specification -Write a program that accepts as input a height in feet (or meters...just be consistent with your units). The program should calulate the height after every second, assuming you release the ball at the starting height. (this function assumes you don't throw up or down) Postion Function: pos(t)--16 * t2 + startingHeight # Feet Equation pos(t)--4.9 * t2 + startinglieight # Meter Equation Print out the position after each print saying it reached the ground at...

  • Hello, I am having some trouble with a supermarket checkout simulation program in C++. What I...

    Hello, I am having some trouble with a supermarket checkout simulation program in C++. What I have so far just basically adds customers to the queue and prints when they arrive. I am struggling with how to implement a way of keeping track of when a given customer finishes(I will attach what I have so far). I had already created queue and node classes (with headers and cpp files) that I modified in my attempt. I would be very grateful...

  • Only do Q2 please!!! please Type the code in python!!!thanks Only do Q2 please!!! please Type...

    Only do Q2 please!!! please Type the code in python!!!thanks Only do Q2 please!!! please Type the code in python!!!thanks 1. (10 points) (Without Python) Unfortunately, Eddard is lost in the dark dungeon of the Red Keep. He knows there are three doors, and one of the doors will lead him to freedom. If Eddard takes Door A, he will wander around the dungeon for 3 days and return to where he started If he takes Door B, he will...

  • PLEASE DO THIS IN PYTHON!! 1.) Goal: Become familiar with The Python Interpreter and IDLE. Use...

    PLEASE DO THIS IN PYTHON!! 1.) Goal: Become familiar with The Python Interpreter and IDLE. Use IDLE to type, compile, and run (execute) the following programs. Name and save each program using the class name. Save all programs in one folder, call it Lab1-Practices. Save all programs for future reference as they illustrate some programming features and syntax that you may use in other labs and assignments. ================================== Program CountDown.py =================================== # Program CountDown.py # Demonstrate how to print to...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Exercise #2:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Exercise #2: Design and implement a program (name it CompareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array with integer values. The program defines method Compare() that takes two signal-dimensional arrays of type integer. The method compares the content of the arrays and returns...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to...

  • Description In this homework, you are asked to implement a multithreaded program that will allow ...

    Description In this homework, you are asked to implement a multithreaded program that will allow us to measure the performance (i.e, CPU utilization, Throughput, Turnaround time, and Waiting time in Ready Queue) of the four basic CPU scheduling algorithms (namely, FIFO, SJE PR, and RR). Your program will be emulating/simulating the processes whose priority, sequence of CPU burst time(ms) and I'O burst time(ms) will be given in an input file. Assume that all scheduling algorithms except RR will be non-preemptive,...

  • Please solve only if you know how to do it. Write the code using C++ (not...

    Please solve only if you know how to do it. Write the code using C++ (not Python or Java). Show and explain everything neatly. COMMENTS (7.5% of programming assignment grade): Your program should have at least ten (10) different detailed comments explaining the different parts of your program. Each individual comment should be, at a minimum, a sentence explaining a particular part of your code. You should make each comment as detailed as necessary to fully explain your code. You...

  • Please write the following code as simple as possible in python: You will need to define...

    Please write the following code as simple as possible in python: You will need to define a function with four arguments. Here is what I used: > def find_matches(file1.txt, output1.txt, strings, value): file1.txt will contain a list of various strings. The program must copy from the first argument, and it should be written in the second argument (the second file, "output1.txt"). The third and fourth arguments will determine which specific strings will be copied over to the second file. For...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT