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 "dungeoninfo.h
#include
#include
#include
int main
Open shared memory
int shmfd shmopendungeonshmname, OCREAT ORDWR;
if shmfd
perrorFailed to open shared memory";
return ;
Truncate
if ftruncateshmfd sizeofstruct Dungeon
perrorFailed to truncate shared memory";
closeshmfd;
return ;
Map dungeon
struct Dungeon dungeon mmapNULL sizeofstruct Dungeon PROTREAD PROTWRITE, MAPSHARED, shmfd;
if dungeon MAPFAILED
perrorFailed to map shared memory";
closeshmfd;
return ;
Initialize dungeon
dungeonrunning true;
Initialize pid threads
pidt barbarianpid, wizardpid, roguepid;
Forks for all the characters
if barbarianpid fork
execlbarbarian "barbarian", char NULL;
perrorFailed to exec barbarian";
exit;
if wizardpid fork
execlwizard "wizard", char NULL;
perrorFailed to exec wizard";
exit;
if roguepid fork
execlrogue "rogue", char NULL;
perrorFailed to exec rogue";
exit;
RunDungeon call
RunDungeonwizardpid, roguepid, barbarianpid;
Wait for all children to exit
int status;
while wait&status;
Clean up shared memory and close
munmapdungeon sizeofstruct Dungeon;
closeshmfd;
shmunlinkdungeonshmname;
return ;
barbarian.c file looks like this:
#include
#include
#include
#include
#include
#include
#include
#include "dungeoninfo.h
GLOBAL DUNGEON variable
struct Dungeon dungeon;
void barbarianattackint sig
if sig DUNGEONSIGNAL
Copy the enemy's health to the attack field when the signal is received
dungeonbarbarian.attack dungeonenemy.health;
printfBarbarian attacks with power: d
dungeonbarbarian.attack;
Sleep for the defined duration to simulate attack delay
sleepSECONDSTOATTACK;
After the sleep, check if the attack value still matches the enemy's health
if dungeonbarbarian.attack dungeonenemy.health
printfAttack successful!
;
else
printfAttack failed or enemy health changed!
;
int main
Shared memory
int shmfd shmopendungeonshmname, ORDWR;
if shmfd
perrorFailed to open shared memory";
exit;
dungeon mmapNULL sizeofstruct Dungeon PROTREAD PROTWRITE, MAPSHARED, shmfd;
Check if I fuck up the shared memory map
if dungeon MAPFAILED
perrorFailed to map shared memory";
closeshmfd;
exit;
signalDUNGEONSIGNAL, barbarianattack;
while dungeonrunning
pause; Wait for the signal to attack
Clean up and close
munmapdungeon sizeofstruct Dungeon;
closeshmfd;
return ;
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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
