Threads, Events, and Scheduling Andy Wang COP 5611 Advanced Operating Systems.

Slides:



Advertisements
Similar presentations
CS 149: Operating Systems February 3 Class Meeting
Advertisements

CPU Scheduling CPU Scheduler Performance metrics for CPU scheduling
Chapter 5 CPU Scheduling. CPU Scheduling Topics: Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling.
Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation.
Scheduling in Batch Systems
Precept 3 COS 461. Concurrency is Useful Multi Processor/Core Multiple Inputs Don’t wait on slow devices.
OS Spring ’ 04 Scheduling Operating Systems Spring 2004.
Review: Process Communication Sequential Communication –Result of P1 becomes at termination the input to P2 –Tools: Redirect (>,>>) and Pipe (|) Concurrent.
1 Thursday, June 15, 2006 Confucius says: He who play in root, eventually kill tree.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
5: CPU-Scheduling1 Jerry Breecher OPERATING SYSTEMS SCHEDULING.
Wk 2 – Scheduling 1 CS502 Spring 2006 Scheduling The art and science of allocating the CPU and other resources to processes.
Why Threads Are A Bad Idea (for most purposes) John Ousterhout Sun Microsystems Laboratories
Scheduler Activations Jeff Chase. Threads in a Process Threads are useful at user-level – Parallelism, hide I/O latency, interactivity Option A (early.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 5 Operating Systems.
OPERATING SYSTEMS CPU SCHEDULING.  Introduction to CPU scheduling Introduction to CPU scheduling  Dispatcher Dispatcher  Terms used in CPU scheduling.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
1 Previous lecture review n Out of basic scheduling techniques none is a clear winner: u FCFS - simple but unfair u RR - more overhead than FCFS may not.
1 Scheduling Processes. 2 Processes Each process has state, that includes its text and data, procedure call stack, etc. This state resides in memory.
Chapter 5 – CPU Scheduling (Pgs 183 – 218). CPU Scheduling  Goal: To get as much done as possible  How: By never letting the CPU sit "idle" and not.
CS 153 Design of Operating Systems Spring 2015 Lecture 11: Scheduling & Deadlock.
Concurrency: Threads, Address Spaces, and Processes Andy Wang Operating Systems COP 4610 / CGS 5765.
Scheduling Basic scheduling policies, for OS schedulers (threads, tasks, processes) or thread library schedulers Review of Context Switching overheads.
Operating Systems Lecture Notes CPU Scheduling Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Thread Scheduling.
1 CSE451 Scheduling Autumn 2002 Gary Kimura Lecture #6 October 11, 2002.
1 Scheduling The part of the OS that makes the choice of which process to run next is called the scheduler and the algorithm it uses is called the scheduling.
Scheduling Lecture 6. What is Scheduling? An O/S often has many pending tasks. –Threads, async callbacks, device input. The order may matter. –Policy,
Cpr E 308 Spring 2005 Process Scheduling Basic Question: Which process goes next? Personal Computers –Few processes, interactive, low response time Batch.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
ITFN 2601 Introduction to Operating Systems Lecture 4 Scheduling.
CSE 60641: Operating Systems Next topic: CPU (Process/threads/scheduling, synchronization and deadlocks) –Why threads are a bad idea (for most purposes).
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
CS533 - Concepts of Operating Systems 1 Threads, Events, and Reactive Objects - Alan West.
Lecture Topics: 11/15 CPU scheduling: –Scheduling goals and algorithms.
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
1 CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling.
Chapter 4 CPU Scheduling. 2 Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation.
Lecture 4 CPU scheduling. Basic Concepts Single Process  one process at a time Maximum CPU utilization obtained with multiprogramming CPU idle :waiting.
Process Scheduling. Scheduling Strategies Scheduling strategies can broadly fall into two categories  Co-operative scheduling is where the currently.
CPU scheduling.  Single Process  one process at a time  Maximum CPU utilization obtained with multiprogramming  CPU idle :waiting time is wasted 2.
Lecturer 5: Process Scheduling Process Scheduling  Criteria & Objectives Types of Scheduling  Long term  Medium term  Short term CPU Scheduling Algorithms.
CPU Scheduling Andy Wang Operating Systems COP 4610 / CGS 5765.
Processes and Threads Processes and their scheduling
Process Management Presented By Aditya Gupta Assistant Professor
April 6, 2001 Gary Kimura Lecture #6 April 6, 2001
Chapter 2 Scheduling.
Chapter 8 – Processor Scheduling
Threads, Events, and Scheduling
Chapter 6: CPU Scheduling
Process management Information maintained by OS for process management
Chapter 6: CPU Scheduling
Chapter 5: CPU Scheduling
Andy Wang Operating Systems COP 4610 / CGS 5765
Operating System Concepts
CPU SCHEDULING.
CPU scheduling decisions may take place when a process:
Threads, Events, and Scheduling
CS703 – Advanced Operating Systems
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Threads, Events, and Scheduling
Thomas E. Anderson, Brian N. Bershad,
Why Threads Are A Bad Idea (for most purposes)
Why Threads Are A Bad Idea (for most purposes)
Why Threads Are A Bad Idea (for most purposes)
CSE 153 Design of Operating Systems Winter 2019
Don Porter Portions courtesy Emmett Witchel
Concurrency: Threads, Address Spaces, and Processes
Presentation transcript:

Threads, Events, and Scheduling Andy Wang COP 5611 Advanced Operating Systems

Basic Concept of Threads/Processes Thread: A sequential execution stream Address space: Chunks of memory and everything needed to run a program Process: An address space + thread(s) Two types of threads  Kernel threads  User-level threads

Kernel vs. User Threads kernel user kernel threads processes user threads OS scheduler only knows about kernel threads

Characteristics of User Threads User threads + Good performance Scheduling involves voluntary yields of processors - Sometimes incorrect behaviors A thread blocked on I/O may prevent other ready threads from running Kernel knows nothing about the priorities among threads  A low-priority thread may preempt a high-priority thread

Characteristics of Kernel Threads Kernel threads (each user thread mapped to a kernel thread) + Correct concurrency semantics - Poor performance Scheduling involves kernel crossing

One Solution: Scheduler Activations Additional interface  Thread system can request kernel threads dynamically  Thread system can advice kernel scheduler on preemptions  Kernel needs to notify the thread system of various events (e.g., blocking) via upcalls  Kernel needs to make a kernel thread available to activate user-level scheduler

Why Threads Are A Bad Idea (for most purposes) by John Ousterhout Threads  Grew up in OS world (processes)  Every programmer should be a threads programmer? Problem: threads are very hard to program. Alternative: events Claims:  For most purposes, events are better.  Threads should be used only when true CPU concurrency is needed

What Are Threads? General-purpose solution for managing concurrency Multiple independent execution streams Shared state Pre-emptive scheduling Synchronization (e.g. locks, conditions) Shared state (memory, files, etc.) Threads

What Are Threads Used For? Operating systems: one kernel thread for each user process Scientific applications: one thread per CPU Distributed systems: process requests concurrently (overlap I/Os) GUIs:  Threads correspond to user actions; can service display during long-running computations  Multimedia, animations

What's Wrong With Threads? Too hard for most programmers to use Even for experts, development is painful casualwizards all programmers Visual Basic programmers C programmers C++ programmers Threads programmers

Why Threads Are Hard Synchronization:  Must coordinate access to shared data with locks  Forget a lock? Corrupted data Deadlock:  Circular dependencies among locks.  Each process waits for some other process: system hangs. lock Alock B thread 1thread 2

Why Threads Are Hard, cont'd Hard to debug: data and timing dependencies Threads break abstraction: can't design modules independently Callbacks don't work with locks Module A Module B T1T2 sleepwakeup deadlock! Module A Module B T1 T2 deadlock! callbacks calls

Why Threads Are Hard, cont'd Achieving good performance is hard:  Simple locking yields low concurrency  Fine-grain locking reduces performance  OSes limit performance (context switches) Threads not well supported:  Hard to port threaded code (PCs? Macs?)  Standard libraries not thread-safe  Kernel calls, window systems not multi- threaded  Few debugging tools (LockLint, debuggers?)

Event-Driven Programming One execution stream: no CPU concurrency Register interest in events (callbacks) Event loop waits for events, invokes handlers No preemption of event handlers Handlers generally short-lived Event Loop Event Handlers

What Are Events Used For? Mostly GUIs:  One handler for each event (press button)  Handler implements behavior (undo, delete file, etc.) Distributed systems:  One handler for each source of input (i.e., socket)  Handler processes incoming request, sends response  Event-driven I/O for I/O overlap

Problems With Events Long-running handlers make application non-responsive  Fork off subprocesses for long-running things (e.g., multimedia), use events to find out when done  Break up handlers (e.g. event-driven I/O)  Periodically call event loop in handler (reentrancy adds complexity) Can't maintain local state across events (handler must return)

Problems With Events No CPU concurrency (not suitable for scientific apps) Event-driven I/O not always well supported (e.g. poor write buffering).

Events vs. Threads Events avoid concurrency as much as possible:  Easy to get started with events: no concurrency, no preemption, no synchronization, no deadlock  Use complicated techniques only for unusual cases  With threads, even the simplest application faces the full complexity

Events vs. Threads Debugging easier with events:  Timing dependencies only related to events, not to internal scheduling  Problems easier to track down: slow response to button vs. corrupted memory

Events vs. Threads, cont'd Events faster than threads on single CPU:  No locking overheads  No context switching Events more portable than threads Threads provide true concurrency:  Can have long-running stateful handlers without freezes  Scalable performance on multiple CPUs

Should You Abandon Threads? No: important for high-end servers But, avoid threads wherever possible:  Use events, not threads, for GUIs, distributed systems, low-end servers  Only use threads where true CPU concurrency is needed  Where threads needed, isolate usage in threaded application kernel: keep most of code single-threaded Threaded Kernel Event-Driven Handlers

Summary Concurrency is fundamentally hard; avoid whenever possible Threads more powerful than events, but power is rarely needed Threads are for experts only Use events as primary development tool (both GUIs and distributed systems) Use threads only for performance-critical kernels

Process Scheduling Goals  Low latency  High throughput  Fairness

Basic Scheduling Approaches FIFO + Fair - High latency Round robin + fair + low latency - poor throughput

Basic Scheduling Approaches STCF/SRTCF (shortest time/remaining time to completion first) + low latency + high throughput - unfair

Basic Scheduling Approaches Multilevel feedback queues  A job starts with the highest priority queue  If time slice expires, lower the priority by one level  If time slice does not expire, raise the priority by one level  Age long-running jobs

Lottery Scheduling Claim  Priority-based schemes are ad hoc Lottery scheduling  Randomized scheme  Based on a currency abstraction  Idea: Processes own lottery tickets CPU randomly draws a ticket and execute the corresponding process

Properties of Lottery Scheduling Guarantees fairness through probability Guarantees no starvation, as long as each process owns one ticket To approximate SRTCF  Short jobs get more tickets  Long jobs get fewer

Examples Each short job gets 10 tickets Each long job gets 1 ticket Suppose we have the following scenarios: # short jobs/ # long jobs% of CPU for each short job% of CPU for each long job 1/191%9% 0/2N/A50% 2/050%N/A 10/110%1% 1/1050%5%

Partially Consumed Tickets What if a process is chosen, but it does not consume the entire time slice?  The process receives compensation tickets  Idea Get chosen more frequently But with shorter time slice

Ticket Currencies Load Insulation  A process can dynamically change its ticketing policies without affecting other processes Need to convert currencies before transferring tickets base: Alice: process1: thread1 300 thread Bob: process2: thread3