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

[2021] Massey - 159.341 Programming Languages, Algorithms AND Concurrency - Final Exam - Q2 Synchronization

This question has been solved
Engage in a Conversation

Question 2 – Synchronisation [8 marks]

  CourseNana.COM

a) Below is a proposed solution to the Critical Section Problem (for 2 threads). [4 marks] CourseNana.COM

(Assume that two threads are launched with the id's 0 and 1) CourseNana.COM

  CourseNana.COM

int light[2]; light[0]=0; light[1]=0; // shared variables CourseNana.COM

int turn[2]; turn[0] =0; turn[1] =0; CourseNana.COM

void thread_function(int id) { // id is either 0 or 1 CourseNana.COM

int j = 1 - id; // j is id of other thread CourseNana.COM

while(true) { CourseNana.COM

lights[id] = 1; CourseNana.COM

while((lights[j] == 1) && (turn[id] == 0)) { CourseNana.COM

turn[j] = 1; // tell other thread to go first CourseNana.COM

} CourseNana.COM

// --- perform critical section --- CourseNana.COM

turn[id] = 0; CourseNana.COM

lights[id] = 0; CourseNana.COM

} CourseNana.COM

} CourseNana.COM

  CourseNana.COM

Is this solution correct? Either provide a justification of why it is correct or give an example of how it fails one of the three requirements for the critical section problem. CourseNana.COM

  CourseNana.COM

Note: Do not worry about memory barriers, assume that memory transactions are performed CourseNana.COM

in order and are immediately visible to the other thread. CourseNana.COM

  CourseNana.COM

b) Provide a solution to the Critical Section Problem by writing implementations of [2 marks] CourseNana.COM

lock() and unlock() using the atomic instruction represented by the following pseudo code (note this is implemented as a single hardware instruction CourseNana.COM

that executes atomically. CourseNana.COM

  CourseNana.COM

int atomic_instruction(int *a, int e, int v) { CourseNana.COM

int o = *a; CourseNana.COM

if(o == e) { CourseNana.COM

*a = v; CourseNana.COM

} CourseNana.COM

return o; CourseNana.COM

} CourseNana.COM

  CourseNana.COM

c) Describe what would happen if a semaphore intended to be used for mutual [2 marks] CourseNana.COM

exclusion was initialised to 2 instead of 1? 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,