Acceptable file format for submission is C source file, not a text file, archive
ID: 3811558 • Letter: A
Question
Acceptable file format for submission is C source file, not a text file, archive file, word document or executable file. You just need to fill the main body of the file hw5.c. Check your program carefully before submission. You need to write a multithread program in UNIX to simulate the checkouts in a supermarket. The supermarket has N checkouts and the process of simulation will last for T1 seconds. You do not need to consider thread safety. The two-dimension integer array t[N][R] is to record the remaining waiting time for each customer. The one-dimension integer array n[N] is to record the number of customers in each queue. One thread which runs every one second is used to simulate the processing of all the checkouts. This thread need to do the following things. (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. Another thread is used to generate one new customer every T2 seconds. (a) The new customer will join the queue with the least customers and leave the supermarket if he need to join a queue with more than R customers. (b) The processing time for the new customer is a randomly generated integer in the range of [Tmin; Tmax). An example program screenshot is shown in the following figure.Explanation / Answer
Answer: See the code below:
---------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
//number of checkouts
#define N 5
//time required to run simulation
#define T1 5
//max number of customers in a checkout (queue)
#define R 5
//time required to generate a new customer
#define T2 2
//Tmin
#define Tmin 1
//Tmax
#define Tmax 10
//array to record remaining waiting time for each customer
int t[N][R];
//array to record number of customers in each checkout (queue)
int n[N];
//function for first thtread
void *first_thread(void *vargp)
{
//sleep(1);
int i,j;
printf("first thread ");
for(i=0;i<N && n[i]!=0;i++)
{
if(t[i][0]==0)
{
for(j=1;j<n[i];j++)
{
t[i][j-1]=t[i][j];
}
n[i]--;
}
else
t[i][0]--;
}
system("clear");
for(i=0;i<N;i++)
{
printf("Checkout %d:%d %d ",i,t[i][0],n[i]);
}
return NULL;
}
//function for second thread
void *second_thread(void *vargp)
{
//sleep(1);
printf("second thread ");
//add a new customer to the queue, if less than R
int min_customer_queue=0,i,r;
for(i=1;i<N;i++)
{
if(n[i] < n[min_customer_queue])
{
min_customer_queue = i;
}
}
printf("min queue:%d ",min_customer_queue);
if(n[min_customer_queue] < R)
{
n[min_customer_queue]++;
printf("min:%d,per:%d ",min_customer_queue,n[min_customer_queue]);
//generates processing time between Tmin and Tmax
r=(rand()%(Tmax-Tmin))+Tmin;
t[min_customer_queue][n[min_customer_queue]-1]=r;
printf("r:%d ",r);
}
return NULL;
}
int main()
{
int done=0;
pthread_t tid1, tid2;
printf("before thread ");
while(done<=T1)
{
sleep(T2);
pthread_create(&tid2, NULL, second_thread, NULL);
pthread_join(tid2, NULL);
sleep(1);
pthread_create(&tid1,NULL, first_thread, NULL);
pthread_join(tid1, NULL);
done++;
}
printf("After thread ");
exit(0);
}
------------------------------
Note: You can change the values of symbolic constants like N, T1, T2, R, Tmin and Tmax used in program as per your requirement.