Synchronization
16 Questions
1 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Define Mutual Exclusion

A region of code may only be executed by one single thread at a time.

Define Producer-Consumer

One thread produces something that another thread needs to use/consume. we can just use a flag to indicate to another thread that an event has occurred, and we don't necessarily need hardware support for that.

Define Barrier

All threads must finish before moving past this point.

Explain why the following lock implementation is faulty.

                    Thread 0                       Thread 1
time 0: lock: ld R1, &lockvar.       
time 1: bnz R1, lock.                     lock: ld R1, &lockvar
time 2: sti &lockvar, #1                  bnz R1, lock
time 3:                                   sti &lockvar, #1

<p>Both threads can acquire the lock here, because there is no atomicity.</p> Signup and view all the answers

Explain how a test-and-set instruction works.

<p>Read the value stored in memory location M, test the value against a constant, if they match, write the value in register Rx to the memory location M</p> Signup and view all the answers

Write code to implement a mutual exclusion lock using a test-and-set instruction:

<p>lock: t&amp;s R1, &amp;lockvar bnz R1, lock ret</p> <p>unlock: st &amp;lockvar, #0 ret</p> Signup and view all the answers

What are the four metrics used in evaluating lock implementations?

<p>Uncontended Lock Acquisition, Traffic, Fairness, Storage</p> Signup and view all the answers

Fill out the following table for the Test-and-Set lock: as time goes on (Init, t&s1, t&s2, t&s3, t&s2, unlock1, t&s2, t&s3, t&s3, unlock2, t&s3, unlock3) Include answer as an array of values <P1, P2, P3, Bus Request> So the init would be < -, -, -, - > and

<p>&lt; -, -, -, - &gt;, &lt;M, -, -, BusRdX&gt;, &lt;I, M, -, BusRdX&gt;, &lt;I, I, M, BusRdX&gt;, &lt;I, M, I, BusRdX&gt;, &lt;M, I, I, BusRdX&gt;, &lt;I, M, I, BusRdX&gt;, &lt;I, I, M, BusRdX&gt;, &lt;I, I, M,- &gt; &lt;I, M, I, BusRdX&gt; &lt;I, I, M, BusRdX&gt; &lt;I, I, M,- &gt;</p> Signup and view all the answers

Explain the Test and Test and Set Lock implementation. Explain why TTSL reduces traffic but increases uncontended lock acquisition latency.

<p>Used to reduce contention on the lock variable. Basically, instead of issuing t&amp;s instructions over and over again, we can check first to see if the lock variable seems to be available. If it is, then we can actually go ahead and do the t&amp;s instruction and try to acquire the lock.The load instruction and the following branch instruction form a tight loop that keeps reading (but not writing) the address where lockvar is located until the value in the location turns 0. Therefore, while a lock is being held by a processor, other processors do not execute the test-and-set atomic instructions. This prevents repeated invalidations that are useless as they lead to failed lock acquisitions. Only when the lock is released, will the processors attempt to acquire the lock using the atomic instructions. The uncontended lock-acquisition latency is higher than the test-and-set lock implementation due to the extra load and branch instructions. However, the traffic during the time a lock is held is significantly reduced. Only a small fraction of lock acquisition attempts generate traffic.</p> Signup and view all the answers

Write an implementation of a TTSL mutual exclusion lock:

<p>lock: ld R1, &amp;lockvar bnz R1, lock t&amp;s R1, &amp;lockvar bnz R1, lock ret</p> <p>unlock: st &amp;lockvar, #0 ret</p> Signup and view all the answers

Fill out the following table for the TTSL: as time goes on (Init, ld1, t&s1, ld2, ld3, ld2, unlock1, ld2, t&s2, ld3, ld3, unlock2, ld3, t&s3, unlock3) Include answer as an array of values <P1, P2, P3, Bus Request> So the init would be < -, -, -, - > and

