1. Homepage
  2. Exam
  3. [2021] Massey - 159.341 Programming Languages, Algorithms AND Concurrency - Final Exam - Q5 Multithreaded Programming

[2021] Massey - 159.341 Programming Languages, Algorithms AND Concurrency - Final Exam - Q5 Multithreaded Programming

This question has been solved
Engage in a Conversation

Question 5 – Multithreaded Programming [15 marks] CourseNana.COM

  CourseNana.COM

Ken has written the following multi-threaded program written in C/C++ to simulate football CourseNana.COM

players and a groundskeeper using a football field. CourseNana.COM

  CourseNana.COM

#define NPLAYERS 50 CourseNana.COM

semaphore sem_field; CourseNana.COM

int player_count; CourseNana.COM

void* player_thread(void *p) { CourseNana.COM

long id = (long)p; CourseNana.COM

while(true) { CourseNana.COM

player_count++; CourseNana.COM

if(player_count == 1) { // First player in field CourseNana.COM

wait(sem_field); // Wait for field semaphore CourseNana.COM

} CourseNana.COM

printf("Player %li: on the field.\n", id); CourseNana.COM

sleep(PLAY_TIME); // Practice football CourseNana.COM

player_count--; CourseNana.COM

if(player_count == 0) { // Last player leaving field CourseNana.COM

signal(sem_field); // Release field semaphore CourseNana.COM

} CourseNana.COM

sleep(REST_TIME); // Rest for a while CourseNana.COM

} CourseNana.COM

} CourseNana.COM

  CourseNana.COM

void* groundskeeper_thread(void *p) { CourseNana.COM

long id = (long)p; CourseNana.COM

while(true) { CourseNana.COM

wait(sem_field); // Wait for access to field CourseNana.COM

printf("Groundskeeper: mowing field\n"); CourseNana.COM

sleep(CUT_TIME); // Cut grass on field CourseNana.COM

signal(sem_field); // Release access to field CourseNana.COM

sleep(GROUNDS_TIME); // Wait until grass needs cutting CourseNana.COM

} CourseNana.COM

} CourseNana.COM

int main() { CourseNana.COM

unsigned long i; CourseNana.COM

create(&sem_field, ??); CourseNana.COM

for(i = 0; i < NPLAYERS; i++) { CourseNana.COM

// Create Player Thread CourseNana.COM

create_thread(player_thread, (void*)i); CourseNana.COM

} CourseNana.COM

  CourseNana.COM

// Create 1 Groundskeeper Thread CourseNana.COM

create_thread(groundskeeper_thread, (void*)0); CourseNana.COM

// Wait for program to complete CourseNana.COM

Sleep(86400000ULL); CourseNana.COM

} CourseNana.COM

  CourseNana.COM

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. CourseNana.COM

  CourseNana.COM

Synchronisation is provided by semaphores with the type semaphore and three functions CourseNana.COM

create, signal and wait that initialise, signal and wait on a semaphore respectively. CourseNana.COM

A separate thread is created for each of the players and one for the groundskeeper. CourseNana.COM

  CourseNana.COM

Each Player uses the following algorithm: CourseNana.COM

add 1 to number of players CourseNana.COM

if first player on the field CourseNana.COM

lock the field CourseNana.COM

play football for a while CourseNana.COM

subtract 1 from the number of players CourseNana.COM

if last player leaving field CourseNana.COM

unlock the field CourseNana.COM

wait for a while CourseNana.COM

repeat CourseNana.COM

  CourseNana.COM

The Groundskeeper uses the following algorithm: CourseNana.COM

lock the field CourseNana.COM

cut the grass CourseNana.COM

unlock the field CourseNana.COM

wait for a while CourseNana.COM

repeat CourseNana.COM

  CourseNana.COM

There are several problems with the existing program. CourseNana.COM

  CourseNana.COM

a) Ken is unsure what value the semaphore sem_field should be initialised to. [3 marks] CourseNana.COM

Give the appropriate value Ken should initialise this semaphore to. CourseNana.COM

  CourseNana.COM

b) The program has a race condition, identify it and describe the issues may [3 marks] CourseNana.COM

arise if it is not fixed. CourseNana.COM

  CourseNana.COM

c) Describe a strategy for preventing the race condition you identified in [3 marks] CourseNana.COM

part b. Your strategy may make use of new semaphores but should not CourseNana.COM

fundamentally change the algorithms of the players or the groundskeeper. CourseNana.COM

  CourseNana.COM

d) Provide an implementation of the strategy you described in part c). [3 marks] CourseNana.COM

  CourseNana.COM

e) Another problem is that the players prevent the groundskeeper from cutting [3 marks] CourseNana.COM

the grass. If the players keep entering the field and it is never empty then the CourseNana.COM

STUDENT NAME: CourseNana.COM

STUDENT ID: CourseNana.COM

Page 8 of 8 COS CourseNana.COM

groundskeeper is never able to lock sem_field. CourseNana.COM

  CourseNana.COM

Describe a solution to this problem so that the groundskeeper gets preference CourseNana.COM

when it's time to cut the grass (players already on the field should be allowed to CourseNana.COM

finish). CourseNana.COM

Get the Solution to This Question

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
Messey University代写,159.341代写,Programming Languages代写, Algorithms AND Concurrency代写,Messey University代编,159.341代编,Programming Languages代编, Algorithms AND Concurrency代编,Messey University代考,159.341代考,Programming Languages代考, Algorithms AND Concurrency代考,Messey Universityhelp,159.341help,Programming Languageshelp, Algorithms AND Concurrencyhelp,Messey University作业代写,159.341作业代写,Programming Languages作业代写, Algorithms AND Concurrency作业代写,Messey University编程代写,159.341编程代写,Programming Languages编程代写, Algorithms AND Concurrency编程代写,Messey Universityprogramming help,159.341programming help,Programming Languagesprogramming help, Algorithms AND Concurrencyprogramming help,Messey Universityassignment help,159.341assignment help,Programming Languagesassignment help, Algorithms AND Concurrencyassignment help,Messey Universitysolution,159.341solution,Programming Languagessolution, Algorithms AND Concurrencysolution,