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 sig_atomic_t player1_ready =0;
volatile sig_atomic_t player2_ready =0;
volatile sig_atomic_t game_over =0;
int player1_wins =0;
int player2_wins =0;
pid_t pid1, pid2;
void parentSignalHandler(int sig)
{
if (sig == SIGUSR1)
{
player1_ready =1; //update the players ready flag
}
else if (sig == SIGUSR2)
{
player2_ready =1; //update the players ready flag
}
else if (sig == SIGCHLD)
{
wait(NULL); //reap the children/clear the process table and get rid of zombies
}
else if (sig == SIGINT)
{
kill(0, SIGTERM); //terminate the children
}
}
void childSignalHandler(int sig){
if (sig == SIGUSR1)
{
}// Player 1 gets a hint that their guess is low
else if (sig == SIGUSR2)
{
}// Player 1 gets a hint that their guess is high
else if (sig == SIGINT)
{
game_over =1; // Stop guessing and reset
}
else if (sig == SIGTERM)
{
exit(0); // Exit on termination
}
}
void chld1()
{
struct sigaction sa;
sa.sa_handler = childSignalHandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags =0;
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
int min =0;
int max =101;
int guess;
int fd = open("player1_guesses.txt", O_RDWR | O_CREAT, 0644);
while(1)//loop forever game loop
{
kill(getppid(), SIGUSR2);
//loop forever guess loop
while(1)
{
player1_ready =0;
guess =(min + max)/2;
write(fd, &guess, sizeof(int));
sleep(1);
kill(getppid(), SIGUSR1);
while(!player1_ready)
{
sleep(1);
}
if (sig == SIGUSR1)
{
min = guess;
}
else if (sig == SIGUSR2)
{
max = guess;
}
else if (sig == SIGINT)
{
break;
}
}
}
close(fd);
exit(0);
}
void chld2()
{
struct sigaction sa;
sa.sa_handler = childSignalHandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags =0;
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
int min =0;
int max =101;
int guess;
int fd = open("player2_guess.txt", O_RDWR | O_CREAT, 0644);
while (1)
{
//game loop
kill(getppid(), SIGUSR2);
while(1)
{
player2_ready =0;
guess = min +(rand()%(max - min +1));
write(fd, &guess, sizeof(int));
sleep(1);
kill(getppid(), SIGUSR2);
while (!player2_ready)
{
sleep(1);
}
if (sig == SIGUSR1)
{
min = guess;
}
else if (sig == SIGUSR2)
{
max = guess;
}
else if (sig == SIGINT)
{
break;
}
}
}
close(fd);
exit(0);
}
void parent ()
{
struct sigaction sa;
sa.sa_handler = parentSignalHandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags =0;
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGCHLD, &sa, NULL);
int game_counter =0;
int target;
sleep(5);
//tell the children game is starting
kill(pid1, SIGUSR1);
kill(pid2, SIGUSR2);
while(game_counter <10)
{
//wait on each child ready signal
while(!(player1_ready && player2_ready))
{
sleep(1);
}
printf("Game %d: Player 1 wins =%d, Player 2 wins =%d
", game_counter, player1_wins, player2_wins);
//parent generates the random target
target =1+(rand()%100);
//loop forever-referee
while(1)
{
while (!(player1_ready && player2_ready))
{
sleep(1);
}
//open the guess files
int fd1= open("player1_guesses.txt", O_RDONLY);
int fd2= open("player2_guesses.txt", O_RDONLY);
int guess1;
int guess2;
read(fd1, &guess1, sizeof(int));
read(fd2, &guess2, sizeof(int));
close(fd1);
close(fd2);
//check the guesses and send the appropriate signal to each child
if(guess1< target)
{
kill(pid1, SIGUSR1);
}
else if(guess1> target)
{
kill(pid1, SIGUSR2);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!