CS 4284 Systems Capstone Godmar Back Processes and Threads.

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Chapter 3 Process Description and Control
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
CS 4284 Systems Capstone Godmar Back Linking and Loading.
1 Operating Systems and Protection CS Goals of Today’s Lecture How multiple programs can run at once  Processes  Context switching  Process.
Context Switch Animation Another one by Anastasia.
Figure 2.8 Compiler phases Compiling. Figure 2.9 Object module Linking.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
OS Spring’03 Introduction Operating Systems Spring 2003.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Process Description and Control A process is sometimes called a task, it is a program in execution.
OS Spring’04 Introduction Operating Systems Spring 2004.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
6.828: PC hardware and x86 Frans Kaashoek
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Introduction to Processes CS Intoduction to Operating Systems.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Chapter 3 Process Description and Control
Multiprogramming CSE451 Andrew Whitaker. Overview Multiprogramming: Running multiple programs “at the same time”  Requires multiplexing (sharing) the.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
REVIEW OF COMMONLY USED DATA STRUCTURES IN OS. NEEDS FOR EFFICIENT DATA STRUCTURE Storage complexity & Computation complexity matter Consider the problem.
Activation Records (in Tiger) CS 471 October 24, 2007.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
Functions/Methods in Assembly
Compiler Construction Code Generation Activation Records
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
1 Assembly Language: Function Calls Jennifer Rexford.
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
Virtualizing the CPU: Processes 1. How to provide the illusion of many CPUs? CPU virtualizing – The OS can promote the illusion that many virtual CPUs.
CS 3214 Computer Systems Lecture 9 Godmar Back.
Processes and threads.
Process concept.
C function call conventions and the stack
Processes.
CS 5204 Operating Systems Processes and Threads Godmar Back.
Anton Burtsev February, 2017
Protection of System Resources
Anton Burtsev February, 2017
ICS143A: Principles of Operating Systems Lecture 13: Context switching
Overview of today’s lecture
CS 5204 Operating Systems Linking and Loading Godmar Back.
Introduction to Compilers Tim Teitelbaum
Machine-Level Programming 4 Procedures
CS 4284 Systems Capstone Linking and Loading Godmar Back.
Assembly Language Programming II: C Compiler Calling Sequences
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Discussions on HW2 Objectives
System Calls David Ferry CSCI 3500 – Operating Systems
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
Discussions on HW2 Objectives
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Low-Level Thread Dispatching on the x86
Presentation transcript:

CS 4284 Systems Capstone Godmar Back Processes and Threads

Processes & Threads

CS 4284 Spring 2013 Overview Definitions How does OS execute processes? –How do kernel & processes interact –How does kernel switch between processes –How do interrupts fit in What’s the difference between threads/processes Process States Priority Scheduling

CS 4284 Spring 2013 Process These are all possible definitions: –A program in execution –An instance of a program running on a computer –Schedulable entity (*) –Unit of resource ownership –Unit of protection –Execution sequence (*) + current state (*) + set of resources (*) can be said of threads as well

CS 4284 Spring 2013 Alternative definition Thread: –Execution sequence + CPU state (registers + stack) Process: –n Threads + Resources shared by them (specifically: accessible heap memory, global variables, file descriptors, etc.) In most contemporary OS, n >= 1. In Pintos, n=1: a process is a thread – as in traditional Unix. –Following discussion applies to both threads & processes.

CS 4284 Spring 2013 Context Switching Multiprogramming: switch to another process if current process is (momentarily) blocked Time-sharing: switch to another process periodically to make sure all process make equal progress –this switch is called a context switch. Must understand how it works –how it interacts with user/kernel mode switching –how it maintains the illusion of each process having the CPU to itself (process must not notice being switched in and out!)

CS 4284 Spring 2013 Context Switching Process 1 Process 2 Kernel user mode kernel mode Timer interrupt: P1 is preempted, context switch to P2 System call: (trap): P2 starts I/O operation, blocks context switch to process 1 I/O device interrupt: P2’s I/O complete switch back to P2 Timer interrupt: P2 still has time left, no context switch Timer interrupt: P2 still has time left, no context switch

CS 4284 Spring 2013 Aside: Kernel Threads Process 1 Process 2 Kernel user mode kernel mode Most OS (including Pintos) support kernel threads that never run in user mode – in fact, in Project 1, all Pintos threads run like that. Kernel Thread Careful: “kernel thread” not the same as kernel-level thread (KLT) – more on KLT later

