Implementation of Processes

Similar documents
Processes and Threads Implementation

Learning Outcomes. Processes and Threads Implementation. Processes Process s user-level stack and execution state. Threads The Thread Model.

Processes and Threads Implementation

Learning Outcomes. Processes and Threads. Major Requirements of an Operating System. Processes and Threads

Processes and Threads

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

System Building. Events, Co-routines, Continuations and Threads - OS (and application) Execution Models. Event Model. Events

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Review: Program Execution. Memory program code program data program stack containing procedure activation records

Review: Program Execution. Memory program code program data program stack containing procedure activiation records

Threads and Concurrency

Threads and Concurrency

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

Questions answered in this lecture: CS 537 Lecture 19 Threads and Cooperation. What s in a process? Organizing a Process

Review: Program Execution. Memory program code program data program stack containing procedure activation records

Review: Program Execution. Memory program code program data program stack containing procedure activation records

Threads. Computer Systems. 5/12/2009 cse threads Perkins, DW Johnson and University of Washington 1

Threads Implementation. Jo, Heeseung

Review: Program Execution. Memory program code program data program stack containing procedure activation records

What s in a process?

Threads. Raju Pandey Department of Computer Sciences University of California, Davis Spring 2011

Lecture 4: Mechanism of process execution. Mythili Vutukuru IIT Bombay

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

Lecture 5: Concurrency and Threads

10/10/ Gribble, Lazowska, Levy, Zahorjan 2. 10/10/ Gribble, Lazowska, Levy, Zahorjan 4

Threads Chapter 5 1 Chapter 5

Processes The Process Model. Chapter 2 Processes and Threads. Process Termination. Process States (1) Process Hierarchies

Processes & Threads. Process Management. Managing Concurrency in Computer Systems. The Process. What s in a Process?

Chapter 2 Processes and Threads

What is a Process? Answer 1: a process is an abstraction of a program in execution

What s in a traditional process? Concurrency/Parallelism. What s needed? CSE 451: Operating Systems Autumn 2012

Threads Implementation. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Process Description and Control

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

Computer Systems II. First Two Major Computer System Evolution Steps

Precept 2: Non-preemptive Scheduler. COS 318: Fall 2018

Lecture 4: Threads; weaving control flow

Process Description and Control

Notes. CS 537 Lecture 5 Threads and Cooperation. Questions answered in this lecture: What s in a process?

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

Distributed Systems Operation System Support

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Process Description and Control. Chapter 3

System Call. Preview. System Call. System Call. System Call 9/7/2018

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CSE 120 Principles of Operating Systems

CSE 120 Principles of Operating Systems

Preemptive Scheduling and Mutual Exclusion with Hardware Support

Lecture 4: Memory Management & The Programming Interface

There are 10 total numbered pages, 5 Questions. You have 60 minutes. Budget your time carefully!

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Why use an Operating System? Operating System Definition

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

CA Compiler Construction

CSCE 313: Intro to Computer Systems

CS5460: Operating Systems

SE350: Operating Systems. Lecture 3: Concurrency

CSE 120 Principles of Operating Systems

Processes. Process Management Chapter 3. When does a process gets created? When does a process gets terminated?

Processes and Threads. From Processes to Threads. The Case for Threads. The Case for Threads. Process abstraction combines two concepts.

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Operating Systems Structure and Processes Lars Ailo Bongo Spring 2017 (using slides by Otto J. Anshus University of Tromsø/Oslo)

Lecture 11: Interrupt and Exception. James C. Hoe Department of ECE Carnegie Mellon University

آنستیتیوت تکنالوجی معلوماتی و مخابراتی ICTI

殷亚凤. Processes. Distributed Systems [3]

Scheduler Activations. Including some slides modified from Raymond Namyst, U. Bordeaux

Processes. Sanzheng Qiao. December, Department of Computing and Software

Process Scheduling Queues

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Overview. Thread Packages. Threads The Thread Model (1) The Thread Model (2) The Thread Model (3) Thread Usage (1)

Fall 2017 :: CSE 306. Introduction to. Virtual Memory. Nima Honarmand (Based on slides by Prof. Andrea Arpaci-Dusseau)

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

csci3411: Operating Systems

Major Requirements of an OS

What is a Process? Processes and Process Management Details for running a program

CSC369 Lecture 2. Larry Zhang

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

Sistemi in Tempo Reale

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

DISTRIBUTED COMPUTER SYSTEMS

Processes and Threads

Chapter 3 Process Description and Control

Jan 20, 2005 Lecture 2: Multiprogramming OS