<p>&lt; -, -, -, - &gt;, &lt;E, -, -, BusRd&gt;, &lt;M, -, -, - &gt;, &lt;S, S, -, BusRd &gt;, &lt;S, S, S, - &gt;, &lt;M, I, I, BusUpgr &gt;, &lt;S, S, I, BusRd &gt;, &lt;I, M, I, BusUpgr&gt;, &lt;I, S, S, BusRd&gt;, &lt;I, S, S, - &gt;, &lt;I, M, I, BusUpgr&gt;, &lt;I, S, S, BusRd&gt;, &lt;I, I, M, BusUpgr&gt;, &lt;I, I, M, - &gt;</p> Signup and view all the answers

What metric does a Ticket Lock and Array based lock improve on over other lock implementations?

<p>Fairness</p> Signup and view all the answers

Write code to implement ticket lock, and array based queue lock?

<p>ticket_lock_init(int *next_ticket, int *now_serving) { *next_ticket = 0; *now_serving = 0; }</p> <p>ticket_lock_acquire(int *now_serving, int *next_ticket) { my_ticket = fetch_and_incr(next_ticket); while(*now_serving != my_ticket) {} }</p> <p>ticket_lock_release(int *next_ticket, int *now_serving) { (*now_serving)++; }</p> <p>ABQL: ABQL_init(int *next_ticket, int *can_serve) { *next_ticket = 0; for (i = 1; i &lt; MAX_SIZE; i++) { can_serve[i] = 0; can_serve[0] = 1; } }</p> <p>ABQL_acquire(int *next_ticket, int *can_serve) { *my_ticket = fetch_and_incr(next_ticket); while(can_serve[*my_ticket] != 1) {} }</p> <p>ABQL_release(int *can_serve) { can_serve[*my_ticket + 1] = 1; can_serve[*my_ticket] = 0; }</p> Signup and view all the answers

Describe LL/SC.

<p>pair of instructions that give the illusion of atomicity A lock acquisition essentially consists of a load, some instructions like a branch, and then a store. Atomicity guarantees that either all three of these things happen, or none of them do. However, from the point of view of other processors, the only visible instruction is the store instruction. The load/read and branch are only visible to the local processor. So we can create the illusion by cancelling the store if something happens between the load and the store. Store Conditional : The execution of the store instruction is conditional upon a few things that would break the illusion of atomicity. If the address of the store matches the address written in the linked register mentioned below, then the store succeeds. Load Linked: A special load instruction that requires the monitoring of the validity of the block address. When it loads the block into a register, it also writes the address of the block into a special process register called a linked register. This linked register gets cleared when another processor does something that invalidates the block.</p> Signup and view all the answers

Fill out the following table for LL/SC: as time goes on (Init,ll1, sc1, ll2, ll3, ll2, unlock1, ll2, sc2, ll3, ll3, unlock2, ll3, sc3, unlock3) Include answer as an array of values <P1, P2, P3, Bus Request> So the init would be < -, -, -, - > and

<p>&lt; -, -, -, - &gt;, &lt;E, -, -, BusRd&gt;, &lt;M, -, -, - &gt;, &lt;S, S, -, BusRd &gt;, &lt;S, S, S, - &gt;, &lt;S, S, S, BusUpgr &gt;, &lt;M, I, I, BusUpgr &gt;, &lt;S, S, I, BusRd &gt;, &lt;I, M, I, BusUpgr&gt;, &lt;I, S, S, BusRd&gt;, &lt;I, S, S, - &gt;, &lt;I, M, I, BusUpgr&gt;, &lt;I, S, S, BusRd&gt;, &lt;I, I, M, BusUpgr&gt;, &lt;I, I, M, - &gt;</p> Signup and view all the answers

Do Problem 2 in HW2. True if you get answers correct, false otherwise.

<p>True</p> Signup and view all the answers

More Like This

Process Synchronization
16 questions

Process Synchronization

AppreciativeHamster avatar
AppreciativeHamster
Synchronization in IoT and Embedded Systems
24 questions
Use Quizgecko on...
Browser
Browser