CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska lazowska@cs.washington.edu 570 Allen.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 3 Process Description and Control
CSE 451: Operating Systems Winter 2007 Module 4 Processes Ed Lazowska Allen Center 570.
CSE 451: Operating Systems Winter 2006 Module 4 Processes Ed Lazowska Allen Center 570.
Processes CSCI 444/544 Operating Systems Fall 2008.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
OS Spring’03 Introduction Operating Systems Spring 2003.
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.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
CSE451 Processes Spring 2001 Gary Kimura Lecture #4 April 2, 2001.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Processes April 5, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
CSE 451: Operating Systems Spring 2012 Module 4 Processes Ed Lazowska Allen Center 570.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
CSE 451: Operating Systems Winter 2012 Processes Mark Zbikowski Gary Kimura.
Implementing Processes and Process Management Brian Bershad.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
1 CSE451 Architectural Supports for Operating Systems Autumn 2002 Gary Kimura Lecture #2 October 2, 2002.
CSE 451: Operating Systems Winter 2015 Module 5 1 / 2 User-Level Threads & Scheduler Activations Mark Zbikowski 476 Allen Center.
CSE 451: Operating Systems Winter 2014 Module 5 Threads Mark Zbikowski Allen Center 476 © 2013 Gribble, Lazowska, Levy, Zahorjan.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Operating Systems CMPSC 473
Process Management Process Concept Why only the global variables?
Protection and OS Structure
CS 6560: Operating Systems Design
CSE 451: Operating Systems Winter 2011 Threads
Protection of System Resources
CSE 451: Operating Systems Spring 2013 Module 5 Threads
CSE 451: Operating Systems Autumn 2013 Module 4 Processes
CSE 451: Operating Systems Winter 2010 Module 5 Threads
Processes in Unix, Linux, and Windows
More examples How many processes does this piece of code create?
CSE 451: Operating Systems Winter 2007 Module 5 Threads
CSE 451: Operating Systems Autumn 2004 Module 5 Threads
CSE 451: Operating Systems Winter 2011 Processes
CSE 451: Operating Systems Spring 2008 Module 5 Threads
CSE 451: Operating Systems Winter 2010 Module 4 Processes
Lecture Topics: 11/1 General Operating System Concepts Processes
CSE 451: Operating Systems Autumn 2003 Lecture 5 Threads
CSE 451: Operating Systems Winter 2007 Module 5 Threads
Processes Hank Levy 1.
CSE 451: Operating Systems Winter 2003 Lecture 5 Threads
Processes and Process Management
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
CSE 451: Operating Systems Winter 2009 Module 5 Threads
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
CSE 451: Operating Systems Spring 2005 Module 5 Threads
October 9, 2002 Gary Kimura Lecture #5 October 9, 2002
CSE 451: Operating Systems Winter 2012 Threads
CSE 451: Operating Systems Autumn 2009 Module 5 Threads
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
CSE 451: Operating Systems Winter 2007 Module 2 Architectural Support for Operating Systems Brian Bershad 562 Allen Center 1.
Processes in Unix, Linux, and Windows
CSE 451: Operating Systems Autumn 2009 Module 4 Processes
Processes in Unix and Windows
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
CS510 Operating System Foundations
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
Processes Hank Levy 1.
CSE 451: Operating Systems Autumn 2010 Module 4 Processes
CS703 – Advanced Operating Systems
CSE 451: Operating Systems Autumn Module 24 Virtual Machine Monitors
CSE451 Introduction to Operating Systems Spring 2007
CSE 451: Operating Systems Winter 2006 Module 5 Threads
CSE 451: Operating Systems Winter 2001 Lecture 5 Threads
Presentation transcript:

CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska lazowska@cs.washington.edu 570 Allen Center 1

© 2012 Gribble, Lazowska, Levy, Zahorjan What’s “in” a process? A process consists of (at least): An address space, containing the code (instructions) for the running program the data for the running program (static data, heap data, stack) CPU state, consisting of The program counter (PC), indicating the next instruction The stack pointer Other general purpose register values A set of OS resources open files, network connections, sound channels, … In other words, it’s all the stuff you need to run the program or to re-start it, if it’s interrupted at some point © 2012 Gribble, Lazowska, Levy, Zahorjan

The OS gets control because of … Trap: Program executes a syscall Exception: Program does something unexpected (e.g., page fault) Interrupt: A hardware device requests service © 2012 Gribble, Lazowska, Levy, Zahorjan

© 2012 Gribble, Lazowska, Levy, Zahorjan PCBs and CPU state When a process is running, its CPU state is inside the CPU PC, SP, registers CPU contains current values When the OS gets control (trap, exception, interrupt), the OS saves the CPU state of the running process in that process’s PCB when the OS returns the process to the running state, it loads the hardware registers with values from that process’s PCB – general purpose registers, stack pointer, instruction pointer This is called a context switch © 2012 Gribble, Lazowska, Levy, Zahorjan

© 2012 Gribble, Lazowska, Levy, Zahorjan The syscall How do user programs do something privileged? e.g., how can you write to a disk if you can’t execute an I/O instructions? User programs must call an OS procedure – that is, get the OS to do it for them OS defines a set of system calls User-mode program executes system call instruction with a parameter indicating the specific function desired Syscall instruction Like a protected procedure call © 2012 Gribble, Lazowska, Levy, Zahorjan

© 2012 Gribble, Lazowska, Levy, Zahorjan The syscall instruction atomically: Saves the current PC Sets the execution mode to privileged Sets the PC to a handler address With that, it’s a lot like a local procedure call Caller puts arguments in a place callee expects (registers or stack) One of the args is a syscall number, indicating which OS function to invoke Callee (OS) saves caller’s state (registers, other control state) so it can use the CPU OS function code runs OS must verify caller’s arguments (e.g., pointers) OS returns using a special instruction Automatically sets PC to return address and sets execution mode to user © 2012 Gribble, Lazowska, Levy, Zahorjan

A kernel crossing illustrated Firefox: read(int fileDescriptor, void *buffer, int numBytes) Save user PC PC = trap handler address Enter kernel mode user mode kernel mode trap handler PC = saved PC Enter user mode Save app state Verify syscall number Find sys_read( ) handler in vector table sys_read( ) kernel routine Verify args Initiate read Choose next process to run Setup return values Restore app state ERET instruction © 2012 Gribble, Lazowska, Levy, Zahorjan

Interrupts and exceptions work the same way as traps Transition to kernel mode Save state of running process in PCB Handler routine deals with whatever occurred Choose a next process to run Restore that process’s CPU state from its PCB Execute an instruction that returns you to user mode at the appropriate instruction © 2012 Gribble, Lazowska, Levy, Zahorjan

The OS kernel is not a process It’s just a block of code! (In a microkernel OS, many things that you normally think of as the operating system execute as user-mode processes. But the OS kernel is just a block of code.) © 2012 Gribble, Lazowska, Levy, Zahorjan

© 2012 Gribble, Lazowska, Levy, Zahorjan Threads Key idea: separate the concept of a process (address space, OS resources) … from that of a minimal “thread of control” (execution state: stack, stack pointer, program counter, registers) This execution state is usually called a thread, or sometimes, a lightweight process thread © 2012 Gribble, Lazowska, Levy, Zahorjan

The design space Key older UNIXes MS/DOS address space one thread per process one thread per process one process many processes thread Java Mach, NT, Linux, … many threads per process many threads per process one process many processes © 2012 Gribble, Lazowska, Levy, Zahorjan

(old) Process address space 0xFFFFFFFF stack (dynamic allocated mem) SP heap (dynamic allocated mem) address space static data (data segment) code (text segment) PC 0x00000000 © 2012 Gribble, Lazowska, Levy, Zahorjan

(new) Address space with threads thread 1 stack SP (T1) 0xFFFFFFFF thread 2 stack SP (T2) thread 3 stack SP (T3) address space heap (dynamic allocated mem) static data (data segment) 0x00000000 code (text segment) PC (T2) PC (T1) PC (T3) © 2012 Gribble, Lazowska, Levy, Zahorjan

© 2012 Gribble, Lazowska, Levy, Zahorjan Kernel threads OS now manages threads and processes / address spaces all thread operations are implemented in the kernel OS schedules all of the threads in a system if one thread in a process blocks (e.g., on I/O), the OS knows about it, and can run other threads from that process possible to overlap I/O and computation inside a process Kernel threads are cheaper than processes less state to allocate and initialize But, they’re still pretty expensive for fine-grained use orders of magnitude more expensive than a procedure call thread operations are all system calls context switch argument checks must maintain kernel state for each thread © 2012 Gribble, Lazowska, Levy, Zahorjan

© 2012 Gribble, Lazowska, Levy, Zahorjan In the beginning … Fork a process Creates an address space that’s a clone of the parent, with one thread There’s a PCB that describes the address space and the OS resources There’s a TCB that holds the CPU state and is the unit of scheduling The TCB and the PCB are linked – e.g., so the OS knows which set of page tables to use when scheduling a particular thread First thread can create additional threads OS creates a new TCB, initializes CPU state (an entry point must be provided in the “create” syscall) © 2012 Gribble, Lazowska, Levy, Zahorjan

Kernel threads Mach, NT, Linux, … address space What happens when a thread wants to: create another thread? terminate itself? wait for some condition? signal some condition? do I/O? os kernel thread CPU (thread create, destroy, signal, wait, etc.) © 2012 Gribble, Lazowska, Levy, Zahorjan

User-level threads user-level thread library address space (thread create, destroy, signal, wait, etc.) address space What happens when a thread wants to: create another thread? terminate itself? wait for some condition? signal some condition? do I/O? os kernel thread CPU 12/6/2018 © 2012 Gribble, Lazowska, Levy, Zahorjan 17

© 2012 Gribble, Lazowska, Levy, Zahorjan You really want multiple threads per address space Kernel threads are much more efficient than processes, but they’re still not cheap all operations require a kernel call and parameter validation User-level threads are: really fast/cheap great for common-case operations creation, synchronization, destruction can suffer in uncommon cases due to kernel obliviousness I/O preemption of a lock-holder Scheduler activations are an answer pretty subtle though © 2012 Gribble, Lazowska, Levy, Zahorjan