CS 4284 Spring 2013 Mode Switching User  Kernel mode –For reasons external or internal to CPU External (aka hardware) interrupt: –timer/clock chip, I/O device, network card, keyboard, mouse –asynchronous (with respect to the executing program) Internal interrupt (aka software interrupt, trap, or exception) –are synchronous –can be intended: for system call (process wants to enter kernel to obtain services) –or unintended (usually): fault/exception (division by zero, attempt to execute privileged instruction in user mode) Kernel  User mode switch on iret instruction

CS 4284 Spring 2013 Context vs Mode Switching Mode switch guarantees kernel gains control when needed –To react to external events –To handle error situations –Entry into kernel is controlled Not all mode switches lead to context switches –Kernel code’s logic decides when – subject of scheduling Mode switch always hardware supported –Context switch (typically) not – this means many options for implementing it!

CS 4284 Spring 2013 Implementing Processes To maintain illusion, must remember a process’s information when not currently running Process Control Block (PCB) –Identifier (*) –Value of registers, including stack pointer (*) –Information needed by scheduler: process state (whether blocked or not) (*) –Resources held by process: file descriptors, memory pages, etc. (*) applies to TCB (thread control block) as well

CS 4284 Spring 2013 PCB vs TCB In 1:1 systems (Pintos), TCB==PCB –struct thread –add information there as projects progress In 1:n systems: –TCB contains execution state of thread + scheduling information + link to PCB for process to which thread belongs –PCB contains identifier, plus information about resources shared by all threads struct thread { tid_t tid; /* Thread identifier. */ enum thread_status status; /* Thread state. */ char name[16]; /* Name. */ uint8_t *stack; /* Saved stack pointer. */ int priority; /* Priority. */ struct list_elem elem; /* List element. */ /* others you’ll add as needed. */ };

CS 4284 Spring 2013 Steps in context switch: high-level Save the current process’s execution state to its PCB Update current’s PCB as needed Choose next process N Update N’s PCB as needed Restore N’s PCB execution state –May involve reprogramming MMU

CS 4284 Spring 2013 Execution State Saving/restoring execution state is highly tricky: –Must save state without destroying it Registers –On x86: eax, ebx, ecx, … Stack –Special area in memory that holds activation records: e.g., the local (automatic) variables of all function calls currently in progress –Saving the stack means retaining that area & saving a pointer to it (“stack pointer” = esp)

CS 4284 Spring 2013 The Stack, seen from C/C++ Q.: which of these variables are stored on the stack, and which are not? int a; static int b; int c = 5; struct S { int t; } s; void func(int d) { static int e; int f; struct S w; int *g = new int[10]; } A.: On stack: d, f, w (including w.t), g Not on stack: a, b, c, s (including s.t), e, g[0]…g[9] A.: On stack: d, f, w (including w.t), g Not on stack: a, b, c, s (including s.t), e, g[0]…g[9]

CS 4284 Spring 2013 Switching Procedures Inside kernel, context switch is implemented in some procedure (function) called from C code –Appears to caller as a procedure call Must understand how to switch procedures (call/return) Procedure calling conventions –Architecture-specific –Defined by ABI (application binary interface), implemented by compiler –Pintos uses SVR4 ABI

CS 4284 Spring 2013 x86 Calling Conventions Caller saves caller-saved registers as needed Caller pushes arguments, right-to-left on stack via push assembly instruction Caller executes CALL instruction: save address of next instruction & jump to callee Caller resumes: pop arguments off the stack Caller restores caller-saved registers, if any Callee executes: –Saves callee-saved registers if they’ll be destroyed –Puts return value (if any) in eax Callee returns: pop return address from stack & jump to it

CS 4284 Spring 2013 Example int globalvar; int callee(int a, int b) { return a + b; } int caller(void) { return callee(5, globalvar); } callee: pushl %ebp movl %esp, %ebp movl 12(%ebp), %eax addl 8(%ebp), %eax leave ret caller: pushl %ebp movl %esp, %ebp pushl globalvar pushl $5 call callee popl %edx popl %ecx leave ret

CS 4284 Spring 2013 Pintos Context Switch (1) threads/thread.c, threads/switch.S static void schedule (void) { struct thread *cur = running_thread (); struct thread *next = next_thread_to_run (); struct thread *prev = NULL; if (cur != next) prev = switch_threads (cur, next); retlabel: /* not in actual code */ schedule_tail (prev); } uint32_t thread_stack_ofs = offsetof (struct thread, stack); Stack … next cur &retlabel esp

