You will simulate a production line found in a typical Industrial plant. The man
ID: 3715529 • Letter: Y
Question
You will simulate a production line found in a typical Industrial plant. The manufacturing of these widgets requires three stages. The first stage requires 10 workers. In the first stage each worker does what is called the rough assemble of the widget. The second stage will do the final assembly. In this stage 15 workers are charged with this task. The third and final stage is where the widget gets painted and inspected. There are only 5 workers required for the third and final stage. Your job is to simulate this manufacturing process using threads instead of workers. Each day the manufacturing produces 100 widgets. Each thread in stage 1 will call rough(). Each thread in stage 2 will call the final_assembly() method and in stage 3 each thread will call the methods paint() followed by inspection(). You must include these libraries: #include <cstdlib> #include <time.h> void rough(){ int i,n; srand((unsigned)time(NULL)); n = int(1000*(1.0*rand()/RAND_MAX)); for(i = 0; i<= n; i++); } void final_assembly(){ int i,n; srand((unsigned)time(NULL)); n = int(1000*(1.0*rand()/RAND_MAX)); for(i = 0; i<= n; i++); } void paint(){ int i,n; srand((unsigned)time(NULL)); n = int(1000*(1.0*rand()/RAND_MAX)); for(i = 0; i<= n; i++); } void inspection(){ int i,n; srand((unsigned)time(NULL)); n = int(1000*(1.0*rand()/RAND_MAX)); for(i = 0; i<= n; i++); } Input: None Output: Write to a text file named output3.txt the following: Example: Worker thread 3 completes rough assembly on widget # Worker thread 14 completes final assembly on widget # Worker thread 1 completes rough assembly on widget # Worker thread 22 completes painting and inspection on widget #
Explanation / Answer
#include <stdio.h>
#include <cstdlib.h>
#include <pthread.h>
#include<time.h>
// We should use the global variables in order to change the threads
int k=0;
// The function to be executed by all threads
void *myThread1(void *var1)
{
// Store the value argument passed to this thread
int *myid1 = (int *)var1;
const clock_t START = clock();
// Let us create a static variable to observe the thread activities
static int a = 0;
++a; ++k;
rough();
const double T_ELAP1=(double)(clock()-START)/CLOCKS_PER_SEC;
// Print the output1
printf("Thread ID: %d, Static: %d, Global: %d Time Elapsed:%d ", *myid1,++a,++k,T_ELAP1);
}
void rough(){
int i,n;
srand((unsigned)time(NULL));
n = int(1000*(1.0*rand()/RAND_MAX));
for(i = 0; i<= n; i++);
}
void *myThread2(void *var2)
{
// Store the value argument passed to this thread
int *myid2 = (int *)var2;
const clock_t START= clock();
// Let us create a static variable to observe the thread activities
static int b = 0;
++b; ++k;
final_assembly();
const double T_ELAP2=(double)(clock()-START)/CLOCKS_PER_SEC;
// Print the output
printf("Thread ID: %d, Static: %d, Global: %d Time Elapsed:%d ", *myid2,++b,++k,T_ELAP2);
}
void final_assembly()
{
int i,n;
srand((unsigned)time(NULL));
n = int(1000*(1.0*rand()/RAND_MAX));
for(i = 0; i<= n; i++);
}
void *myThread3(void *var3)
{
// Store the value argument passed to this thread
int *myid3=(int *)var3;
const clock_t START=clock();
// Let us create a static variable to observe the thread activities
static int c= 0;
++c; ++k;
final_assembly();
paint();
inspection();
const double T_ELAP3=(double)(clock()- START)/CLOCKS_PER_SEC;
// Print the output
printf("Thread ID: %d, Static: %d, Global: %d Time Elapsed:%d ",*myid3,++c,++k,T_ELAP3);
}
int main()
{
int i;
pthread_t tid1,tid2,tid3;
const clock_t START = clock();
// Let us create three threads
for (i = 0;i<=3;i++);
pthread_create(&tid1, NULL, myThread1, (void *)i);
pthread_create(&tid2, NULL, myThread2, (void *)i);
pthread_create(&tid3, NULL, myThread3, (void *)i);
pthread_exit(NULL);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.