Question: In c , I am attempting to create a main game file that communicates with other files using signals. For the main file, I have

In c, I am attempting to create a main game file that communicates with other files using signals. For the main file, I have something like this
#include
#include
#include "dungeon_info.h"
#include
#include
#include
int main(){
// Open shared memory
int shm_fd = shm_open(dungeon_shm_name, O_CREAT | O_RDWR,0666);
if (shm_fd ==-1){
perror("Failed to open shared memory");
return 1;
}
// Truncate
if (ftruncate(shm_fd, sizeof(struct Dungeon))==-1){
perror("Failed to truncate shared memory");
close(shm_fd);
return 1;
}
//Map dungeon
struct Dungeon* dungeon = mmap(NULL, sizeof(struct Dungeon), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd,0);
if (dungeon == MAP_FAILED){
perror("Failed to map shared memory");
close(shm_fd);
return 1;
}
// Initialize dungeon
dungeon->running = true;
// Initialize pid threads
pid_t barbarian_pid, wizard_pid, rogue_pid;
// Forks for all the characters
if ((barbarian_pid = fork())==0){
execl("./barbarian", "barbarian", (char *) NULL);
perror("Failed to exec barbarian");
exit(1);
}
if ((wizard_pid = fork())==0){
execl("./wizard", "wizard", (char *) NULL);
perror("Failed to exec wizard");
exit(1);
}
if ((rogue_pid = fork())==0){
execl("./rogue", "rogue", (char *) NULL);
perror("Failed to exec rogue");
exit(1);
}
// RunDungeon call
RunDungeon(wizard_pid, rogue_pid, barbarian_pid);
// Wait for all children to exit
int status;
while (wait(&status)>0);
// Clean up shared memory and close
munmap(dungeon, sizeof(struct Dungeon));
close(shm_fd);
shm_unlink(dungeon_shm_name);
return 0;
}
barbarian.c file looks like this:
#include
#include
#include
#include
#include
#include
#include
#include "dungeon_info.h"
//GLOBAL DUNGEON variable
struct Dungeon* dungeon;
void barbarian_attack(int sig){
if (sig == DUNGEON_SIGNAL){
// Copy the enemy's health to the attack field when the signal is received
dungeon->barbarian.attack = dungeon->enemy.health;
printf("Barbarian attacks with power: %d
", dungeon->barbarian.attack);
// Sleep for the defined duration to simulate attack delay
sleep(SECONDS_TO_ATTACK);
// After the sleep, check if the attack value still matches the enemy's health
if (dungeon->barbarian.attack == dungeon->enemy.health){
printf("Attack successful!
");
} else {
printf("Attack failed or enemy health changed!
");
}
}
}
int main(){
// Shared memory
int shm_fd = shm_open(dungeon_shm_name, O_RDWR,0666);
if (shm_fd ==-1){
perror("Failed to open shared memory");
exit(1);
}
dungeon = mmap(NULL, sizeof(struct Dungeon), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd,0);
// Check if I fuck up the shared memory map
if (dungeon == MAP_FAILED){
perror("Failed to map shared memory");
close(shm_fd);
exit(1);
}
signal(DUNGEON_SIGNAL, barbarian_attack);
while (dungeon->running){
pause(); // Wait for the signal to attack
}
// Clean up and close
munmap(dungeon, sizeof(struct Dungeon));
close(shm_fd);
return 0;
}
My game file is currently not communicating correctly, how would I fix it

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!