Question 5 – Multithreaded Programming [15 marks]
Ken has written the following multi-threaded program written in C/C++ to simulate football
players and a groundskeeper using a football field.
#define NPLAYERS 50
semaphore sem_field;
int player_count;
void* player_thread(void *p) {
long id = (long)p;
while(true) {
player_count++;
if(player_count == 1) { // First player in field
wait(sem_field); // Wait for field semaphore
}
printf("Player %li: on the field.\n", id);
sleep(PLAY_TIME); // Practice football
player_count--;
if(player_count == 0) { // Last player leaving field
signal(sem_field); // Release field semaphore
}
sleep(REST_TIME); // Rest for a while
}
}
void* groundskeeper_thread(void *p) {
long id = (long)p;
while(true) {
wait(sem_field); // Wait for access to field
printf("Groundskeeper: mowing field\n");
sleep(CUT_TIME); // Cut grass on field
signal(sem_field); // Release access to field
sleep(GROUNDS_TIME); // Wait until grass needs cutting
}
}
int main() {
unsigned long i;
create(&sem_field, ??);
for(i = 0; i < NPLAYERS; i++) {
// Create Player Thread
create_thread(player_thread, (void*)i);
}
// Create 1 Groundskeeper Thread
create_thread(groundskeeper_thread, (void*)0);
// Wait for program to complete
Sleep(86400000ULL);
}
The program simulates a number of people playing football on a field and a groundskeeper who occasionally needs cut the grass. The groundskeeper can only cut the grass if there are no people playing football on the field so a semaphore (sem_field) is used to control access to the field.
Synchronisation is provided by semaphores with the type semaphore and three functions
create, signal and wait that initialise, signal and wait on a semaphore respectively.
A separate thread is created for each of the players and one for the groundskeeper.
Each Player uses the following algorithm:
add 1 to number of players
if first player on the field
lock the field
play football for a while
subtract 1 from the number of players
if last player leaving field
unlock the field
wait for a while
repeat
The Groundskeeper uses the following algorithm:
lock the field
cut the grass
unlock the field
wait for a while
repeat
There are several problems with the existing program.
a) Ken is unsure what value the semaphore sem_field should be initialised to. [3 marks]
Give the appropriate value Ken should initialise this semaphore to.
b) The program has a race condition, identify it and describe the issues may [3 marks]
arise if it is not fixed.
c) Describe a strategy for preventing the race condition you identified in [3 marks]
part b. Your strategy may make use of new semaphores but should not
fundamentally change the algorithms of the players or the groundskeeper.
d) Provide an implementation of the strategy you described in part c). [3 marks]
e) Another problem is that the players prevent the groundskeeper from cutting [3 marks]
the grass. If the players keep entering the field and it is never empty then the
STUDENT NAME:
STUDENT ID:
Page 8 of 8 COS
groundskeeper is never able to lock sem_field.
Describe a solution to this problem so that the groundskeeper gets preference
when it's time to cut the grass (players already on the field should be allowed to
finish).