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

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

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Lecture 5: Concurrency and Threads

SE350: Operating Systems. Lecture 3: Concurrency

Processes and Threads Implementation

Process Scheduling Queues

Implementation of Processes

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

Processes and Threads Implementation

Threads Implementation. Jo, Heeseung

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

CSE 120 Principles of Operating Systems

CSE 120 Principles of Operating Systems

Mutex Implementation

CSE 120 Principles of Operating Systems

Lecture 4: Threads; weaving control flow

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

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

Processes and Threads

CS 318 Principles of Operating Systems

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

CS510 Operating System Foundations. Jonathan Walpole

CS 318 Principles of Operating Systems

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

CS 153 Design of Operating Systems Winter 2016

Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

What s in a process?

Process Description and Control. Chapter 3

Threads and Too Much Milk! CS439: Principles of Computer Systems January 31, 2018

Threads and Too Much Milk! CS439: Principles of Computer Systems February 6, 2019

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

Lecture 5: Synchronization w/locks

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

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

Introduction to the Process David E. Culler CS162 Operating Systems and Systems Programming Lecture 2 Sept 3, 2014

Operating Systems Design Fall 2010 Exam 1 Review. Paul Krzyzanowski

Why use an Operating System? Operating System Definition

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Computer Systems II. First Two Major Computer System Evolution Steps

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

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

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

Yielding, General Switching. November Winter Term 2008/2009 Gerd Liefländer Universität Karlsruhe (TH), System Architecture Group

An Operating System in Action

SMD149 - Operating Systems

CSE 153 Design of Operating Systems Fall 2018

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

Operating System Concepts Ch. 5: Scheduling

Architectural Support for Operating Systems

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

CSE 153 Design of Operating Systems

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

Objectives. Making TOS preemptive Avoiding race conditions

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

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

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

Threads (light weight processes) Chester Rebeiro IIT Madras

Threads and Concurrency

Threads and Concurrency

Announcements/Reminders

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru Department of Electronics and Communication Engineering

EECS 482 Introduction to Operating Systems

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

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

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

Announcements. Reading. Project #1 due in 1 week at 5:00 pm Scheduling Chapter 6 (6 th ed) or Chapter 5 (8 th ed) CMSC 412 S14 (lect 5)

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

Preemptive Scheduling and Mutual Exclusion with Hardware Support

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

Processes and More. CSCI 315 Operating Systems Design Department of Computer Science

Problem Set 2. CS347: Operating Systems

Process Description and Control

CSC 4320 Test 1 Spring 2017

Review: Easy Piece 1

Process Characteristics. Threads Chapter 4. Process Characteristics. Multithreading vs. Single threading

Threads Chapter 4. Reading: 4.1,4.4, 4.5

Jan 20, 2005 Lecture 2: Multiprogramming OS

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

Processes and Threads

Processes and Threads

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Threads Chapter 5 1 Chapter 5

Inter-Process Communication and Synchronization of Processes, Threads and Tasks: Lesson-1: PROCESS

Subject: Operating System (BTCOC403) Class: S.Y.B.Tech. (Computer Engineering)

Operating Systems and Protection CS 217

Chapter 4: Multithreaded Programming

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Major Requirements of an OS

Chap 4, 5: Process. Dongkun Shin, SKKU

KERNEL THREAD IMPLEMENTATION DETAILS. CS124 Operating Systems Winter , Lecture 9

CSCE 313: Intro to Computer Systems

Sistemi in Tempo Reale

Chapter 3: Processes. Operating System Concepts 8 th Edition,

Processes. CS3026 Operating Systems Lecture 05

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

Process- Concept &Process Scheduling OPERATING SYSTEMS

The Kernel Abstraction. Chapter 2 OSPP Part I

Operating Systems. Computer Science & Information Technology (CS) Rank under AIR 100

1 PROCESSES PROCESS CONCEPT The Process Process State Process Control Block 5

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

Transcription:

