Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The deadline of submission is April 17th. Acceptable file format for submission

ID: 3810250 • Letter: T

Question

The deadline of submission is April 17th. 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. 2. Check your program carefully before submission. Problem 1. 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. 2. 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. 3. 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. 4. 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). Example An example program screenshot is shown in the following figure. Checkout 0:14 29 Checkout 1:20 30 Checkout 2:33 40 Check out 3:11 22 Checkout 4: 3 39

Explanation / Answer

Compile using

c99-gcc thread.c -lpthread

program.c

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

#define N 5
#define R 2
#define T1 100
#define T2 3
#define TMin 4
#define TMax 10

volatile int n[N]={2,2,2,2,2};
volatile int t[N][R] = {{14, 29},{20,30},{33,40},{11,22},{3,39}};

void* processCheckout(void *par) {
   for(int tim=0; tim < T1; tim++) {
       for(int q=0; q < N; q++) {
           t[q][0]--;
           if(t[q][0] == 0) {
               for(int c=0; c < n[q]-1; c++) {
                   t[q][c] = t[q][c+1];
               }
               n[q]--;
           }
       }
       sleep(1);
       system("clear");
       for(int q=0; q < N; q++) {
           printf("Checkout %d: ",q);
           for(int c=0; c < n[q]; c++) {
               printf("%d ",t[q][c]);
           }
           printf(" ");
       }
       for(int q=0; q < N; q++) {
           printf("%d ",n[q]);
       }
       printf(" ");
   }
   pthread_exit(NULL);
}

void* addCustomer(void *par) {
   for(int tim=0; tim < T1; tim += T2) {
       sleep(T2);
       int wa = TMin + rand()%(TMax-TMin);
       int mini = 0;
       for(int q=1; q < N; q++) {
           if(n[q] < n[mini])
               mini = q;
           //printf("Add customer mini: %d", mini);
       }
       if(n[mini] >= R) continue;
       n[mini]++;
       t[mini][n[mini]-1] = wa;  
   }
   pthread_exit(NULL);
}

int main() {
   pthread_t pc, ac;
   srand(time(NULL));
   pthread_create(&pc, NULL, processCheckout, NULL);
   pthread_create(&ac, NULL, addCustomer, NULL);
   pthread_exit(NULL);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote