Week 3 - Mutual Exclusion Flashcards

1
Q

Critical Sections

A

Set of steps algorithim that must be executed to update a shared data strucuture

repeat
«critical section»
«do other work»
forever;

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

Race condition

A

outcome of two or more threads competing to modify shared data, where interference between threads results in erroneous results or a corrupted data structure.

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

Mutual exclusion

A

methods to ensure only one thread does a particular activity, such as executing their critical section, at a time. i.e., all other threads are excluded from doing that activity.

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

Atomicity

A

Mutual exclusion used to ensure that critical sections excute atomically.

An concurrent programming, code fragment is a piece of code that executes as a whole without interruption. Where no other code in other threads can interfere with execution.

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

Race Conditions are Evil

A

Noxious kind of programming error.

They occur non-deterministically, causing intermittent faults.

  • Program could be tested and never exhibit race condition.
  • When program does crash randomly, and nobody knows why.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Avoiding a Race Condtion

A

repeat
… do something to begin mutual exclusion…
«critical section»
… do something to end mutual exclusion…
«do normal work»
forever;

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

Attempt at Mutal Exclusion (LOCK)

A

New shared boolean, lock, initialized to false, specifies whether thread is in its critical section.

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

lock idea

A

when first thread is ready to enter critical section, its wait loop terminates immediately, lock is set to true, and critical section starts to execute.

if second thread wants critical section it will see lock is true, and wait loop iterates until the first thread leaves its critical section and sets lock back to false.

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

Attempt at turn

A

a new shared variable called turn specifies whose turn it is to enter cirital section next.

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

turn idea

A

turn is initalised to 0.

thread 0 is allowed to execute first cirital section.

  • if thread 1 tries to enter its own critical section before thread 0 has finished, it waits in loop doing nothing.

when thread 0 leaves critical sction, turn is set to 1.

  • if thread 1 was waiting in a loop, it then ends.

roles are now swapped, thread 0 cannot reenter the critical region until thread 1 has had its turn.

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

problem with turn idea

A

Thread 1 may be blocked forever.

Guarantees safety but not progress.

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

Algorthim interested

A

Two shared variables, interested [0] and interested [1]

they are boolean variables, either true or false.

when true interested[i] wants to enter critical section

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

interested idea

A

both interested[0] and interested[1] set to false intialization.

thread sets to interested variable when wants enter into ciritcal region.
- if other interested it has to wait in loop until thread is finished crirtical section.

leave crirtical section, unset interested variavle, so other thread can have acess.

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

interested problem

A

Doesnt establish mutual exlusion.

both threads now loop block forever.

guarntees safety, not progress.

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

Petersons Algorithim

A

Discovered until 1981.

Late for such a fundamental and simple looking algorithm.

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

Petersons Mutual Exclusion

A

Not useful in practise.

No easy generalization of more than two threads.

Relies on busy waiting, wasteful of CPU cycles.

17
Q

Petersons Mutual Exclusion solutions

A

Realistic solutions:
Parallel systems specialised atomic instructions.

Multitasking systems:
Support for synchronization into thread or process scheduling algorithims.

18
Q

Hardware Support: Test and Set

A

atomic instruction sometimes provided by hardware: Test and Set Lock (TSL).

Test and modify the content of a word atomically

19
Q

Mutual Exclusion With TSL

A

Using TSL, can do critical sections with the following approach.
Shared data: Boolean lock = false ; // initially

20
Q

Programming Abstractions

A

Parallel computers can use TSL and similar instructions to implement mutual exclusion and other kinds of synchronization.

Depends on busy waiting, and not appropriate in multi-tasking environments (because it wastes computer cycles).

Need higher-level abstractions for synchronization, that can be implemented either by low-level instructions like TSL, or OS scheduling algorithms.

21
Q

OS Support: Semaphores

A

semaphore S is an integer variable that may be accessed only using two operations P and V, with pseudocode essentially like atomic:

P(S) : S = S - 1; //decrease S

V(S) : S = S + 1;
// increase S

semaphore value must never go under 0.

22
Q

Evaluation of Semaphores

A

Semaphores provided one of the first high-level synchronization abstractions.

  • Can be implemented efficiently on multiprocessors or in multi-tasking operating systems.
23
Q

Java Synchronized Methods

A

Java and several other modern programming languages use a version of the monitor idea.

methods in a Java class can be declared to be synchronized

If two threads try to call synchronized methods on the same object at the same time, one blocks until the other has completed.

static methods: if two threads try to call synchronized methods in the same class, one blocks.

24
Q

How Python implement synchronization?

A

you can use Join() method

threading module provides a Lock class to deal with the race conditions. Lock is implemented using a Semaphore object provided by the Operating System.

Python’s threading module offers: RLocks (Reentrant Locks), Semaphores and Barriers.