Question

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. (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

//Include the needed header files
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

//Declare N as constant
#define N 5

//Declare R as constant
#define R 6

//Declare T1 as constant
#define T1 60

//Declare T2 as constant
#define T2 3

//Declare TMIN as constant
#define TMIN 20

//Declare TMAX as constant
#define TMAX 30

//Declare the array t[N][R]
int t[N][R];

//Declare the array n[N]
int n[N];

//Method definition
void function1(void parameter)
{
//Declare the needed variables
int count = 0, index1, index2;

//Loop
while(count<T1)
{
//Increment counter
count++;

//Loop
for(index1=0;index1<N;index1++)
{
//Check condition
if(n[index1]>0)
{
//Check condition
if(t[index1][0]==0)
{
//Decrement
n[index1]--;

//Loop
for(index2=0;index2<n[index1];index2++)
{
//Update
t[index1][index2]=t[index1][index2+1];

//Update
t[index1][n[index1]]=0;
}
}

//Otherwise
else

//Decrement
t[index1][0]--;
}
}

//Output for every checkout every customers time
for(index1=0;index1<N;index1++)
{
//Print
printf("checkout %d :",index1);

//Loop
for(index2=0;index2<R;index2++)
{
//Check condition
if(t[index1][index2]!=0)

//Print
printf("%d ",t[index1][index2]);
}

//Print
printf("customers %d ",n[index1]);

//Print new line
printf(" ");
}

//Delay
sleep(1);

//Clear
system("clear");
}

//Stop
exit(1);
}

//Method definition
void function2(void parameter)
{
//Declare the needed variables
int index1, index2, minValue, place;

//Loop
while(1)
{
//Initialize
minValue=n[0];
place=0;

//Loop
for ( index1 = 0 ; index1 < N ; index1++ )
{
//Condition
if ( n[index1] < minValue )
{
//Update
minValue = n[index1];
place = index1;
}
}

//Compute
t[place][minValue]= rand()%(TMAX - TMIN)+TMIN;

//Increment
n[place]++;

//Delay
sleep(T2);
}
}

//Driver
int main()
{
//Create thread variables
pthread_t td1,td2;

//Declare a variable
int retValue;

//Start thread1
retValue=pthread_create(&td1,NULL,function1,NULL);

//Check condition
if(retValue!=0)
{
//Print
printf("error");

//Exit
exit(1);
}

//Start thread2
retValue=pthread_create(&td2,NULL,function2,NULL);

//Check condition
if(retValue!=0)
{
//Print
printf("error");

//Exit
exit(1);
}

//Thread join
pthread_join(td1,NULL);

//Thread join
pthread_join(td2,NULL);

//Stop
return 0;
}

Add a comment
Know the answer?
Add Answer to:
This is Python programming to develop a multithread program. Please follow the step below. 1. You...
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
  • 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...

  • 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...

  • (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that...

    (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that you have been provided on Canvas. That file has a name and 3 grades on each line. You are to ask the user for the name of the file and how many grades there are per line. In this case, it is 3 but your program should work if the files was changed to have more or fewer grades per line. The name and...

  • 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,...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

  • PLEASE DO THIS IN PYTHON!!!!!!!! 1.) For each lab program you develop, make sure to include...

    PLEASE DO THIS IN PYTHON!!!!!!!! 1.) For each lab program you develop, make sure to include the following header - replace the dots with your section #, semester, your full name, your instructor’s name, and lab #:                         Class: CSE 1321L Section:       ...          Term:          ... Instructor:    ... Name:          ...    Lab#:          ... Make sure to put the correct comment character(s) before the above lines in each file. C# & Java use // for comments, Python uses a # Exercise #1:...

  • please write in python Write a program that asks for beginning and ending number. The program...

    please write in python Write a program that asks for beginning and ending number. The program generates a set of 20 random numbers between the numbers and displays a count of each number generated. Sample output Enter starting and ending number separated by comma: 5, 25 5 occurs 2 times 7 occurs 1 time 8 occurs 1 time 10 occurs 2 times 12 occurs 1 time 13 occurs 2 time 15 occurs 5 times 16 occurs 1 time 20 occurs...

  • 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...

  • C++ Programming By using Binary heap Develop a CPP program to test is an array conforms...

    C++ Programming By using Binary heap Develop a CPP program to test is an array conforms heap ordered binary tree. This program read data from cin (console) and gives an error if the last item entered violates the heap condition. Use will enter at most 7 numbers. Example runs and comments (after // ) are below. Your program does not print any comments. An output similar to Exp-3 and Exp-4 is expected. Exp-1: Enter a number: 65 // this is...

  • Write a Python program that prints a name as shown in the image below. The program...

    Write a Python program that prints a name as shown in the image below. The program asks the user to enter a name and how many time the user wants to display the name.. You need two to input two integer values. First, for repeating name in a row. Second, for number of rows. In total, your program will have three inputs. Add three comment lines at start of the program, which shows program purpose, your name and date. Hint:...

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