Mod 2: Synchronization Flashcards

1
Q

What are saved in “the stack”?

A

(1) Reference to an object
(2) Value type data

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

What are saved in “the Heap”?

A

The objects data

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

What is requried in order for processes and threads to be able to work together?

A

That they can communicate with each other

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

How can two threads communicate with each other?

A

(1) Shared variables or objects
(2) Message passing through method calls

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

Why are both ways of communication between threads cumbersome and not easy to program?

A

(1) Thread runs only within the time they receive from CPU
(2) Threads may not complete an operation before CPU switches to another thread

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

Why does the application lack control over how threads run?

A

Because operating system has its own scheduler and algorithms to give threads CPU time to run

This also means that it entierly out of the application control when switches between threads takes place

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

Describe context-switching

A

(1) a thread is allocated a pre-defined time period to execute on the process
(2) when the time expires the thread’s context is saved until next run
(3) processor begins to execute next thread

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

What does interleave mean?

A

That an operation can consist of multiple step

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

What is an atomic operation?

A

The opposite of interleaving, meaning an operation happens without interleaving = either it happens completely or not at all. It cannot stop in the middle.

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

What does it mean in practic that an operation is atomic?

A

It will not have any side effects until the action is complete.

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

What do we need to ensure to achieve an atomic operation?

A

That only one thread has access to block of code at a time

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

What can we use to ensure atomic operations?

A

Critical Section protocols

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

How is critical sections protected?

A

By applying mutual exclusion etc.

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

What is “thread interference”?

A

Condition which occur when more than one thread, executing simultaneously, access same piece of data

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

What can happen when more than one thread access the same data?

A

The data may get corrupted or a thread may not get the desired output

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

How can we avoid corrupt data?

A

By ensuring the code is thread safe

17
Q

What does it mean that code is “thread safe”?

A

Code that is safe to call by multiple threads

18
Q

What causes interference?

A

Interleaving

19
Q

How can we avoid interference?

A

(1) Declaring method as “syncronizhed” (in Java, locks in C#)
(2) Using thread-local variables
(3) Mutual Exclusion/Atomic operations

20
Q

What is “synchronization”?

A

Synchronizing threads access to prevent issues such as race condition, deadlock and starvation

21
Q

What is race condition?

A

The situation where shared resources are used my multiple threads and read/write occurs in the wrong order causing error output and corrupt data

22
Q

Give an example of a race condition

A

eg. while T1 are working with an open file, editing data, T2 may close it or T3 might open the file too

23
Q

What does an atomic operation do?

A

An atomic operation completes it task without interference from another tread.