2014 15 2ND Sem
2014 15 2ND Sem
Problem Description: Read the following specification which explains HEADS & TAILS which is a multi player game.
A. Description of how the game is played: In this game there are two primary entities involved (a) Monitor and (b) Player. There
is just one Monitor and there can be multiple Players. Each Player tosses a coin only once and communicates the result (Head =
1, Tail = 0) to the Monitor. The Monitor keeps the count of number of heads and number of tails. Every time the Monitor
receives the result from a player it inspects the result and increment number of heads or tails by one. Having received the results
from all the players the Monitor publishes the final results. writes the final results (Number of heads = X, Number of tails = Y)
in an output file named out.txt.
B. Description of Concurrent Tasks: Both the Monitor and Players run as separate threads. Given below is the description of
both the threads.
WE WANT A STRICT SCHEDULE: Player Monitor Player Monitor Player Monitor ... until number of players
Partial code is given to you. Please write code only for boxed questions.
1. class Data: This class represents shared data object. The Player and Monitor threads are going to use the object of this class for
the purpose of communication. The meaning of instance variable lock: Object is self-explanatory.
public Data(){
Q.1 Initialize the first four state variables by reading data from a
file named "input.txt". See Fig.1 for file format.
}
// ALL GETTER AND SETTER METHODS CAN BE ASSUMED
}
2. class Monitor: This class encapsulates the logic of Monitor thread. Provide implementation for the boxed questions i.e. Q.2 (a),
Q.2. (b) and Q.2. (c).
Q.2 (c) specify condition for which monitor should wait and
let the monitor wait for this condition to be satisfied
}
}
Q.2 (e) Write the results i.e., number of heads
and number of tails to a file named "out.txt"
} // end of run method
}// end of class definition
3. class Player: This class encapsulates the logic of Player thread. Provide implementation for the boxed questions i.e. Q.3 (a),
Q.3. (b) and Q.3. (c).
}
}