Concurrency, Threads, and Events Ken Birman (Based on a slide set prepared by Robbert van Renesse)

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

Concurrency, Thread and Event CS6410 Sept 6, 2011 Ji-Yong Shin.
Today From threads to file systems
6/9/2015B.Ramamurthy1 Process Description and Control B.Ramamurthy.
CS533 Concepts of Operating Systems Class 3 Monitors.
Process Description and Control
Review: Process Management Objective: –Enable fair multi-user, multiprocess computing on limited physical resources –Security and efficiency Process: running.
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Scheduler Activations Effective Kernel Support for the User-Level Management of Parallelism.
Concurrency CS 510: Programming Languages David Walker.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
3.5 Interprocess Communication
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Threads CSCI 444/544 Operating Systems Fall 2008.
Concurrency, Threads, and Events Robbert van Renesse.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
G Robert Grimm New York University Scheduler Activations.
CS 3013 & CS 502 Summer 2006 Threads1 CS-3013 & CS-502 Summer 2006.
Why Threads Are A Bad Idea (for most purposes) John Ousterhout Sun Microsystems Laboratories
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.
Operating Systems CSE 411 CPU Management Sept Lecture 11 Instructor: Bhuvan Urgaonkar.
Lecture 4: Parallel Programming Models. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
COP 4600 Operating Systems Spring 2011 Dan C. Marinescu Office: HEC 304 Office hours: Tu-Th 5:00-6:00 PM.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Thread Scheduling.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Scheduling Lecture 6. What is Scheduling? An O/S often has many pending tasks. –Threads, async callbacks, device input. The order may matter. –Policy,
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
CSE 451: Operating Systems Winter 2015 Module 5 1 / 2 User-Level Threads & Scheduler Activations Mark Zbikowski 476 Allen Center.
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.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Processes & Threads Introduction to Operating Systems: Module 5.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
Threads versus Events CSE451 Andrew Whitaker. This Class Threads vs. events is an ongoing debate  So, neat-and-tidy answers aren’t necessarily available.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
Operating System Concepts
Where Testing Fails …. Problem Areas Stack Overflow Race Conditions Deadlock Timing Reentrancy.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Overview: Using Hardware.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
Threads prepared and instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University 1July 2016Processes.
Threads vs. Events SEDA – An Event Model 5204 – Operating Systems.
Concurrency, threads, and events
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Process Description and Control
Process Description and Control
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Process Description and Control
Process Description and Control
Why Threads Are A Bad Idea (for most purposes)
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
CS510 Operating System Foundations
Why Threads Are A Bad Idea (for most purposes)
Why Threads Are A Bad Idea (for most purposes)
CS703 – Advanced Operating Systems
CSE 153 Design of Operating Systems Winter 2019
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

Concurrency, Threads, and Events Ken Birman (Based on a slide set prepared by Robbert van Renesse)

Summary Paper 1 Using Threads in Interactive Systems: A Case Study (Hauser et al 1993) Analyzes two interactive computing systems Classifies thread usage Finds that programmers are still struggling (pre-Java) Limited scheduling support Priority-inversion

Summary Paper 2 SEDA: An Architecture for Well- Conditioned, Scalable Internet Services (Welsh, 2001) Analyzes threads vs event-based systems, finds problems with both Suggests trade-off: stage-driven architecture Evaluated for two applications Easy to program and performs well

What is a thread? A traditional “process” is an address space and a thread of control. Now add multiple thread of controls Share address space Individual program counters and stacks Same as multiple processes sharing an address space.

Thread Switching To switch from thread T1 to T2: Thread T1 saves its registers (including pc) on its stack Scheduler remembers T1’s stack pointer Scheduler restores T2’ stack pointer T2 restores its registers T2 resumes Two models: preemptive/non-preemptive

Thread Scheduler Maintains the stack pointer of each thread Decides what thread to run next E.g., based on priority or resource usage Decides when to pre-empt a running thread E.g., based on a timer May need to deal with multiple CPUs But not usually “fork” creates a new thread Blocking or calling “yield” lets scheduler run

Synchronization Primitives Semaphores P(S): block if semaphore is “taken” V(S): release semaphore Monitors: Only one thread active in a module at a time Threads can block waiting for some condition using the WAIT primitive Threads need to signal using NOTIFY or BROADCAST

Uses of threads To exploit CPU parallelism Run two CPUs at once in the same program To exploit I/O parallelism Run I/O while computing, or do multiple I/O Listen to the “window” while also running code, e.g. allow commands during an interactive game For program structuring E.g., timers To avoid deadlock in RPC-based applications

Hauser’s categorization Defer Work: asynchronous activity Print, , create new window, etc. Pumps: pipeline components Wait on input queue; send to output queue E.g., slack process: add latency for buffering Sleepers & one-shots Periodic activity & timers

Categorization, cont’d Deadlock Avoiders Avoid deadlock through ordered acquisition of locks When needing more locks, roll-back and re-acquire Task Rejuvenation: recovery Start new thread when old one dies, say because of uncaught exception

Categorization, cont’d Serializers: event loop for (;;) { get_next_event(); handle_event(); } Concurrency Exploiters Use multiple CPUs Encapsulated Forks Hidden threads used in library packages E.g., menu-button queue

