W9 - Intro to Threads Flashcards
(16 cards)
Two parts of a traditional process with a single thread of execution
Sequential program execution stream
- this is the code executed as a sequence. includes state of CPU registers, stack, …
Protected resources
- main memory state, I/O state, …
Thread
A sequential execution stream within a process.
Is there protection between threads?
No. Threads share the process’s resources.
Multithreading
A single program made up of a number of different concurrent activites (threads of execution).
Creating process vs creating thread
New thread will use less memory, takes less time to create, and terminate faster.
Threads naturally share memory for communication, for processes the kernel needs to provide protection and mechanisms for communiation.
Benefits of multi threading
- Exploit multiple cores
- If one task blocks on I/O other tasks in the program should still be able to execute (increased responsiveness)
- Some problems are best structured as communicating tasks (gui should be responsive)
What state is shared by all threads in process
Content of memory (global variables, heap)
I/O state (file descriptors, network connections)
How are threads useful in a web server as an example
Threads are created to process requests and issue responses. In the meanwhile the server continues to listen for other requests. Other threads to read data, access DB, etc, can also exist.
Why choose multi threading over multiple processes
Context switch expensive compared to thread switch.
Thread creation/termination is faster than process creation/termination.
Threads naturally share memory
What does our single threaded process model look like?
Process control block
User address space
User stack + kernel stack (just this one pair)
What does our multi threaded process model look like?
Process control block
User address space (shared between all threads)
Multiple threads, each made up of:
- Thread control block
- User stack + Kernel stack
What state remains private to each thread (typically kept in kernel memory)
TCB (Thread control block)
Thread ID
CPU registers (including PC)
Execution stack pointer
Scheduling info (e.g. priority)
Pointer to the PCB
Reminder: What’s a PCB
Process Control Block is a data structure used by the OS to keep track of processes
It has:
- PID
- Process State
- PC
- CPU Registers
- Mem management info
- Scheduling info
- I/O Status
- If multithreaded, thread list or TCBs.
Benefits of using processes over threads
Much simpler, since theres no uncontrolled access to shared variables and you can avoid all sorts of errors.
Thread Control Block
Private info individual to the thread stored here.
- TID
- CPU registers (including PC)
- Execution stack ptr
- Scheduling info
- Pointer to PCB