OS Structure, Processes & Process Management. Don Porter Portions courtesy Emmett Witchel

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

Chapter 5: Threads. Outline

CS 111. Operating Systems Peter Reiher

Timers 1 / 46. Jiffies. Potent and Evil Magic

CHAPTER 2: PROCESS MANAGEMENT

Mon Sep 17, 2007 Lecture 3: Process Management

Threads. Threads The Thread Model (1) CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5

CISC2200 Threads Spring 2015

What is a Process? Answer 1: a process is an abstraction of a program in execution

CSC369 Lecture 2. Larry Zhang, September 21, 2015

What s An OS? Cyclic Executive. Interrupts. Advantages Simple implementation Low overhead Very predictable

Lecture 3: Processes. CMPUT 379, Section A1, Winter 2014 January 13, 15 and 17

SMD149 - Operating Systems

Memory management, part 2: outline

Transcription:

Implementation of Processes A processes information is stored in a process control block (PCB) The PCBs form a process table Sometimes the kernel stack for each process is in the PCB Sometimes some process info is on the kernel stack E.g. registers in the in OS/161 P7 P6 P5 P4 P3 P2 P1 P0 19

Implementation of Processes Example fields of a process table entry 20

Threads The Thread Model (a) Three processes each with one thread (b) One process with three threads 21

The Thread Model Items shared by all threads in a process Items private to each thread 22

The Thread Model Each thread has its own stack 23

Thread Model Local variables are per thread Allocated on the stack Global variables are shared between all threads Allocated in data section Concurrency control is an issue Dynamically allocated memory (malloc) can be global or local Program defined 24

Thread Usage A word processor with three threads 25

Thread Usage A multithreaded Web server 26

Thread Usage Rough outline of code for previous slide (a) Dispatcher thread (b) Worker thread 27

Thread Usage Three ways to construct a server 28

Summarising Why Threads? Simpler to program than a state machine Less resources are associated with them than a complete process Cheaper to create and destroy Shares resources (especially memory) between them Performance: Threads waiting for I/O can be overlapped with computing threads Note if all threads are compute bound, then there is no performance improvement (on a uniprocessor) Threads can take advantage of the parallelism available on machines with more than one CPU (multiprocessor) 29

Implementing Threads in User Space A user-level threads package 30

User-level Threads Implementation at user-level User-level Thread Control Block (TCB), ready queue, blocked queue, and dispatcher Kernel has no knowledge of the threads (it only sees a single process) If a thread blocks waiting for a resource held by another thread, its state is save and the dispatcher switches to another ready thread Thread management (create, exit, yield, wait) are implemented in a runtime support library 31

User-Level Threads Pros Thread management and switching at user level is much faster than doing it in kernel level No need to trap into kernel and back to switch Dispatcher algorithm can be tuned to the application E.g. use priorities Can be implemented on any OS (thread or nonthread aware) Can easily support massive numbers of threads on a per-application basis Use normal application virtual memory Kernel memory more contrained. Difficult to efficiently support wildly differing numbers of threads for different applications. 32

User-level Threads Cons Threads have to yield() manually (no timer interrupt delivery to user-level) Co-operative multithreading A single poorly design/implemented thread can monopolise the available CPU time There are work-arounds (e.g. a timer signal per second to enable pre-emptive multithreading), they are course grain and kludgey. Does not take advantage of multiple CPUs (in reality, we still have a single threaded process as far as the kernel is concerned) 33

User-Level Threads Cons If a thread makes a blocking system call (or takes a page fault), the process (and all the internal threads) blocks Can t overlap I/O with computation Can use wrappers as a work around Example: wrap the read() call Use select() to test if read system call would block» select() then read()» Only call read() if it won t block» Otherwise schedule another thread Wrapper requires 2 system calls instead of one» Wrappers are needed for environments doing lots of blocking system calls? Can change to kernel to support non-blocking system call Lose on any system advantage, page faults still a problem. 34

Implementing Threads in the Kernel A threads package managed by the kernel 35

Kernel Threads Threads are implemented in the kernel TCBs are stored in the kernel A subset of information in a traditional PCB The subset related to execution context TCBs have a PCB associated with them Resources associated with the group of threads (the process) Thread management calls are implemented as system calls E.g. create, wait, exit 36

Kernel Threads Cons Thread creation and destruction, and blocking and unblocking threads requires kernel entry and exit. More expensive than user-level equivalent Pros Preemptive multithreading Parallelism Can overlap blocking I/O with computation Can take advantage of a multiprocessor 37

Hybrid Schemes 38

Multiprogramming Implementation Skeleton of what lowest level of OS does when an interrupt occurs a thread/context switch 39

