Lecture: threads & switching
preparation
- read OSPP §4, Concurrency and Threads
processes and threads
- locks recap
- mutual exclusion
- boils down to
xchg
on x86
- safety? progress? fairness?
- performance? scalability?
- threads and processes
- programming abstraction: virtual CPUs
- process API: fork/execve/exit/kill/wait/getpid
- thread API: pthread etc.
- definitions I
- process: address space, hosting one or more threads
- thread: basic execution and scheduling unit
- definitions II
- threads and processes are both tasks
- threads: tasks with shared address space
- example: Linux clone
- generalization: namespaces & containers
- questions
- T/F - writing to memory location x in process A
will change the value at the same memory location x in process B
- T/F - thread A creates a file descriptor; thread B can access it
a thread implementation
- thread = stack + virtual CPU registers
- registers
- SP: point to a separate stack
- PC: where to resume execution
- how/where are other registers saved
- scheduling state: running, runnable, killed, etc.
- cooperative scheduling: threads give up control by calling yield
- useful?
- downsides? how would you improve it?
- example: uthread implementation
- what’s needed for switching
- save
- do we need to save the PC
- save current thread’s registers on the stack
- save the SP - to where
- restore
- where to get the SP value
- restore saved registers
- what address to return to
- scheduler activations