Question: Can you help me make this C program work properly #include #include #include #include #include #include #include #include #include #include volatile sig _ atomic _
Can you help me make this C program work properly
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
volatile sigatomict playerready ;
volatile sigatomict playerready ;
volatile sigatomict gameover ;
int playerwins ;
int playerwins ;
pidt pid pid;
void parentSignalHandlerint sig
if sig SIGUSR
playerready ; update the players ready flag
else if sig SIGUSR
playerready ; update the players ready flag
else if sig SIGCHLD
waitNULL; reap the childrenclear the process table and get rid of zombies
else if sig SIGINT
kill SIGTERM; terminate the children
void childSignalHandlerint sig
if sig SIGUSR
Player gets a hint that their guess is low
else if sig SIGUSR
Player gets a hint that their guess is high
else if sig SIGINT
gameover ; Stop guessing and reset
else if sig SIGTERM
exit; Exit on termination
void chld
struct sigaction sa;
sasahandler childSignalHandler;
sigemptyset&sasamask;
sasaflags ;
sigactionSIGUSR &sa NULL;
sigactionSIGUSR &sa NULL;
sigactionSIGINT &sa NULL;
sigactionSIGTERM &sa NULL;
int min ;
int max ;
int guess;
int fd openplayerguesses.txt ORDWR OCREAT, ;
whileloop forever game loop
killgetppid SIGUSR;
loop forever guess loop
while
playerready ;
guess min max;
writefd &guess, sizeofint;
sleep;
killgetppid SIGUSR;
whileplayerready
sleep;
if sig SIGUSR
min guess;
else if sig SIGUSR
max guess;
else if sig SIGINT
break;
closefd;
exit;
void chld
struct sigaction sa;
sasahandler childSignalHandler;
sigemptyset&sasamask;
sasaflags ;
sigactionSIGUSR &sa NULL;
sigactionSIGUSR &sa NULL;
sigactionSIGINT &sa NULL;
sigactionSIGTERM &sa NULL;
int min ;
int max ;
int guess;
int fd openplayerguess.txt ORDWR OCREAT, ;
while
game loop
killgetppid SIGUSR;
while
playerready ;
guess min randmax min ;
writefd &guess, sizeofint;
sleep;
killgetppid SIGUSR;
while playerready
sleep;
if sig SIGUSR
min guess;
else if sig SIGUSR
max guess;
else if sig SIGINT
break;
closefd;
exit;
void parent
struct sigaction sa;
sasahandler parentSignalHandler;
sigemptyset&sasamask;
sasaflags ;
sigactionSIGUSR &sa NULL;
sigactionSIGUSR &sa NULL;
sigactionSIGINT &sa NULL;
sigactionSIGCHLD &sa NULL;
int gamecounter ;
int target;
sleep;
tell the children game is starting
killpid SIGUSR;
killpid SIGUSR;
whilegamecounter
wait on each child ready signal
whileplayerready && playerready
sleep;
printfGame d: Player wins d Player wins d
gamecounter, playerwins, playerwins;
parent generates the random target
target rand;
loop foreverreferee
while
while playerready && playerready
sleep;
open the guess files
int fd openplayerguesses.txt ORDONLY;
int fd openplayerguesses.txt ORDONLY;
int guess;
int guess;
readfd &guess sizeofint;
readfd &guess sizeofint;
closefd;
closefd;
check the guesses and send the appropriate signal to each child
ifguess target
killpid SIGUSR;
else ifguess target
killpid SIGUSR;
