Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Designing a game that utilizes the Readers - Writers synchronization problem can be an interesting and challenging task for you. As we have seen in
Designing a game that utilizes the ReadersWriters synchronization problem can be an interesting and challenging task for you. As we have seen in the last class, the ReadersWriters problem is often used to manage access to shared data where multiple readers can access the data simultaneously, but only one writer should have exclusive access to this shared resource.
The conceptual scenario of this game where the ReadersWriters synchronization problem could be applied is as follows:
Game Elements: You have tree elements in this game.
Dungeon Data Shared Resource:
o Represents the state of the dungeon, including room layouts, enemy positions, and treasure locations.
Players Readers:
o Multiple players explore the dungeon concurrently.
o Players can view the current state of the dungeon.
Game Master Writer:
o Controls the overall state of the dungeon.
o Modifies the dungeon layout, moves enemies, and updates treasure locations.
Implementation:
Readers Players:
o Players can read view the current state of the dungeon simultaneously.
o Multiple players can explore different parts of the dungeon concurrently without interfering with each other.
Writer Game Master:
o The Game Master has exclusive write access to the dungeon data.
o When the Game Master makes modifications eg changes the layout, moves enemies, updates treasures it must ensure that no player is reading the data at that moment.
ReadersWriters Synchronization:
o Use a mutex to protect access to the dungeon data the shared data
o When a player wants to view the dungeon state, it acquires the read lock.
o When the Game Master wants to modify the dungeon state, it acquires the write lock.
o Readers can access the dungeon data concurrently as long as no writer has the write lock.
o If a writer has the write lock, no readers or other writers can access the data.
Avoiding Starvation:
o Implement a fair solution to avoid starvation.
o Use condition variables to signal waiting readers or writers when the lock becomes available.
Your Task: you have to complete the missing part at hwc by adding the appropriate lock, unlock, condwait and condsignal to the code. I have put TODO to indicate that part is missing.
The output of the program of players can be as follows:
husam@istinye:~$ aout
Player starts reading. Readers: Writers:
Player explores the dungeon. Treasures: Enemies:
Player finishes reading. Readers: Writers:
Player starts reading. Readers: Writers:
Player explores the dungeon. Treasures: Enemies:
Player finishes reading. Readers: Writers:
Player starts reading. Readers: Writers:
Player explores the dungeon. Treasures: Enemies:
Player finishes reading. Readers: Writers:
Game Master starts writing. Readers: Writers:
Game Master updates the dungeon. Treasures: Enemies:
Game Master finishes writing. Readers: Writers:
Player starts reading. Readers: Writers:
Player explores the dungeon. Treasures: Enemies:
Player finishes reading. Readers: Writers:
Player starts reading. Readers: Writers:
Player explores the dungeon. Treasures: Enemies:
Player finishes reading. Readers: Writers:
Player starts reading. Readers: Writers:
Player explores the dungeon. Treasures: Enemies:
Player finishes reading. Readers: Writers:
Game Master starts writing. Readers: Writers:
Game Master updates the dungeon. Treasures: Enemies:
Game Master finishes writing. Readers: Writers:
Player starts reading. Readers: Writers:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started