part3 Flashcards

1
Q

What is synchronisation?

A

The limitation of possible program traces by coordinating execution such that certain conditions are satisfied.

E.g., certain invariant is satisfied and/or execution has some desired property

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

What is an invariant?

A

an assertion that holds at all control points, such as mutual exclusion being maintained or y being less than or equal to x

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

What does action synchronisation rely on?

A

Action counting and invariants on the counting

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

If A is an action in a program, what does cA denote?

A

The number of completed executions of A

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

What is a topology invariant?

A

An invariant derived directly from the program text

E.g., two actions always occuring one after the other

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

What two things is action synchronisation specified by? What are such inequalities referred to as?

A

Inequalities on:

  1. Action counts
  2. Program variables directly related to this counting

Synchronisation conditions or synchronisation invariants

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

What does P(s) refer to? How about V(s)?

A

P(s): <await(s>0)>; s=s-1;

V(s): <s = s + 1>

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

What are the primary two semaphore invariants? What invariant arises from these two?

A

S0: S >= 0
S1: s = s0 + cV(s) - cP(s)

S2: cP(s) <= s0 + cV(s)

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

What is the progress property of semaphores?

A

Blocking is only allowed if the safety properties would be violated

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

What is the action synchronisation solution in general? Specifically, give a collections of tasks/threads executing actions A,B,C,D and the synchronisation:

SYNC: a * cA + c * cC <= b * cB + d * cD + e for non-negative constants a,b,c,d,e

A

Introduce a semaphore s, s0 = e and replace:

A -> P(s)^a; A
C -> P(s)^c; C
B -> B; V(s)^b
D -> D; V(s)^d

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

What does sem_t *sem declare?

A

A pointer to a semaphore object, which will be used for synchronization.

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

What function is used to open and create a named semaphore?

A

sem_open(name, flags, mode, init-val)

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

In sem_open, what does the name parameter represent? What does the flags parameter in sem_open determine? What is the purpose of the mode parameter in sem_open? What does the init-val parameter in sem_open define?

A

A system-wide unique name for the semaphore.

The operation to be performed.

To specify the file permissions.

The initial value of the semaphore.

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

What function is used to close a semaphore in the current process while keeping it accessible to others?

A

sem_close(sem)

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

Which function removes a named semaphore from the system? When is a named semaphore fully removed from the system?

A

sem_unlink(name)

When no process is using it.

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

What does sem_init(sem, pshared, init-val) do? In sem_init, what does the pshared parameter indicate?

A

Initializes an unnamed semaphore.

Whether the semaphore is shared across processes or just threads within a process.

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

What function is used to destroy an unnamed semaphore? What happens if you use a semaphore after it has been destroyed with sem_destroy?

A

sem_destroy(sem)

It must not be used, as its resources are released.

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

What does sem_wait(sem) do?

A

Attempts to decrement (lock) the semaphore; if the semaphore value is greater than zero, it decrements it. If the value is zero, it blocks until the semaphore becomes available.

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

How does sem_trywait(sem) differ from sem_wait(sem)?

A

sem_trywait does not block; it attempts to decrement/lock the semaphore and returns an error if not possible.

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

What is the purpose of sem_post(sem)?

A

To increment the semaphore and wake one waiting thread if any are blocked.

21
Q

How can you retrieve the current value of a semaphore? In sem_getvalue, what does a negative semaphore value represent?

A

By using sem_getvalue(sem, &val).

The number of waiters (the length of the waiting queue).

22
Q

When is busy-waiting accepted? What is the condition for busy-waiting to work as expected?

A

When:

  1. Waiting is guaranteed to be short in comparison to the cost of context switching
  2. There is nothing else to do

The tests in the while and locking the mutex are executed atomically

23
Q

What is a deadlock state? How is it typically proven that a deadlock does NOT exist?

A

A system state in which a set of tasks is blocked indefinitely

Proof by contradiction; Assume deadlock occurs, investigate all task sets that can be blocked at the same time, show a contradiction;

24
Q

What are the three main ways of preventing deadlocks?

A

Always let critical sections terminate
- always call unlock(m) after lock(m)

Avoid cyclic waiting
- avoid P or lock operations that may block indefinitely between lock(m) and unlock(m)
- use a fixed order when calling P-operations on semaphores or mutexes