Common Problems Priority Inversion High priority thread waits for low priority thread Solution: temporarily push priority up (rejected??) Deadlock X waits for Y, Y waits for X Incorrect Synchronization Forgetting to release a lock Failed “fork” Tuning E.g. timer values in different environment

Problems he neglects Implicit need for ordering of events E.g. thread A is supposed to run before thread B does, but something delays A Non-reentrant code Languages lack “monitor” features and users are perhaps surprisingly weak at detecting and protecting concurrently accessed data

Criticism of Hauser He assumes superb programmers and seems to believe that “most” programmers won’t use threads (his example systems are really platforms, not applications) Systems old but/and not representative Pre-Java and C# And now there are some tools that can help discover problems

What is an Event? An object queued for some module Operations: create_event_queue(handler)  EQ enqueue_event(EQ, event-object) Invokes, eventually, handler(event-object) Handler is not allowed to block Blocking could cause entire system to block But page faults, garbage collection, …

Example Event System (Also common in telecommunications industry, where it’s called “workflow programming”)

Event Scheduler Decides which event queue to handle next. Based on priority, CPU usage, etc. Never pre-empts event handlers! No need for stack / event handler May need to deal with multiple CPUs

Synchronization? Handlers cannot block  no synchronization Handlers should not share memory At least not in parallel All communication through events

Uses of Events CPU parallelism Different handlers on different CPUs I/O concurrency Completion of I/O signaled by event Other activities can happen in parallel Program structuring Not so great… But can use multiple programming languages!

Hauser’s categorization ?! Defer Work: asynchronous activity Send event to printer, etc Pumps: pipeline components Natural use of events! Sleepers & one-shots Periodic events & timer events

Categorization, cont’d Deadlock Avoiders Ordered lock acquisition still works Task Rejuvenation: recovery Watchdog events?

Categorization, cont’d Serializers: event loop Natural use of events and handlers! Concurrency Exploiters Use multiple CPUs Encapsulated Events Hidden events used in library packages E.g., menu-button queue

Common Problems Priority inversion, deadlock, etc. much the same with events

Threaded Server Throughput

Event-driven Server Throughput

Threads vs. Events Events-based systems use fewer resources Better performance (particularly scalability) Event-based systems harder to program Have to avoid blocking at all cost Block-structured programming doesn’t work How to do exception handling? In both cases, tuning is difficult

Both? In practice, many kinds of systems need to support both threads and events Threaded programs in Unix are the common example of these, because window systems use events The programmer uses cthreads or pthreads Major problem: the UNIX kernel interface wasn’t designed with threads in mind!

Why does this cause problems? Many system calls block the “process” File read or write, for example And many libraries aren’t reentrant So when the user employs threads The application may block unexpectedly Limited work-around: add “kernel threads” And the user might stumble into a reentrancy bug

Events as seen in Unix Window systems use small messages… But the “old” form of events are signals Kernel basically simulates an interrupt into the user’s address space The “event handler” then runs… But can it launch new threads? Some system calls can return EINTR Very limited options to “block” signals in critical sections

How people work around this? They try not to do blocking I/O Use asynchronous system calls… or select… or some mixture of the two Or try to turn the whole application into an event-driven one using pools of threads, in the SEDA model (more or less) One dedicated thread per I/O “channel”, to turn signal-style events into events on the event queue for the processing stage

This can be hard, but it works Must write the whole program and have a way to review any libraries it uses! One learns, the hard way, that pretty much nothing else works Unix programs built by inexperienced developers are often riddled with concurrency bugs!

SEDA Mixture of models of threads and (small message-style) events Events, queues, and “pools of event handling threads”. Pools can be dynamically adjusted as need arises. Similar to Javabeans and EventListeners?

SEDA Stage

Authors: “Best of both worlds” Ease of programming of threads Or even better Performance of events Or even better

Threads Considered Harmful Like goto, transfer to some entry in program In any scope Destroys structure of programs Primitive Synchronization Primitives Too low-level Too coarse-grained Too error-prone Prone to over-specification

Example: create file 1. Create file 2. Read current directory (may be cached) 3. Update and write back directory 4. Write file

Thread Implementations 1. Serialize: op1; op2; op3; op4 Simplest and most common 2. Use threads Requires at least two semaphores! Results in complicated program 3. Simplified threads a) Create file and read directory in parallel b) Barrier c) Write file and write directory in parallel Over-specification!

Event Implementation Create a dummy handler that awaits file creation and directory read events and then send an event to update the directory. Not great…

GOP: Discussion Specifies dependencies at a high-level No semaphores, condition variables, etc No explicit threads nor events Can easily be supported by many languages C, Java, etc. Top-down specification cmp with make, prolog, theorem prover Exception handling easily supported

Conclusion Threads still problematic As a code structuring mechanism High resource usage Events also problematic Hard to code, but efficient SEDA and GOP address shortcomings But neither can be said to have taken hold

Issues not discussed Kernel vs. User-level implementation Shared memory and protection tradeoffs Problems seen in demanding applications that may launch enormous numbers of threads