Question: this code is stuck in a never ending loop where one child guesses 0 and child two guesses one. the game number isnt displayed or
this code is stuck in a never ending loop where one child guesses and child two guesses one. the game number isnt displayed or incremented
Please fix the following code and run it in vscode on mac os using
gcc o filename filename.c
please send the output of the terminal
please make sure it completes all ten games and handles the signals properly
PLEASE PLEASE do not send code that will not work. This is my fifth time asking the same question that other "experts" have not properly completed. COMPILE AND RUN, SEND instructions on how to fix the code
#include
#include
#include
#include
#include
#include
#include
Global variables for signal handling and game state
volatile sigatomict childready ;
volatile sigatomict childready ;
volatile sigatomict childguess ;
volatile sigatomict childguess ;
int childscore childscore ;
int target ;
Signal handlers for the referee parent
void handlesigusrint sig
childready ;
childguess ;
ddots
void handlesigusrint sig
childready ;
childguess ;
void handlesigintint sig
printfGame interrupted. Final scores:
;
printfPlayer : d Player : d
childscore, childscore;
exit;
Referee's game logic
void refereegameint childpid, int childpid
for int gameround ; gameround ; gameround
Generate target number
target rand;
printfGame d: Target is d
gameround, target;
Notify children to start guessing
killchildpid, SIGUSR;
killchildpid, SIGUSR;
while
if childready && childready
Read guesses from files
int guess guess;
int fd openguesstxtRDONLY;
int fd openguesstxtRDONLY;
readfd &guess sizeofint;
readfd &guess sizeofint;
closefd;
closefd;
printfPlayer guessed: d Player guessed: d
guess guess;
Compare guesses with the target
if guess target
childscore;
printfPlayer wins this round!
;
break;
else if guess target
killchildpid, SIGUSR; Guess was too low
else
killchildpid, SIGUSR; Guess was too high
if guess target
hildscore;
printfPlayer wins this round!
;
break;
else if guess target
killchildpid, SIGUSR; Guess was too low
else
killchildpid, SIGUSR; Guess was too high
Reset flags for the next round
childready ;
childready ;
Reset the game by sending SIGINT to children
killchildpid, SIGINT;
killchildpid, SIGINT;
printfFinal scores:
Plaver : d Plaver : d
child score, child score;
Player s guessing strategy binary search
void playerlogic
int min max guess;
while
guess min max; Binary search strategy
int fd openguesstxtWRONLY CREAT TRUNC, ;
writefd &guess, sizeofint;
closefd;
killgetppid SIGUSR; Signal parent that guess is ready
pause; Wait for parent's response
if childguess
if guess target min guess;
if guess target max guess;
Player s guessing strategy random guessing
void playerlogic
int min max guess;
srandtimeNULL;
while
guess randmax min min; Random guess strategy
int fd openguesstxtWRONLY CREAT TRUNC, ;
writefd &guess, sizeofint;
closefd;
killgetppid SIGUSR; Signal parent that guess is ready
pause; Wait for parent's response
if childguess
if guess target min guess;
if guess target max guess;
Main function that sets up signal handlers, forks children, and starts the game
int main
srandtimeNULL; Seed random number generator
Set up signal handlers
struct sigaction sa;
sasaflags ;
sasahandler handlesigusr;
sigactionSIGUSR &sa NULL;
sasahandler handlesigusr;
sigactionSIGUSR &sa NULL;
sasahandler handlesigint;
sigactionSIGINT &sa NULL;
pidt childpid fork;
if childpid
playerlogic; Player code
pidt childpid fork;
if childpid
playerlogic; Player code
refereegamechildpid, childpid; Parent code
Clean up
killchildpid, SIGTERM;
killchildpid, SIGTERM;
waitNULL; Reap child processes
waitNULL;
return ;
while
if childready && childready
Read guesses from files
int guess guess;
int fd openguesstxtRDONLY;
int fd openguesstxtRDONLY;
readfd &guess sizeofint;
readfd &guess sizeofint;
closefd;
closefd;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
