Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scheduler activations Landon Cox March 23, 2016. What is a process? Informal A program in execution Running code + things it can read/write Process ≠

Similar presentations


Presentation on theme: "Scheduler activations Landon Cox March 23, 2016. What is a process? Informal A program in execution Running code + things it can read/write Process ≠"— Presentation transcript:

1 Scheduler activations Landon Cox March 23, 2016

2 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

3 Parts of a process Thread Sequence of executing instructions Active: does things Address space Data the process uses as it runs Passive: acted upon by threads

4 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

5 Private vs global thread state What state is private to each thread? PC (where actor is in his/her script) Stack, SP (actor’s mindset) What state is shared? Global vars, heap (props) Code (same script)

6 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

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

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

9 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

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

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

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

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

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

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

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

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

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

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

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

21 Switching to a new user-level thread

22 User-level threads Scheduler code Handler code Interrupt- handler code Synchron. code SP Program code PCPC Thread running user code

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

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

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

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

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

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

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

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

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

32 Switching to a new kernel thread

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

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

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

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 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 Scheduler code Handler code Interrupt- handler code Synchron. code Program code PC SP Which thread could run next? Only the one that was interrupted

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

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

40 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

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

42 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.

43 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.

44 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.

45 Course administration Research projects Ideally, work on an ongoing project from your own research Otherwise, suggestions will be posted tonight Timeline for research projects Proposal due Friday, April 1 Status report due Wednesday, April 13 Final report due Wednesday, April 20 (plus demo/presentation) Exams Still grading them Will return exams on Friday


Download ppt "Scheduler activations Landon Cox March 23, 2016. What is a process? Informal A program in execution Running code + things it can read/write Process ≠"

Similar presentations


Ads by Google