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

Need to be written in C Three salesmen come to a manufacturer to get some T-shir

ID: 3919274 • Letter: N

Question

 Need to be written  in C  Three salesmen come to a manufacturer to get some T-shirts to sell on the market.  The total number of T-shirts is 1000. Saleman A each time takes 1/3 of the remaining  T-shirts. B takes 1/4 of the remaining T-shirts and C takes 1/5 of the remaining.  If the number of T-shirts for a saleman to take is not an integer, round it up to  the ceiling integer. For example, if the result is 3.14, round it up to 4.   Use three threads to simulate this. Each thread should execute in a loop. When the T-shirts are gone, stop all threads and terminate the program. Print out the T-shirts taken by each salesman each time. In the end, you add up all the T-shirts given out. If the total does not come up to 1000, your program is wrong.  a. Write a program without considering the mutual exclusion. Run the program several times to show that mutual exclusion is not guaranteed and the result is not correct.   b. Write the program again considering the mutual exclusion. Run the  program several times to show that mutual exclusion is guaranteed.  To make longer execution of threads, use some sleep(n) functions in  the program. Experiment with n to choose the best one to show the  results.  The output may look like this (yours maybe different):  A takes away 334 T-shirts. B takes away 167 T-shirts. C takes away 100 T-shirts. B takes away 100 T-shirts. ....  The total number of T-shirts given out is: ... 

Explanation / Answer

here is your program : ------------------->>>>>>>>>>>>

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

/* Define globally accessible variables and a mutex */

#define NUMTHRDS 3
double tshirts;
pthread_t callThd[NUMTHRDS];
pthread_mutex_t mutexsum;


void *dotprod(void *arg)
{

   /* Define and use local variables for convenience */

   int i = *((int*)arg);
   char arr[3] = {'A','B','C'};
   double m = 0;


   pthread_mutex_lock (&mutexsum);
   while(tshirts > 0){
   m = (tshirts/(3+i));
   m = round(m+0.49);
   tshirts = tshirts - m;
   pthread_mutex_unlock (&mutexsum);
   printf(" %c Takes away %d T-Shirts. ",arr[i],(int)m);
   sleep(3);
pthread_mutex_lock (&mutexsum);
   }
   pthread_mutex_unlock (&mutexsum);
  
   pthread_exit((void*) 0);
}

/*
The main program creates threads which do all the work and then
print out result upon completion. Before creating the threads,
the input data is created. Since all threads update a shared global variable tshirts,
we need a mutex for mutual exclusion. The main thread needs to wait for
all threads to complete, it waits for each one of the threads. We specify
a thread attribute value that allow the main thread to join with the
threads it creates. Note also that we free up handles when they are
no longer needed.
*/

int main (int argc, char *argv[])
{
   int i;
   int k[3] = {0,1,2};
   void *status;
   tshirts = 1000;

   pthread_mutex_init(&mutexsum, NULL);            

   for(i=0;i<NUMTHRDS; i++)
   {
      /*
      Each thread works on a different set of data.
      The offset is specified by 'i'.
      */
      //printf(" %d",i);
      pthread_create(&callThd[i], NULL, dotprod, (void *)&k[i]);
   }

   /* Wait on the other threads */
   for(i=0; i<NUMTHRDS; i++)
   {
      pthread_join(callThd[i], &status);
   }

   /* After joining, print out the results and cleanup */
   printf (" The total number of t-shirts given out is = %0.2f ",1000-tshirts);
   pthread_mutex_destroy(&mutexsum);
   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