Page 1 of 1

asignment Script: You are the organizer of a gaming exhibit at the E3 Electronic Entertainment Expo. You want to allow t

Posted: Sat May 14, 2022 6:38 pm
by answerhappygod
asignment Script:
You are the organizer of a gaming exhibit at the E3 Electronic Entertainment Expo. You want to allow the attendees to play your startup’s new game demo. You model the attendees as threads, called players, and your job is to synchronize access to a single copy of the game, as follows:
• When a player arrives, he or she waits in a waiting area.
• Once there are 4 or more players waiting to play, you allow exactly 4 of them to
leave the waiting area to begin playing. These four leave the waiting area and
approach the game console.
• When a player reaches the console, the player waits until all four players are at
the console, at which point all four players begin playing.
• Players may finish playing at any time. However, you cannot allow any new
players to begin playing until all four players have left.
• You do not need to let players out of the waiting area in the order in which they
arrived.
• You cannot assume that a player will ever finish playing.
• You decide to solve this synchronization problem using two custom synchronization primitives, which have “barrier-like” semantics:
GameBarrier gb;
ConsoleBarrier cb;
Your task is to implement these synchronization primitives according to the specifications listed below. Each player thread has access to the two global barriers, and uses them in the following sequence:
void Player(ThreadID tid, GameBarrier gb, ConsoleBarrier cb) { gb.waitToPlay();
cb.waitAtConsole(); play(); gb.donePlaying();
}
The GameBarrier can be in one of three states:
• GAME_NOTREADY: There are fewer than 4 players waiting to play. When the barrier
is in this state, no player can progress beyond waitToPlay().
• GAME_FILLING: There are (or were) at least 4 players waiting to play, and either we
are in the first turn or else all four players from the prior turn have departed (via donePlaying()). When the barrier is in this state, a player can progress beyond waitToPlay(), and in fact four players must progress beyond this function. When exactly four players have progressed beyond this function, the barrier enters the GAME_FILLED state.
• GAME_FILLED: Four players have been sent to the console, and the turn is not over (meaning that the departure of all four from the console via donePlaying() has not yet taken place). When the barrier is in this state, no waiting player can progress beyond waitToPlay().
The ConsoleBarrier can be in one of two states:
• CONSOLE_WAIT: Four players have not yet arrived at the console in the current round.
When the barrier is in this state, no player can progress beyond waitAtConsole().
• CONSOLE_ALLOW: Four players have arrived in the current round. When the barrier is in this
state, all four waiting players must progress beyond waitAtConsole(), after which the state reverts to CONSOLE_WAIT. Your barrier will require the use of condition variables. Recall that condition variables provide three methods:
Condition.wait(Lock mutex), Condition.signal(), and Condition.broadcast().
Locks provide two methods: Lock.acquire() and Lock.release(). Note that part (but not all) of your work is to ensure that the barriers make the correct state transitions. Below, where indicated, fill out the variables and methods for the GameBarrier and ConsoleBarrier objects.
public class GameBarrier {
private static final int GAME_NOTREADY = 0; private static final int GAME_FILLING = 1; private static final int GAME_FILLED = 2; private Lock mutex;
private int state;
private Condition cv;
/* SPECIFY ANY OTHER CLASS VARIABLES */ private int NumWaiters;
private int NumPlayers;
public GameBarrier(Lock lock) {
this.mutex = lock;
this.state = GAME_NOTREADY; this.cv = new Condition();
/* INITIALIZE ANY OTHER CLASS VARIABLES */ this.NumWaiters = 0;
this.NumPlayers = 0;
We deducted 1 pt each if your answer was missing either NumWaiters or NumPlayers
}
public void waitToPlay()
{
/* YOU MUST FILL IN THIS FUNCTION */
mutex.acquire();
NumWaiters++
if ((NumWaiters >= 4) &&
(state == GAME_NOTREADY)) {
state = GAME_FILLING;
cv.broadcast();
}
while (state != GAME_FILLING) { cv.wait(mutex);
}
NumWaiters--;
NumPlayers++;
if (NumPlayers == 4) state = GAME_FILLED;
mutex.release();
}
public void donePlaying() {
/* YOU MUST FILL IN THIS FUNCTION */
mutex.acquire(); NumPlayers--;
if (NumPlayers = 0) {
state = GAME_NOT_READY; if (NumWaiters >= 4) {
state = GAME_FILLING; cv.broadcast(mutex);
} }
} }
public class ConsoleBarrier {
private static final int CONSOLE_WAIT = 0; private static final int CONSOLE_ALLOW = 1; private Lock mutex;
private int state;
mutex.release();
private Condition cv;
/* SPECIFY ANY OTHER CLASS VARIABLES */ int NumPlayers;
public ConsoleBarrier(Lock lock) { this.mutex = lock;
this.state = CONSOLE_WAIT; this.cv = new Condition();
/* INITIALIZE ANY OTHER CLASS VARIABLES */ this.NumPlayers = 0;
We deducted 2 pts if your answer was missing NumPlayers }
public void waitAtConsole() {
/* YOU MUST FILL IN THIS FUNCTION */
mutex.acquire(); NumPlayers++
if (NumPlayers == 4) {
state = CONSOLE_ALLOW; cv.broadcast();
}
while (state == CONSOLE_WAIT) { cv.wait(mutex);
}
NumPlayers--;
if (NumPlayers == 0) state = CONSOLE_WAIT; mutex.release()