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

A simulation of a factory is to be written in C++ using pthreads. There are 3 wo

ID: 3575713 • Letter: A

Question

A simulation of a factory is to be written in C++ using pthreads. There are 3 workplaces named station 1, station 2, station 3, where components of the widget are produced Station 1 needs the screwdriver, and updates parts[0] of the parts list array. Station 2 needs both the screwdriver and saw to complete it part, it updates parts [1]. Station 3 needs the saw to construct its part, it updates parts[2]. For this problem it is your job to efficiently utilize the mutexes (screwdriver, updater, saw) to correctly coordinate the production of the parts. Be aware that we must be careful to avoid deadlock and your solution should not reduce the problem to sequential execution. Place the mutex methods in the correct order for this segment of code. const int num = 100000; int parts[3]; pthread_mutex_t screwdriver = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t updater = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t saw = PTHREAD_MUTEX_INITIALIZER; void *station1(void *arg) {while (parts(0]

Explanation / Answer

void *station1(void *arg){
while(parts[0]<num){
pthread_mutex_lock(&screwdriver); //this method loc ks the mutex screwdriver and does not allow                                                   // other methods to get screwdriver
pthread_mutex_lock(&updater);

parts[0]++;
cout<<"part 1 created "<<parts[0]<<endl;
pthread_mutex_unlock(&updater); //this will unlock mutator updater and release it for ohers to use
pthread_mutex_unlock(&screwdriver);

}
return NULL;
}

void *station2(void *arg){
while(parts[1]<num){
pthread_mutex_lock(&screwdriver);
pthread_mutex_lock(&saw);

pthread_mutex_lock(&updater);
parts[1]++;
cout<<"part 2 created "<<parts[1]<<endl;
pthread_mutex_unlock(&saw);
pthread_mutex_unlock(&updater);
pthread_mutex_unlock(&screwdriver);

}
return NULL;
}

void *station3(void *arg){
while(parts[2]<num){
pthread_mutex_lock(&saw);
pthread_mutex_lock(&updater);

parts[2]++;
cout<<"part 3 created "<<parts[2]<<endl;
pthread_mutex_unlock(&updater);
pthread_mutex_unlock(&saw);

}
return NULL;
}

// dead lock occurs when

1)station 1 is having screwdriver and waiting for updater  

station 2 is having updater and waiting for screwdriver.

this is avoided because station 2 will not get updater untill it has screw driver

2)station 2 is saw and wating for updater

station 3 is having updater and waiting screw

this is also avoided because station 3 cannot have updater untill it has saw.

3)station 1 is having screwdriver waiting for updater

station 2 having updater and waiting for saw

station 3 having saw and waiting for updater

this is also avoided because station 2 cannot have updater until it has screwdriver and saw.

--->its performance will be greater than that of sequential execution because

example:

first station 1 enters loop and gets the screwdriver and updater at the same time station 3 can enter into loop and get saw and wait for updater

next sation1 execute the

parts[0]++;
cout<<"part 1 created "<<parts[0]<<endl; satements and then release updater

next station3 gets updater and station1 releases screw driver

next station 1 check for loop condition , station 2 can get screwdriver, station 3 executes

parts[2]++;
cout<<"part 3 created "<<parts[2]<<endl;

next station 3 releases updater

next station 3 releases saw

next station 3 checks loop condition , station 2 gets saw

next station 2 gets updater and executes

parts[1]++;
cout<<"part 2 created "<<parts[1]<<endl;

--> in this way the concurrancy can be achieved without deadlocks

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