Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scheduler activations

Similar presentations


Presentation on theme: "Scheduler activations"— Presentation transcript:

1 Scheduler activations
Landon Cox March 8, 2016

2 What is a process? Informal Formal A program in execution
Running code + things it can read/write Process ≠ program Formal ≥ 1 threads in their own address space

3 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

4 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

5 Thread control block TCB1 TCB2 TCB3 Thread 1 running Ready queue PC SP
Address Space TCB1 PC SP registers TCB2 PC SP registers TCB3 PC SP registers Ready queue Code Code Code Stack Stack Stack Thread 1 running CPU PC SP registers

6 Thread control block TCB2 TCB3 Thread 1 running Ready queue PC SP
Address Space TCB2 PC SP registers TCB3 PC SP registers Ready queue Code Stack Stack Stack Thread 1 running CPU PC SP registers

7 Switching threads What needs to happen to switch threads?
Thread returns control to scheduler For example, via timer interrupt Scheduler chooses next thread to run Scheduler saves state of current thread To its thread control block Scheduler loads context of next thread From its thread control block Jump to PC in next thread’s TCB

8 Kernel vs user threads Kernel threads User threads
Scheduler and queues reside in the kernel User threads Scheduler and queues reside in user space

9 (same for all processes) (different for every process)
32-bit address space 4GB Kernel data (same for all processes) 3GB (0xc ) User data (different for every process) 0GB Virtual memory

10 Where is the interrupt handler code?
Switching threads Virtual memory 4GB 3GB 0GB Where is the interrupt handler code? In the kernel

11 Switching threads 4GB 3GB 0GB Virtual memory SP Interrupt-handler code
PC On which stack is the IRQ handled? A kernel stack 0GB

12 Switching threads 4GB 3GB 0GB Virtual memory SP Interrupt-handler code
PC Where are the threads’ stacks? In user space 0GB

13 Switching threads 4GB 3GB 0GB Virtual memory SP Interrupt-handler code
PC Where are the threads’ stacks? In user space 0GB

14 Switching threads 4GB 3GB 0GB Virtual memory SP Interrupt-handler code
PC So far, everything is the same for user-level and kernel threads 0GB

15 Kernel threads Scheduler code Handler code For kernel threads,
the thread scheduler and TCB queues are in the kernel Interrupt-handler code Handler code TCB

16 Kernel threads Scheduler code Handler code Interrupt-handler code
Where is the lock/cv code? Interrupt-handler code Handler code TCB

17 Kernel threads Scheduler code Synchron. code Handler code
Where is the lock/cv code? Handler code Interrupt-handler code TCB Also, in the kernel

18 User-level threads Handler code Interrupt-handler code
For user-level threads, the thread scheduler and queues are in user space Handler code Interrupt-handler code TCB Scheduler code

19 User-level threads Handler code Interrupt-handler code
Where is the lock/cv code? Interrupt-handler code Handler code Also, in the user space TCB Scheduler code Synchron. code

20 Switching to a new user-level thread

21 User-level threads Handler code Interrupt-handler code Scheduler code
SP PC TCB Scheduler code Thread running user code Synchron. code Program code

22 Interrupt-handler code
User-level threads SP Interrupt-handler code Handler code PC Timer interrupt! TCB Scheduler code Synchron. code Program code

23 Yes, CPU stores this on kernel stack
User-level threads SP Interrupt-handler code Handler code PC Does the kernel know where user left off? TCB Scheduler code Synchron. code Yes, CPU stores this on kernel stack Program code

24 Depends on meaning of the timer …
User-level threads SP Interrupt-handler code Handler code PC What does kernel do next? TCB Scheduler code Synchron. code Depends on meaning of the timer … Program code

25 Kernel delivers signal to process
User-level threads SP Interrupt-handler code Handler code PC Assuming it’s a signal for the process? TCB Scheduler code Synchron. code Kernel delivers signal to process Program code

26 Interrupt-handler code
User-level threads SP Interrupt-handler code Handler code PC To deliver a signal to user code: make it look like a forced function call to signal handler. TCB Scheduler code Synchron. code Program code

27 User-level threads SP Handler code Interrupt-handler code PC
Where is the process’s timer signal handler? TCB Scheduler code Synchron. code In scheduler code (e.g., yield) Program code

28 Stack of interrupted thread
User-level threads SP Interrupt-handler code Handler code PC On what stack should the scheduler code run? TCB Scheduler code Synchron. code yield Stack of interrupted thread Program code

29 Stack of interrupted thread
User-level threads PC Interrupt-handler code Handler code SP On what stack should the scheduler code run? TCB Scheduler code Synchron. code yield Stack of interrupted thread Program code

30 No, it’s only aware of interrupted one
User-level threads PC Interrupt-handler code Handler code SP Could the kernel transfer control to any other thread? TCB Scheduler code Synchron. code yield No, it’s only aware of interrupted one Program code

31 Example user-thread library.

32 Switching to a new kernel thread

33 Kernel threads Scheduler code Synchron. code Handler code
Interrupt-handler code Handler code SP TCB PC Thread running user code Program code

34 Interrupt-handler code
Kernel threads Scheduler code Synchron. code SP Interrupt-handler code Handler code PC TCB Timer interrupt! Program code

35 Any thread. Kernel aware of all of them
Kernel threads Scheduler code Synchron. code SP Handler code Interrupt-handler code PC TCB Which thread could run next? Any thread. Kernel aware of all of them Program code

36 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

37 Advantage of kernel threads
SP Interrupt-handler code Handler code PC Which thread could run next? TCB Scheduler code Synchron. code Only the one that was interrupted Program code

38 Advantage of kernel threads
SP Interrupt-handler code Handler code PC Why might we be unable to run the interrupted thread? TCB Scheduler code Synchron. code Thread could have made a blocking system call Program code

39 Advantage of kernel threads
SP Interrupt-handler code Handler code PC TCB Scheduler code Synchron. code Program code

40 Advantage of kernel threads
SP Interrupt-handler code Handler code PC Why is this wasteful? TCB Scheduler code Synchron. code There are other threads that could run! Kernel doesn’t know they exist Program code

41 Single-threaded app User App Server
Easy to program, but painfully slow (why?) Pull screen Refresh request New messages request Check messages Horrible lag Send messages Recv messages Refresh UI Tap screen

42 Multi-threaded app User App (UI) App (bg) Server
Can overlap I/O and other work (e.g., UI) Pull screen Refresh request Msg request Check msgs Tap screen Refresh UI (tap) Send msgs Recv Update msgs Refresh UI (pull)

43 Multi-threaded app User App (UI) App (bg) Server Pull screen
Refresh request Msg request Check msgs Tap screen 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

44 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

45 T1: two virtual processors, run user scheduler on each stack
Blocking I/O example T1: two virtual processors, run user scheduler on each stack

46 Blocking I/O example T2: 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.

47 Blocking I/O example T3: 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.

48 Blocking I/O example T4: with the remaining kernel threads, user-level code decides whether to run thread 1 or 2, or some other threads.

49 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)


Download ppt "Scheduler activations"

Similar presentations


Ads by Google