Avoid greedy consumers
- P(a)k should be an indivisible atomic operation when tasks compete for limited resources

25
Q

Give an example of cyclic waiting for three tasks T1, T2, T3 with three mutexes m1, m2, m3

A

T1 blocked on P(m2), T2 blocked on P(m3), T3 blocked on P(m1), leading to deadlock

26
Q

What does the invariant of condition synchronisation refer to? What does it require in terms of different processes?

A

A combination of execution state

Explicit communication (signalling) between processes

27
Q

What are the two principles of condition synchronisation?

A

Checking and blocking where a condition may be violated

Signalling waiters where a condition may have become true

28
Q

What does var cv: Condition represent in condition synchronization?

A

A condition, often associated with a Boolean expression in terms of program variables.

29
Q

What are the four basic operations on a condition variable (cv)?

A

wait, signal, sigall, and empty.

30
Q

What does the wait(…, cv) operation do?

A

Suspends the execution of the caller.

31
Q

What does the signal(cv) operation do?

A

Frees one process/thread suspended on wait(cv); does nothing if there are no suspended processes.

32
Q

What is the effect of the sigall(cv) operation?

A

Frees all processes suspended on wait(cv).

33
Q

What does the empty(cv) operation check?

A

Whether there are no processes suspended on wait(cv), returning true or false.

34
Q

What does pthread_cond_t cond = PTHREAD_COND_INITIALIZER; do?

A

Statically initializes the condition variable cond.

35
Q

What is the purpose of pthread_cond_init(&cond, attr);?

A

Dynamically initializes cond with optional attributes attr (or NULL for defaults).

36
Q

What does pthread_cond_destroy(&cond); do?

A

Destroys the condition variable cond, freeing its resources.

37
Q

How does pthread_cond_wait(&cond, m); work?

A

Blocks the calling thread until cond is signaled, releasing and reacquiring the mutex m.

38
Q

How is pthread_cond_timedwait(&cond, m, exp); different from pthread_cond_wait?

A

It unblocks after the time exp if no signal is received, returning ETIMEDOUT.

39
Q

What is the purpose of pthread_cond_signal(&cond);?

A

Wakes up one thread waiting on cond.

40
Q

What does pthread_cond_broadcast(&cond); do?

A

Wakes up all threads waiting on cond.

41
Q

What is a monitor? What does the data record? Under what condition are the operations of the monitor executed?

A

An object that contains a combination of data and operations on that data

The state of the monitor

Mutual exclusion, i.e., only one task can be inside the monitor at any one time

42
Q

What three things does a monitor do?

A

Encapsulate relevant shared variables

Provide well-defined operations on shared variables

Provide exclusion

43
Q

What are the four signalling disciplines?

A

Signal and exit

Signal and continue

Signal and wait

Signal and urgent wait

44
Q

How does signal and exit work? Is sigall supported? Must the condition hold after the signal?

A

The task executing a signal exits the monitor immediately, and monitor is given to a waiter on that variable, if any.

No

Yes

45
Q

How does signal and continue work? Must the condition hold after the signal?

A

Task executing a signal continues to use that monitor until a wait or until the end of the routine. Any task released by signal competes with the other tasks to obtain monitor access again.

No

46
Q

How does signal and wait work? Must the condition hold after the signal?

A

The task executing a signal is stopped in favour of the signalled process. Signaller waits to access the monitor after the signal and competes with all other tasks. Similar to signal and exit.

Yes

47
Q

How does signal and urgent wait work? Must the condition hold after the signal?

A

Task executing a signal is stopped in favour of the signalled process. Signaller waits to access the monitor again after the signal but does not compete with all other tasks again, as it has priority over them.

Yes

48
Q

What is the minimum number of semaphores required to enforce 5 synchronization conditions involving 2 global variables and 9 local variables manipulated in 15 threads? Assume we only use action synchronization.

A

5

49
Q

What is the Dining Philosopher Problem?

A

A synchronization problem where philosophers share forks to eat. They must acquire two forks but risk deadlock (all waiting), starvation (some never eat), or conflicts over resources.

Use mutexes, semaphores, or resource acquisition strategies to avoid deadlock and starvation.