Summer 2005-2006
receiver just decrements the value of the token by one and passes it to the next person
(player 2) and so-on. Otherwise, if the value of token was zero the receiver collects a
point and passes a new token with initial value of
Tok_value
to the next player. The
first player to collect
Pt
points wins and then kills all the others.
For example:
If
N = 9, Tok_value = 4
and
Pt = 2
, the players are numbered 0
to 8. Player 4 gets the first point then player 0 gains a point and so on. In this example
players gain points in the following order
4, 0, 5, 1, 6, 2, 7, 3, 8, 4
and
4 wins because it is the first one to get
Pt=2
points.
Write a program to simulate the above game. You program should get the inputs
N,
Tok_value
and
Pt
from the user. It then spawns
N
processes and then links the last
process to the first. The linking is being done through unnamed pipes (i.e. there should be
a pipe between two consecutive persons). The ith process represents the ith person in the
circle. Note that the first process has to generate two pipes, one to communicate with the
second process and one with the last process. The token is passed using UNIX unnamed
pipes. The winner kills the losers by sending the signal SIGTERM along the circle.
(Refer to
kill()
system call for this). Before each loser is killed, he has to close the
pipes which he uses (i.e., you have to write a signal handler for SIGTERM).
You must output the progress of the game. E.g. when a receiver gets a token it prints
something like the following: -
I am player number <X>. I have received a token with value
<Y> and I am now sending token to player <Z>. My current
points are <W>.
If a player wins it prints:
I am player number <X>. I have <Y> points.
I have won!!!
Each player that is killed prints before exiting:
I am player number <X>. I
have been killed.
PART II:
THREADS
Marks: 50
In this part you will implement the following problem using Pthreads.
Given two matrices A and B, where A is a matrix with
M
rows and
K
columns and matrix
B contains
K
rows and
N
columns, the matrix product of A and B is matrix C, where C
contains
M
rows and
N
columns. The entry in matrix C for row
i
, column
j
, (
C
i,j
) is the
sum of products of the elements for row
i
in matrix A and column
j
in matrix B.
In this part you will calculate each element
C
i,j
in a separate worker thread. This will
involve creating
MxN
worker threads. The main or parent thread will initialize the
matrices A and B and allocate sufficient memory for matrix C, which will hold the
product of matrices A and B. These matrices will be declared as global data and every
thread will have access to A, B and C. The matrices will be populated by reading in
values from a file. For example, the first two lines of the file will have the number of
rows and column values respectively followed by the matrix data.