Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

4. Threads The program below makes two child threads: a guessing thread and an answering thread. It uses 3 global integers, 1 pthread_mutex_t, and 2

4. Threads The program below makes two child threads: a guessing thread and an

answering thread. It uses 3 global integers, 1 pthread_mutex_t, and 2 pthread_cond_t

turn: Tells whose turn it is, either ANSWERER_TURN or GUESSER_TURN. guess: Holds the most recent guess generated by the guessing thread.shouldContinue: Holds 1 while the program should continue (the guesser does not have the correct number). Holds 0 after the guesser guesses the correct number. lock: a pthread_mutex_t variable. guessersTurn: a pthread_cond_t variable. answerersTurn: a pthread_cond_t variable.

Output:

$ ./guesser (Don't tell, but the answer is 7) Is the answer 6? Sorry, the answer is not 6 Is the answer 9? Sorry, the answer is not 9 Is the answer 19? Sorry, the answer is not 19 Is the answer 17? Sorry, the answer is not 17 Is the answer 31? Sorry, the answer is not 31 Is the answer 10? Sorry, the answer is not 10 Is the answer 12? Sorry, the answer is not 12 Is the answer 9? Sorry, the answer is not 9 Is the answer 13? Sorry, the answer is not 13 Is the answer 26? Sorry, the answer is not 26 Is the answer 11? Sorry, the answer is not 11 Is the answer 18? Sorry, the answer is not 18 Is the answer 27? Sorry, the answer is not 27 Is the answer 3? Sorry, the answer is not 3 Is the answer 6? Sorry, the answer is not 6 Is the answer 28? Sorry, the answer is not 28 Is the answer 2? Sorry, the answer is not 2 Is the answer 20? Sorry, the answer is not 20 Is the answer 24? Sorry, the answer is not 24 Is the answer 27? Sorry, the answer is not 27

Is the answer 8? Sorry, the answer is not 8 Is the answer 7? Congratulations! The answer is 7 

Please finish the program.

One child thread should run answerer(), the other should run guesser(). The two threads should take turns: the guessing thread should make a guess and then the answering thread should compare it with answer (a local variable that only it has).

If the guesser got the wrong number, then the guesser should go again.

Etc.

I have finished main(), which has initializes all objects. Please finish answerer()and guesser(). The vPtr argument is NULL and may be ignored. However, please figure out:

What needs to be protected.

Where should the locks and unlock go?. Where should conditions and signals go.

/*

* */

guesser.c 
#include  #include  #include  #define ANSWERER_TURN #define GUESSER_TURN 
int turn int guess int shouldContinue = 1; pthread_mutex_t lock; pthread_cond_t guessersTurn; pthread_cond_t answerersTurn; 
void* answerer { 

(void* vPtr)

int answer = rand() % 32; printf("(Don't tell, but the answer is %d) ",answer); 

while (1) {

  (a) YOUR CODE HERE 
 while (turn != ANSWERER_TURN) { 
 (b) YOUR CODE HERE 

}

 if (guess == answer) { 
 printf("Congratulations! The answer is %d ",answer); shouldContinue = 0; 

0 1

= GUESSER_TURN; = -1; 
 turn = GUESSER_TURN; 

(c) YOUR CODE HERE

(d) YOUR CODE HERE

break;

} else

 printf("Sorry, the answer is not %d ",guess); 
 turn = GUESSER_TURN; 
 (e) YOUR CODE HERE 
 (f) YOUR CODE HERE 

}

 return(NULL); } 
void* guesser { 

while (1) {

  (g) YOUR CODE HERE 

(void* vPtr)

while (turn != GUESSER_TURN) { 
 (h) YOUR CODE HERE 

}

if ( !shouldContinue ) break; 
guess = rand() % 32; printf("Is the answer %d? ",guess); turn = ANSWERER_TURN; 
(i) YOUR CODE HERE 
(j) YOUR CODE HERE 

}

 return(NULL); } 

int main ()

pthread_t answererId; pthread_t guesserId; 
 pthread_mutex_init(&lock,NULL); pthread_cond_init(&answerersTurn,NULL); pthread_cond_init(&guessersTurn,NULL); 
 pthread_create(&answererId,NULL,answerer,NULL); pthread_create(&guesserId, NULL,guesser, NULL); 
 pthread_join(answererId,NULL); pthread_join(guesserId,NULL); 
 pthread_mutex_destroy(&lock); pthread_cond_destroy(&answerersTurn); pthread_cond_destroy(&guessersTurn); 

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Image Databases Towards Advanced Image Retrieval

Authors: Yihong Gong

1st Edition

1461375037, 978-1461375036

More Books

Students also viewed these Databases questions

Question

1. Explain why strategic planning is important to all managers.

Answered: 1 week ago

Question

Persuasive Speaking Organizing Patterns in Persuasive Speaking?

Answered: 1 week ago