CS 4284 Spring 2013 Pintos Context Switch (2) switch_threads: # Save caller's register state. # Note that the SVR4 ABI allows us to destroy %eax, %ecx, %edx, # but requires us to preserve %ebx, %ebp, %esi, %edi. pushl %ebx; pushl %ebp; pushl %esi; pushl %edi # Get offsetof (struct thread, stack). mov thread_stack_ofs, %edx # Save current stack pointer to old thread's stack. movl SWITCH_CUR(%esp), %eax movl %esp, (%eax,%edx,1) # Restore stack pointer from new thread's stack. movl SWITCH_NEXT(%esp), %ecx movl (%ecx,%edx,1), %esp # Restore caller's register state. popl %edi; popl %esi; popl %ebp; popl %ebx ret Stack … next cur &retlabel esp Stack … next cur &retlabel ebx ebp esi edi esp #define SWITCH_CUR 20 #define SWITCH_NEXT 24 #define SWITCH_CUR 20 #define SWITCH_NEXT 24 cur->stack = esp esp = next->stack // switch_thread (struct thread *cur, struct thread *next)

CS 4284 Spring 2013 Famous Quote For The Day Source: Dennis Ritchie, Unix V6 slp.c (context- switching code) as per The Unix Heritage Society (tuhs.org); gif by Eddie Koehler.The Unix Heritage Society If the new process paused because it was swapped out, set the stack level to the last call to savu(u_ssav). This means that the return which is executed immediately after the call to aretu actually returns from the last routine which did the savu. You are not expected to understand this.

CS 4284 Spring 2013 Pintos Context Switch (3) All state is stored on outgoing thread’s stack, and restored from incoming thread’s stack –Each thread has a 4KB page for its stack –Called “kernel stack” because it’s only used when thread executes in kernel mode –Mode switch automatically switches to kernel stack x86 does this in hardware, curiously. switch_threads assumes that the thread that’s switched in was suspended in switch_threads as well. –Must fake that environment when switching to a thread for the first time. Aside: none of the thread switching code uses privileged instructions: –that’s what makes user-level threads (ULT) possible

