Parallel Distributed Computing
Parallel Distributed Computing
Introduction to threads
Basic idea
We build virtual processors in software, on top of physical processors:
Processor: Provides a set of instructions along with the capability of
automatically executing a series of those instructions.
Thread: A minimal software processor in whose context a series of
instructions can be executed. Saving a thread context implies
stopping the current execution and saving all the data needed
to continue the execution at a later stage.
Process: A software processor in whose context one or more threads
may be executed. Executing a thread, means executing a
series of instructions in the context of that thread.
Introduction to threads
Processes Threads
Context switching
Contexts
• Processor context: The minimal collection of values stored in the
registers of a processor used for the execution of a series of instructions
(e.g., stack pointer, addressing registers, program counter).
Introduction to threads
Processes Threads
Context switching
Contexts
• Processor context: The minimal collection of values stored in the
registers of a processor used for the execution of a series of instructions
(e.g., stack pointer, addressing registers, program counter).
• Thread context: The minimal collection of values stored in registers and
memory, used for the execution of a series of instructions (i.e., processor
context, state).
Introduction to threads
Processes Threads
Context switching
Contexts
• Processor context: The minimal collection of values stored in the
registers of a processor used for the execution of a series of instructions
(e.g., stack pointer, addressing registers, program counter).
• Thread context: The minimal collection of values stored in registers and
memory, used for the execution of a series of instructions (i.e., processor
context, state).
• Process context: The minimal collection of values stored in registers and
memory, used for the execution of a thread (i.e., thread context, but now
also at least MMU register values).
Introduction to threads
Processes Threads
Context switching
Observations
1. Threads share the same address space. Thread context switching can be
done entirely independent of the operating system.
2. Process switching is generally (somewhat) more expensive as it involves
getting the OS in the loop, i.e., trapping to the kernel.
3. Creating and destroying threads is much cheaper than doing so for
processes.
Introduction to threads
Processes Threads
Introduction to threads
Processes Threads
Trade-offs
• Threads use the same address space: more prone to errors
• No support from OS/HW to protect threads using each other’s memory
• Thread context switching may be faster than process context switching
Introduction to threads
Processes Threads
Introduction to threads
Processes Threads
Introduction to threads
Processes Threads
Introduction to threads