Threads and Intro to Scheduling
Threads
- recognize that several programs can split CPU time while sharing
other resources (e.g. memory, files, I/O devices, etc.)
- in general, multiple threads can coexist in one process
- visualize as multiple traces of same process executing independently
- i.e. multiple PCs on same program text
- each thread has various attributes
- execution state (running, ready, etc.)
- thread context (processor state) -- saved when not running
- execution stack and local variables
- pointers to resources (memory, I/O buffers, etc.) shared
with other threads of same process
- process is responsible for managing CPU time sharing of threads
Priviledged Execution
- processes shouldn't be allowed to modify process management info
- could make all other processes suspended and monopolize CPU
- solution: restrict memory access to PCB addresses
- attempt to write to those addresses causes trap to OS
- but OS needs to modify PCB to manage procs, state transitions, etc.
- allowed in privledged execution mode
- use of CPU control features determined by
supervisor (SU) bit of PSW
- can't allow anybody to toggle
(eg. disable interrupts indefinitely)
- instead, bit is toggled by traps/interrupts
- SU bit toggled
- special code invoked: exception or interrupt handler
- code is outside of process's address space
- attempt to modify code would cause trap (seg fault)
Scheduling
- scheduler responsible for multiplexing CPU
- policy: WHEN to remove process from CPU
- mechanism: HOW to remove process and put on another
- scheduling mechanism involves:
- enqueuer - adds (newly made ready) processes to ready list
- computes priority for each process in list
- context switcher - removes process from CPU
- saves register contents in PCB
- context switch to dispatcher
- dispatcher - selects a ready process and performs context switch to it
- scheduling techniques:
- voluntary:
- active running process explicitly invokes switcher
- active process makes resource request
- involuntary:
- timed interrupt invokes switcher (preemption)
- voluntary scheduling:
- process p1 performs yield(r, s)
- yield saves next instruction address (available on stack)
at special memory location "r"
- OS uses PID to calculate: r = f(p1.PID)
- new address to load to PC determined from memory location "s"
- assume process p2 had earlier done a yield(s, r)
- then yield of p1 would result in p2 continuing...
- for more than two processes, "s" = address of dispatcher:
p1: runnning... yield (r, dispatcher)
dispatcher: calculate s, yield (dispatcher, s)
pi: invoked...
[note: p.159 fig 7.3 should replace p,q with r,s]
- problem: process may never yield, need involuntary preemption
- involuntary scheduling:
- interval timer generates interrupt
- interrupt handler invokes scheduler