Scheduler activations Landon Cox March 8 2016 What
- Slides: 49
Scheduler activations Landon Cox March 8, 2016
What is a process? • Informal • A program in execution • Running code + things it can read/write • Process ≠ program • Formal • ≥ 1 threads in their own address space
Threads that aren’t running • What is a non-running thread? • thread=“sequence of executing instructions” • non-running thread=“paused execution” • Must save thread’s private state • To re-run, re-load private state • Want thread to start where it left off
Thread control block (TCB) • What needs to access threads’ private data? • The CPU • This info is stored in the PC, SP, other registers • The OS needs pointers to non-running threads’ data • Thread control block (TCB) • Container for non-running threads’ private data • Values of PC, code, SP, stack, registers
Thread control block Address Space TCB 1 TCB 2 TCB 3 PC PC SP SP registers Code Stack PC Ready queue SP Thread 1 running PC SP registers CPU
Thread control block Address Space Ready queue TCB 2 TCB 3 PC PC SP SP registers Stack Code Stack Thread 1 running PC SP registers CPU
Switching threads • What needs to happen to switch threads? 1. Thread returns control to scheduler • For example, via timer interrupt 2. Scheduler chooses next thread to run 3. Scheduler saves state of current thread • To its thread control block 4. Scheduler loads context of next thread • From its thread control block 5. Jump to PC in next thread’s TCB
Kernel vs user threads • Kernel threads • Scheduler and queues reside in the kernel • User threads • Scheduler and queues reside in user space
32 -bit address space 4 GB Kernel data (same for all processes) 3 GB (0 xc 0000000) User data (different for every process) 0 GB Virtual memory
Switching threads 4 GB 3 GB Where is the interrupt handler code? In the kernel 0 GB Virtual memory
Switching threads 4 GB SP PC Interrupthandler code 3 GB On which stack is the IRQ handled? A kernel stack 0 GB Virtual memory
Switching threads 4 GB SP PC Interrupthandler code 3 GB Where are threads’ stacks? In user space 0 GB Virtual memory
Switching threads 4 GB SP PC Interrupthandler code 3 GB Where are threads’ stacks? In user space 0 GB Virtual memory
Switching threads 4 GB SP PC Interrupthandler code 3 GB So far, everything is the same for user-level and kernel threads 0 GB Virtual memory
Kernel threads Scheduler code For kernel threads, the thread scheduler and TCB queues are in the kernel Handler Interruptcode handler code TCB TCB
Kernel threads Scheduler code Where is the lock/cv code? Handler Interruptcode handler code TCB TCB
Kernel threads Where is the lock/cv code? Also, in the kernel Handler Interruptcode handler code Scheduler code Synchron. code TCB TCB
User-level threads For user-level threads, the thread scheduler and queues are in user space Handler Interruptcode handler code TCB TCB Scheduler code
User-level threads Where is the lock/cv code? Handler Interruptcode handler code Also, in the user space TCB TCB Scheduler code Synchron. code
Switching to a new user-level thread
User-level threads SP Handler Interruptcode handler code PC TCB Thread running user code TCB Program code Scheduler code Synchron. code
User-level threads SP PC Handler Interruptcode handler code Timer interrupt! TCB Program code Scheduler code Synchron. code
User-level threads SP PC Handler Interruptcode handler code Does the kernel know where user left off? Yes, CPU stores this on kernel stack TCB Program code Scheduler code Synchron. code
User-level threads SP PC Handler Interruptcode handler code What does kernel do next? Depends on meaning of the timer … TCB Program code Scheduler code Synchron. code
User-level threads SP PC Handler Interruptcode handler code Assuming it’s a signal for the process? Kernel delivers signal to process TCB Program code Scheduler code Synchron. code
User-level threads SP PC To deliver a signal to user code: make it look like a forced function call to signal handler. Handler Interruptcode handler code TCB Program code Scheduler code Synchron. code
User-level threads SP PC Handler Interruptcode handler code Where is the process’s timer signal handler? In scheduler code (e. g. , yield) TCB Program code Scheduler code Synchron. code
User-level threads SP PC On what stack should the scheduler code run? Stack of interrupted thread Handler Interruptcode handler code TCB yield Program code Scheduler code Synchron. code
User-level threads PC SP On what stack should the scheduler code run? Stack of interrupted thread Handler Interruptcode handler code TCB yield Program code Scheduler code Synchron. code
User-level threads PC SP Could the kernel transfer control to any other thread? No, it’s only aware of interrupted one Handler Interruptcode handler code TCB yield Program code Scheduler code Synchron. code
Example user-thread library.
Switching to a new kernel thread
Kernel threads SP Handler Interruptcode handler code PC Thread running user code Scheduler code Synchron. code TCB TCB Program code
Kernel threads SP PC Handler Interruptcode handler code Scheduler code Synchron. code TCB TCB Timer interrupt! Program code
Kernel threads SP PC Handler Interruptcode handler code Scheduler code Synchron. code TCB TCB Which thread could run next? Any thread. Kernel aware of all of them Program code
Advantages of user-level threads • Synchronization primitives are just a function call • Why are kernel threads more expensive? • Accessing thread management must cross kernel boundary • CPU protection checks, argument checks, handler dispatch, etc. • Switching back to thread also crosses kernel boundary • For user threads, easy to tune scheduling policy to application • Why is this harder for kernel threads? • All processes share the same scheduler • Per-process policies require wider, more complex API
Advantage of kernel threads SP PC Handler Interruptcode handler code Which thread could run next? Only the one that was interrupted TCB Program code Scheduler code Synchron. code
Advantage of kernel threads SP PC Handler Interruptcode handler code Why might we be unable to run the interrupted thread? Thread could have made a blocking system call TCB Program code Scheduler code Synchron. code
Advantage of kernel threads SP PC Handler Interruptcode handler code TCB Program code Scheduler code Synchron. code
Advantage of kernel threads SP PC Handler Interruptcode handler code Why is this wasteful? There are other threads that could run! Kernel doesn’t know they exist TCB Program code Scheduler code Synchron. code
Single-threaded app User App Server Pull screen Refresh request New messages request Check messages Send messages Horrible lag Recv messages Refresh UI Tap screen • Easy to program, but painfully slow (why? )
Multi-threaded app User App (UI) App (bg) Server Pull screen Refresh request Msg request Tap screen Refresh UI (tap) Recv Update msgs Refresh UI (pull) • Can overlap I/O and other work (e. g. , UI) Check msgs Send msgs
Multi-threaded app User App (UI) App (bg) Server Pull screen Refresh request Msg request Tap screen Check msgs Refresh UI (tap) Send msgs Recv Update msgs Refresh UI (pull) Methods on the UI thread must be fast. Why? Anything slow should run on a background thread
Scheduler activations • Problem with user-level threads • Kernel may not find a thread on which to run scheduler • Happens when thread blocks waiting for I/O • This is exactly when you want to run other threads! • Main ideas • • • Kernel assigns user process N virtual processors A virtual processor corresponds to a kernel thread Each kernel thread contains a scheduler activation (initial upcall) Kernel invokes scheduler activation when running a thread Scheduler activations (user-level code) schedule user-level threads Kernel tells user-level code about blocked and pre-empted threads
Blocking I/O example T 1: two virtual processors, run user scheduler on each stack
Blocking I/O example T 2: thread 1 blocks in kernel, create new kernel thread notifying user code thread 1 is blocked. User threads can run in context of new kernel thread.
Blocking I/O example T 3: I/O completes, and pre-empts thread 2. kernel creates new kernel thread to indicate that thread 2 was pre-empted and thread 1 can resume.
Blocking I/O example T 4: with the remaining kernel threads, userlevel code decides whether to run thread 1 or 2, or some other threads.
Course administration • Research projects • Ideally, work on an ongoing project from your own research • Otherwise, ask me for suggestions • Timeline for research projects • Proposal due Friday, March 24 • Status report due Wed, April 12 • Final report due Wed, April 19 (plus demo/presentation)
- Advantages of user level threads
- Scheduler activation in os
- March march dabrowski
- Spg ad builder
- Cathy landon
- Cox regression
- Regressioanalyysi
- Nelson and cox
- Cox mill elementary school
- Prostaglandins synthesized from
- Alicia cox ph
- Alan cox rice
- Inhibidores selectivos de la cox-2
- Dr arzoo khalid
- Nelson and cox
- Haspsa
- Cox valga
- Polipi utero
- Ponto isoelétrico
- Tom cox intermediate
- Penny cox uf
- 4 types of fog aviation
- Raymond cox qc
- Alccs
- Carrie cox
- Box-cox dönüşümü
- Tom cox intermediate
- Cox-merz rule
- Efpractice
- Cox hosting
- Cox spam blocker
- Nelson and cox
- Cox=eox/tox
- Box cox transformation minitab
- Jessica cox biografia
- Ingemar cox
- Wash sector cox's bazar
- Cox urban furniture
- Cox emil
- Cox.net
- Black and cox 1976
- Nele cox
- Cox investor relations
- Nele cox
- Cox
- Gianna bryant biography
- Fs scheduler
- Multilevel feedback queue scheduling
- Halide auto scheduler
- Lab and resource scheduler