Today s Topics COS 318: Operating Systems Implementing Threads u Thread implementation l Non-preemptive versus preemptive threads l Kernel vs. user threads Jaswinder Pal Singh and a Fabulous Course Staff Computer Science Department Princeton University (http://www.cs.princeton.edu/courses/cos318/) 2 Revisit Monolithic OS Structure Thread context switch u Kernel consists of l Boot loader l BIOS l Key drivers l Threads l Scheduler l u Scheduler l Use a ready queue to hold all ready threads l Schedule in a thread in the same address space (thread context switch) l Schedule in a thread with a different address space (process context switch) Kernel scheduler u Scheduler schedules threads on context switch u Voluntary l Thread yields or blocks, e.g. for a resource like disk, a synchronization variable etc l Thread_join (wait for a target process, e.g. child, to terminate) u Involuntary l Interrupt or exception l Some other thread of higher priority needs to run 3 1

Non-Preemptive Scheduling Non-Preemptive Scheduling (contd.) Create Scheduler dispatch Ready Yield Terminate Running Block for resource Blocked Exited u A non-preemptive scheduler invoked by thread calling a yield, block, join or similar u Simplest form of scheduler: When invoked: save current process/thread state choose next process/thread to run dispatch (load PCB/TCB and jump to it) u Scheduler can be viewed as just another kernel thread Resource becomes available (move to ready queue) 5 6 Thread Context u Can be classified into two types: l Private l Shared u Shared state l Contents of memory (global variables, heap) l File system u Private state l Program counter l Registers l Stack Where and How to Save Thread Context? u Save the context on the thread s stack l Many processors have a special instruction to do it efficiently l But, need to deal with the overflow problem Thread 2 Thread 1 u Check before saving Save the context of Thread 1 to its stack l Make sure that the stack has no overflow problem l Copy it to the TCB residing in the kernel heap l Not so efficient, but no overflow problems Context 8 2

Thread Control Block (TCB) l Current state Ready: ready to run Running: currently running Blocked: waiting for resources l Registers l Status (EFLAGS) l Program counter (EIP) l Stack Voluntary thread context switch u Save registers on old stack u Switch to new stack, new thread u Restore registers from new stack u Return u Exactly the same with kernel threads or user threads // We enter as oldthread, but we return as newthread. // Returns with newthread's registers and stack. void thread_switch(oldthreadtcb, newthreadtcb) { pushad; // Push general register values onto the old stack. oldthreadtcb->sp = %esp; // Save the old thread's stack pointer. %esp = newthreadtcb->sp; // Switch to the new stack. popad; // Pop register values from the new stack. return; } 10 Preemption Recall: Non-Preemptive Scheduling u Why? l Timer interrupt for CPU management l Asynchronous I/O completion CPU Terminate Exited u When is CPU interrupted? l Between instructions l Within an instruction, except atomic ones u Manipulate interrupts l Disable (mask) interrupts l Enable interrupts l Non-Maskable Interrupts Memory Interrupt Create Scheduler dispatch Ready Running Yield Resource becomes available (move to ready queue) Block for resource Blocked 12 13 3

State Transitions for Preemptive Scheduling Interrupt Handling for Preemptive Scheduling Create Scheduler dispatch Ready Terminate Running Yield, Interrupt Block for resource Blocked Exited Resource becomes available, I/O completion interrupt (move to ready queue) u Timer interrupt handler: l Save the current process / thread to its PCB / TCB l Call scheduler u I/O interrupt handler: l Save the current process / thread to its PCB / TCB l Do the I/O job l Call scheduler u Issues l Disable/enable interrupts l Make sure that it works on multiprocessors 14 15 - and Kernel-level Threads -level Threads u Managed by user-level runtime software, run in user mode u Kernel knows only about user processes, not user threads u Invoking thread API leads to userlevel function call u Context switch at user-level u Preemption? Kernel scheduler u Threads at user level (in user space, user mode) and at kernel level u level threads map to kernel level threads, which are all u Fast (could be as fast as function call) u Can have custom user-level schedulers u Lower kernel complexity u Can implement on kernels that are single-threaded u Extreme case: kernel is single-threaded the operating system really knows about 17 18 4

Kernel Threads Disadvantages of and Kernel Threads u Managed by OS, run in kernel mode u Invoking thread API causes system call u Context switch invokes OS u PCB per process and TCB per thread in kernel u Kernel has knowledge of threads so can optimize better l E.g. give more CPU time to processes with more threads u When one thread in a process blocks, others can still run l Good for cases where threads block frequently u Extreme case: one kernel thread per user thread, so no need to for user thread mgmt 19 u threads l When a user-level thread is blocked on an I/O event, the whole process is blocked Precisely the case for which threads are often useful l Kernel may not be able to schedule processes optimally May schedule process with idle threads May not give more CPU to processes with many threads May need OS modifications or other mechanisms to solve l Multiprocessor or multi-core systems need at least one kernel thread per processor/core, so hard to do only user-level u Kernel threads l Thread context switches and thread operations more expensive (cross OS boundary) l More complexity in kernel 20 Implementation Models for -level Threads u threads are mapped to kernel threads l Can think of them as a kernel thread per virtual processor l (hence need at least one kernel-level thread per core) u Simplest case, discussed so far, is many to 1 l Only one user-level thread runs at a time, since only one kernel thread l Case of kernel level threads so far can be viewed as 1:1 u Other models exist l m user threads mapped to n kernel threads l certain user level threads bound to a subset of kernel threads l Dynamically change-able no. of kernel threads for user process (but needs more communication mechanisms up/down), etc. Summary of vs. Kernel Threads u Kernel-threads l Kernel-level threads are scheduled by a kernel scheduler l A context switch of kernel-threads is more expensive than user threads due to crossing protection boundaries l Pure user-level threads hard to make work on multiprocessor: need multiple kernel-level threads (virtual processors) u Hybrid models exist, but are complicated l E.g. using kernel level threads can be 1-1 for user-to-kernel level thread mapping, using user-level threads is many-to-1, and there are many-to-many alternatives as well 21 22 5

Interactions between and Kernel Threads u Every thread has its own user stack. What about kernel stack? Two possibilities: l Every user thread has its own kernel stack l All threads of a process share the same kernel stack Private kernel stack Shared kernel stack Summary u Non-preemptive threads issues l Scheduler l Where to save contexts u Preemptive threads l Interrupts can happen any where! u Kernel vs. user threads Memory usage More Less System services Concurrent access Serial access Multiprocessor Yes Not within a process Complexity More Less 23 24 6