P2L2 Threads and Concurrency Flashcards

1
Q

Give a metaphor for a thread: A thread is like ….

List 3 attributes:

  1. ….
  2. ….
  3. …..
A

.. a worker in a toy shop

  1. An active entity
  2. Works simultaneously
  3. Needs coordination
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain the difference between a thread and a process

A

A process has both address space and execution context information.

Threads represent multiple independent execution contexts within the same address space

address space (virtual <-> physical memory mapping)

  • code
  • data
  • heap

execution context

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

State 2 benefits of multithreading

A
  1. Threads can run in parallel since they are executing different instructions of the same code.
  2. Specialization: For instance, we can give higher priority to tasks that handle more important tasks or service higher paying customers.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the advantage of multi-thread over multi-process? Give 2 reasons.

A
  1. Each process needs its own address space.
    - -> more memory usage.
    - -> more memory swaps.
  2. Inter process communication (IPC) - is more costly than inter thread communication, which consists primarily of reading/writing shared variables.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Are threads useful on a single CPU? Explain why or why not.

A

Yes - to hide latency.

For example if a thread makes a disk request I/O - if it takes 2x as long as context switch then it’s a great deal. The thread is stuck anyway on a slow I/O request, so it pays to context switch to another thread who can continue working.

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