CS 4284 Spring 2013 Pintos Kernel Stack 4 kB | kernel stack | | | | | V | | grows downward | |... | | switch_threads’s | | stack frame <---+ | | | | | magic | | | : | | | stack---+ | | name | | status | 0 kB One page of memory captures a process’s kernel stack + PCB Don’t allocate large objects on the stack: void kernel_function(void) { char buf[4096]; // DON’T // KERNEL STACK OVERFLOW // guaranteed }

CS 4284 Spring 2013 Context Switching, Take 2 Process 1 Process 2 Kernel user mode kernel mode intr_entry: (saves entire CPU state) (switches to kernel stack) intr_entry: (saves entire CPU state) (switches to kernel stack) intr_exit: (restore entire CPU state) (switch back to user stack) iret intr_exit: (restore entire CPU state) (switch back to user stack) iret switch_threads: (in) (saves caller’s state) switch_threads: (in) (saves caller’s state) switch_threads: (out) (restores caller’s state) switch_threads: (out) (restores caller’s state) (kernel stack switch)

CS 4284 Spring 2013 External Interrupts & Context Switches intr_entry: /* Save caller's registers. */ pushl %ds; pushl %es; pushl %fs; pushl %gs; pushal /* Set up kernel environment. */ cld mov $SEL_KDSEG, %eax /* Initialize segment registers. */ mov %eax, %ds; mov %eax, %es leal 56(%esp), %ebp /* Set up frame pointer. */ pushl %esp call intr_handler /* Call interrupt handler. Context switch happens in there*/ addl $4, %esp /* FALL THROUGH */ intr_exit: /* Separate entry for initial user program start */ /* Restore caller's registers. */ popal; popl %gs; popl %fs; popl %es; popl %ds iret /* Return to current process, or to new process after context switch. */

CS 4284 Spring 2013 Context Switching: Summary Context switch means to save the current and restore next process’s execution context Context Switch != Mode Switch –Although mode switch often precedes context switch Asynchronous context switch happens in interrupt handler –Usually last thing before leaving handler Have ignored so far when to context switch & why  next

CS 4284 Spring 2013 Process States Only 1 process (per CPU) can be in RUNNING state RUNNING READY BLOCKED Process must wait for event Event arrived Scheduler picks process Process preempted

CS 4284 Spring 2013 Process Events What’s an event? –External event: disk controller completes sector transfer to memory network controller signals that new packet has been received clock has advanced to a predetermined time –Events that arise from process interaction: a resource that was previously held by some process is now available (e.g., lock_release) an explicit signal is sent to a process (e.g., cond_signal) a process has exited or was killed a new process has been created

CS 4284 Spring 2013 Process Lists All ready processes are inserted in a “ready list” data structure –Running process typically not kept on ready list –Can implement as multiple (real) ready lists, e.g., one for each priority class All blocked processes are kept on lists –List usually associated with event that caused blocking – usually one list per object that’s causing events Most of scheduling involves simple and clever ways of manipulating lists

CS 4284 Spring 2013 Priority Based Scheduling Done in Linux (pre ), Windows, Pintos (after you complete Project 1), … MIN MAX Higher Priority Only threads with the highest priority run If more than one, round-robin

CS 4284 Spring 2013 Priority Based Scheduling (2) Advantage: –Dead simple: the highest-priority process runs –Q.: what is the complexity of finding which process that is? Disadvantage: –Not fair: lower-priority processes will never run –Hence, must adjust priorities somehow Many schedulers used in today’s general purpose and embedded OS work like this –Only difference is how/whether priorities are adjusted to provide fairness and avoid starvation –Exception: Linux “completely-fair scheduler” uses different scheme, will discuss that later in semester

CS 4284 Spring 2013 Reasons for Preemption Generally two: quantum expired or change in priorities Reason #1: –A process of higher importance than the one that’s currently running has just become ready Reason #2: –Time Slice (or Quantum) expired Question: what’s good about long vs. short time slices?

CS 4284 Spring 2013 I/O Bound vs CPU Bound Procs Processes that usually exhaust their quanta are said to be CPU bound Processes that frequently block for I/O are said to be I/O bound Q.: what are examples of each? What policy should a scheduler use to juggle the needs of both?

CS 4284 Spring 2013 Process States w/ Suspend Can be useful sometimes to suspend processes –By user request: ^Z in Linux shell/job control –By OS decision: swapping out entire processes (Solaris & Windows do that, Linux doesn’t)

CS 4284 Spring 2013 Windows XP Thread state diagram in a industrial kernel Source: Dave Probert, Windows Internals – Copyright Microsoft 2003

CS 4284 Spring 2013 Windows XP Priority scheduler uses 32 priorities Scheduling class determines range in which priority are adjusted Source: Microsoft® Windows® Internals, Fourth Edition: Microsoft Windows Server™

CS 4284 Spring 2013 Project 1 Suggested Timeline By early next week: –Have read relevant project documentation, set up git, built and run your first kernel, designed your data structures for alarm clock Alarm clock first Pass all basic priority tests next Priority Inheritance & Advanced Scheduler will take the most time to implement & debug, start them in parallel –Should have design for priority inheritance –Develop & test fixed-point layer independently

CS 4284 Spring 2013 Using thread_yield() to implement preemption Current thread (“RUNNING”) is moved to READY state, added to READY list. Then scheduler is invoked. Picks a new READY thread from READY list. Case a): there’s only 1 READY thread. Thread is rescheduled right away Case b): there are other READY thread(s) –b.1) another thread has higher priority – it is scheduled –b.2) another thread has same priority – it is scheduled provided the previously running thread was inserted in tail of ready list. “thread_yield()” is a call you can use whenever you identify a need to preempt current thread. Exception: inside an interrupt handler, use “intr_yield_on_return()” instead RUNNING READY BLOCKED Process must wait for event Event arrived Scheduler picks process Process preempted

CS 4284 Spring 2013 Type-safe arithmetic types in C typedef struct { double re; double im; } complex_t; static inline complex_t complex_add(complex_t x, complex_t y) { return (complex_t){ x.re + y.re, x.im + y.im }; } typedef struct { double re; double im; } complex_t; static inline complex_t complex_add(complex_t x, complex_t y) { return (complex_t){ x.re + y.re, x.im + y.im }; } static inline double complex_real(complex_t x) { return x.re; } static inline double complex_imaginary(complex_t x) { return x.im; } static inline double complex_abs(complex_t x) { return sqrt(x.re * x.re + x.im * x.im); } static inline double complex_real(complex_t x) { return x.re; } static inline double complex_imaginary(complex_t x) { return x.im; } static inline double complex_abs(complex_t x) { return sqrt(x.re * x.re + x.im * x.im); } Pitfall: typedef int fixed_point_t; fixed_point_t x; int y; x = y; // no compile error