Thread Switch A switch between threads can happen any time the OS is invoked On a system call Mandatory if system call blocks or on exit(); On an exception Mandatory if offender is killed On an interrupt Triggering a dispatch is the main purpose of the timer interrupt A thread switch can happen between any two instructions Note instructions do not equal program statements 40

Context Switch Thread switch must be transparent for threads When dispatched again, thread should not notice that something else was running in the meantime (except for elapsed time) OS must save all state that affects the thread This state is called the thread context Switching between threads consequently results in a context switch. 41

Thread a thread_switch(a,b) { Thread b } Simplified Explicit Thread Switch } thread_switch(b,a) { thread_switch(a,b) { } 42

Example Context Switch Running in user mode, SP points to userlevel activation stack Representation of Kernel Stack (Memory) Kernel SP 43

Example Context Switch Take an exception, syscall, or interrupt, and we switch to the kernel stack Kernel SP 44

Example Context Switch We push a on the stack Also called exception frame, user-level context. Includes the user-level PC and SP Kernel SP 45

Example Context Switch Call C code to process syscall, exception, or interrupt Results in a C activation stack building up Kernel SP C activation stack 46

Example Context Switch The kernel decides to perform a context switch It chooses a target thread (or process) It pushes remaining kernel context onto the stack Kernel SP Kernel State C activation stack 47

Example Context Switch Any other existing thread must be in kernel mode (on a uni processor), and have a similar stack layout to the stack we are Kernel stacks of other threads currently using Kernel SP Kernel State C activation stack Kernel State C activation stack Kernel State C activation stack 48

Example Context Switch We save the current SP in the PCB (or TCB), and load the SP of the target thread. Thus we have switched contexts Kernel SP Kernel State C activation stack Kernel State C activation stack Kernel State C activation stack 49

Example Context Switch Load the target thread s previous context, and return to C Kernel SP Kernel State C activation stack C activation stack Kernel State C activation stack 50

Example Context Switch The C continues and (in this example) returns to user mode. Kernel SP Kernel State C activation stack Kernel State C activation stack 51

Example Context Switch The user-level context is restored Kernel SP Kernel State C activation stack Kernel State C activation stack 52

Example Context Switch The user-level SP is restored Kernel SP Kernel State C activation stack Kernel State C activation stack 53

The Interesting Part of a Thread Switch What does the push kernel state part do??? Kernel SP Kernel State C activation stack Kernel State C activation stack 54

OS/161 md_switch md_switch(struct pcb *old, struct pcb *nu) { if (old==nu) { return; } /* * Note: we don't need to switch curspl, because splhigh() * should always be in effect when we get here and when we * leave here. */ old->pcb_kstack = curkstack; old->pcb_ininterrupt = in_interrupt; curkstack = nu->pcb_kstack; in_interrupt = nu->pcb_ininterrupt; mips_switch(old, nu); } 55

OS/161 mips_switch mips_switch: /* * a0 contains a pointer to the old thread's struct pcb. * a1 contains a pointer to the new thread's struct pcb. * * The only thing we touch in the pcb is the first word, which * we save the stack pointer in. The other registers get saved * on the stack, namely: * * s0-s8 * gp, ra * * The order must match arch/mips/include/switchframe.h. */ /* Allocate stack space for saving 11 registers. 11*4 = 44 */ addi sp, sp, -44 56

OS/161 mips_switch /* Save the registers */ sw ra, 40(sp) sw gp, 36(sp) sw s8, 32(sp) sw s7, 28(sp) sw s6, 24(sp) sw s5, 20(sp) sw s4, 16(sp) sw s3, 12(sp) sw s2, 8(sp) sw s1, 4(sp) sw s0, 0(sp) Save the registers that the C procedure calling convention expects preserved /* Store the old stack pointer in the old pcb */ sw sp, 0(a0) 57

OS/161 mips_switch /* Get the new stack pointer from the new pcb */ lw sp, 0(a1) nop /* delay slot for load */ /* Now, restore the registers */ lw s0, 0(sp) lw s1, 4(sp) lw s2, 8(sp) lw s3, 12(sp) lw s4, 16(sp) lw s5, 20(sp) lw s6, 24(sp) lw s7, 28(sp) lw s8, 32(sp) lw gp, 36(sp) lw ra, 40(sp) nop /* delay slot for load */ /* and return. */ j ra addi sp, sp, 44 /* in delay slot */.end mips_switch 58

Thread a mips_switch(a,b) { Thread b } Revisiting Thread Switch } mips_switch(b,a) { mips_switch(a,b) { } 59