Synchronization Flashcards

1
Q

What’s a “race condition”?

A

– Outcome is nondeterministic and depends on the timing of

uncontrollable, unsynchronized events

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How different architectures deal with consistency?

A

All are inconsistent (because we want performance), for example all have store-load reordering.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s mutual exclusion?

A
  • Threads execute an entire critical section one at a time

* Never simultaneously

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s fairness in context of synchronization?

A

– No starvation, namely, a thread that wants to do the critical section, would eventually succeed
– Nice to have: bounded waiting
– Nice to have: FIFO

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can we implement locks (they require a critical section by themselves)?

A
  1. Using SW only, no HW support
    • Possible (was once part of OS courses), but complex, error prone,
    wasteful, and nowadays completely irrelevant, because…
  2. Using HW support (all modern processors provide such support)
    • There are special ops that ensure mutual exclusions
    • Fairness aspects aren’t ensured
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the xchg function do in C?

A

Receives an address and a value,
stores value in address and return the old value in address.

in code:
int xchgl(int* addr, in val){
     int res = *addr;
     *addr = val;
     return res;
}

it does it atomically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the lock instruction?

A

It tells the processor that it’s not allowed to change the order of the locked instructions (as part of OOOE).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the keyword volatile do?

A

It means that the following assembly code can be ran by multiple cores/cpus and all data needs to be written to memory and not caches, to ensure memory consistency.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How the xchg function implemented?

A
xchg(volatile uint *addr, uint newval) {
   uint result;
   asm volatile("lock; xchgl %0, %1" :
   "+m" (*addr), "=a" (result) :
   "1" (newval) :
  "cc");
  // atomic[ result = *addr,
 // swap( *addr , newval ) ]
return result;

xchg instruction performs swapping between *addr and newval.

cc means we modify RFLAGS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In the kernel, on a uni-core, do we need to lock?

A

– Need to disable interrupts if handlers might access the same data
structures

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

On a multicore, do we care if other cores take interrupts?

A

– No, as if they want the lock, they will acquire regardless
– (In particular, within the interrupt handler)
(locks are per core)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

User space can’t disable interrupts, but…
Is there something equivalent to interrupts that we need to address
when considering synchronization?

A

Signals (we may want to block them for a while if the access the
same data structures as the regular program)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What’s the comare-and-swap (“CAS”) op?

A
Atomically, does: 
bool cas(int *p, int old_val, int new_val) {
     if( *p == old_val ) {
         *p = new_val;
          return true;
   } else {
      return false;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s the Test-and-set (“TAS”) op?

A
atomically does
bool tas(bool *b) {
      bool old_value = *b;
      *b = true;
      return old_value;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the fields of a semaphore?

A

• Value (integer)
– Nonnegative
=> counting the number of “resources” currently available
– Negative
=> counting the number of tasks waiting for a resource

• A queue of waiting tasks
– Waiting for the resource to become available
– When value is negative, |value| = queue.length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What operations does the semaphore provide? and how they are implemented?

A

Wait(semaphore)
– value = value - 1
– If( value >= 0 ) // it was >= 1 before we decreased it
• Task can continue to run (it has been assigned the resource)
– Else
• Place task in waiting queue
– A.k.a. P() or proben

Signal(semaphore)
– value += 1
– If( value <= 0 ) // it was <= -1 before we increased it
• Remove one of the tasks from the wait-queue
• Wake it (make it runnable)
– A.k.a. V() or verhogen

17
Q

What’s CREW (concurrent readers, exclusive writers)?

A

• Multiple tasks want to read/write the same data element

• For consistency, need to enforce the following rules
– No problem for several tasks to read simultaneously
– But when a task is writing, no other task is allowed to read or write

18
Q

What’s Amdahl’s law?

A

What’s the maximal expected speed when parallelizing?
– Let n be the number of threads
– Let s be the fraction of the algorithm that is strictly serial (0 <= s <= 1)
– Let T_n be the time it takes to run the algorithm with n threads
– Then, optimally,
Tn = T1 * (s + (1-s)/n)