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 0 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 sig_atomic_t child1_ready =0;
volatile sig_atomic_t child2_ready =0;
volatile sig_atomic_t child1_guess =0;
volatile sig_atomic_t child2_guess =0;
int child1_score =0, child2_score =0;
int target =0;
// Signal handlers for the referee (parent)
void handle_sigusr1(int sig){
child1_ready =1;
child1_guess =1;
\ddots
void handle_sigusr2(int sig){
child2_ready =1;
child2_guess =1;
}
void handle_sigint(int sig){
printf("Game interrupted. Final scores:
");
printf("Player 1: %d, Player 2: %d
", child1_score, child2_score);
exit(0);
}
// Referee's game logic
void referee_game(int child1_pid, int child2_pid){
for (int game_round =1; game_round <=10; game_round++){
// Generate target number
target = rand()%100+1;
printf("Game %d: Target is %d
", game_round, target);
// Notify children to start guessing
kill(child1_pid, SIGUSR1);
kill(child2_pid, SIGUSR2);
while (1){
if (child1_ready && child2_ready){
// Read guesses from files
int guess1, guess2;
int fd1= open("guess1.txt",0_RDONLY);
int fd2= open("guess2.txt",0_RDONLY);
read(fd1, &guess1, sizeof(int));
read(fd2, &guess2, sizeof(int));
close(fd1);
close(fd2);
printf("Player 1 guessed: %d, Player 2 guessed: %d
", guess1, guess2);
// Compare guesses with the target
if (guess1== target){
child1_score++;
printf("Player 1 wins this round!
");
break;
} else if (guess1< target){
| kill(child1_pid, SIGUSR1); // Guess was too low
} else {
| kill(child1_pid, SIGUSR2); // Guess was too high
}
if (guess2== target){
|hild2_score++;
printf("Player 2 wins this round!
");
break;
} else if (guess2< target){
| kill(child2_pid, SIGUSR1); // Guess was too low
} else {
| kill(child2_pid, SIGUSR2); // Guess was too high
}
// Reset flags for the next round
child1_ready =0;
child2_ready =0;
}
// Reset the game by sending SIGINT to children
kill(child1_pid, SIGINT);
kill(child2_pid, SIGINT);
printf("Final scores:
Plaver 1: %d, Plaver 2: %d
", child1 score, child2 score);
```
}
// Player 1's guessing strategy (binary search)
void player1_logic(){
int min =0, max =100, guess;
while (1){
guess =(min + max)/2; // Binary search strategy
int fd = open("guess1.txt",0_WRONLY |0_CREAT |0_TRUNC, 0644);
write(fd, &guess, sizeof(int));
close(fd);
kill(getppid(), SIGUSR1); // Signal parent that guess is ready
pause(); // Wait for parent's response
if (child1_guess){
if (guess < target) min = guess;
if (guess > target) max = guess;
}
}
}
// Player 2's guessing strategy (random guessing)
void player2_logic(){
int min =0, max =100, guess;
srand(time(NULL));
while (1){
guess =(rand()%(max - min +1))+ min; // Random guess strategy
int fd = open("guess2.txt",0_WRONLY |0_CREAT |0_TRUNC, 0644);
write(fd, &guess, sizeof(int));
close(fd);
kill(getppid(), SIGUSR2); // Signal parent that guess is ready
pause(); // Wait for parent's response
if (child2_guess){
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(){
``````
srand(time(NULL)); // Seed random number generator
// Set up signal handlers
struct sigaction sa;
sa.sa_flags =0;
sa.sa_handler = handle_sigusr1;
sigaction(SIGUSR1, &sa, NULL);
sa.sa_handler = handle_sigusr2;
sigaction(SIGUSR2, &sa, NULL);
sa.sa_handler = handle_sigint;
sigaction(SIGINT, &sa, NULL);
pid_t child1_pid = fork();
if (child1_pid ==0){
| player1_logic(); // Player 1 code
}
pid_t child2_pid = fork();
if (child2_pid ==0){
| player2_logic(); // Player 2 code
}
referee_game(child1_pid, child2_pid); // Parent code
// Clean up
kill(child1_pid, SIGTERM);
kill(child2_pid, SIGTERM);
wait(NULL); // Reap child processes
wait(NULL);
return 0;
}
while (1){
if (child1_ready && child2_ready){
// Read guesses from files
int guess1, guess2;
int fd1= open("guess1.txt",0_RDONLY);
int fd2= open("guess2.txt",0_RDONLY);
read(fd1, &guess1, sizeof(int));
read(fd2, &guess2, sizeof(int));
close(fd1);
close(fd2);

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!