E81 CSE 532S: Advanced Multi-Paradigm Software Development Venkita Subramonian, Christopher Gill, Guandong Wang, Zhenning Hu, Zhenghui Xie Department of.

Slides:



Advertisements
Similar presentations
CS 443 Advanced OS Fabián E. Bustamante, Spring 2005 Resource Containers: A new Facility for Resource Management in Server Systems G. Banga, P. Druschel,
Advertisements

Chapter 5 Threads os5.
Seyed Mohammad Ghaffarian ( ) Computer Engineering Department Amirkabir University of Technology Fall 2010.
Lecture 23: Software Architectures
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Server Architecture Models Operating Systems Hebrew University Spring 2004.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
3.5 Interprocess Communication
Threads CSCI 444/544 Operating Systems Fall 2008.
Concurrency, Threads, and Events Robbert van Renesse.
1 I/O Management in Representative Operating Systems.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Software Patterns - F04 Asynchronous Completion Token 1.
Client/Server Software Architectures Yonglei Tao.
E81 CSE 532S: Advanced Multi-Paradigm Software Development Chris Gill Department of Computer Science Washington University, St. Louis
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
Institute of Computer and Communication Network Engineering OFC/NFOEC, 6-10 March 2011, Los Angeles, CA Lessons Learned From Implementing a Path Computation.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
Pattern Oriented Software Architecture for Networked Objects Based on the book By Douglas Schmidt Michael Stal Hans Roehnert Frank Buschmann.
Operating Systems Lecture 2 Processes and Threads Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
1 Chapter 2: Computer-System Structures  Computer System Operation  I/O Structure  Storage Structure  Storage Hierarchy  Hardware Protection  General.
Track 1: Summary Slides for POSA2 Patterns and Frameworks Distributed Real-Time Systems (TI-DRTS) Version:
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Proactor Pattern Venkita Subramonian & Christopher Gill
Explore Patterns in Context-Aware Applications --Using Reactor Pattern to Develop In/Out Board Fall 2002 Yu Du.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Chapter 38 Persistence Framework with Patterns 1CS6359 Fall 2011 John Cole.
ACE Tutorial Laura A. Paterno Software Workshop 20 January 1998.
E81 CSE 532S: Advanced Multi-Paradigm Software Development Chris Gill Department of Computer Science and Engineering Washington University, St. Louis
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 13. Review Shared Data Software Architectures – Black board Style architecture.
E81 CSE 532S: Advanced Multi-Paradigm Software Development Christopher Gill and Venkita Subramonian Department of Computer Science and Engineering Washington.
Architectural pattern: Reactor Source: POSA II pp 179 – 214POSA II Environment: an application that receives multiple requests simultaneously but may process.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Interceptor CS562 Spring 2002 Jan Anand Krishnan Morgan Deters Venkita Subramonian.
E81 CSE 532S: Advanced Multi-Paradigm Software Development Venkita Subramonian, Christopher Gill, Ying Huang, Marc Sentany Department of Computer Science.
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
E81 CSE 532S: Advanced Multi-Paradigm Software Development Venkita Subramonian, Christopher Gill, Huang-Ming Huang, Shih-Ling Chen, Sajeeva Pallemulle.
Thread-Specific Storage (TSS) Chris Gill and Venkita Subramonian E81 CSE 532S: Advanced Multi-Paradigm Software Development.
SPL/2010 Reactor Design Pattern 1. SPL/2010 Overview ● blocking sockets - impact on server scalability. ● non-blocking IO in Java - java.niopackage ●
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Benefits & Limitations of Patterns & Frameworks: Part 1 Douglas C. Schmidt Professor of Computer.
Chapter 4 – Thread Concepts
Introduction to threads
Software Design Refinement Using Design Patterns
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Event Handling Patterns Asynchronous Completion Token
Chapter 4 – Thread Concepts
Instructor: Dr. Hany H. Ammar
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
The Active Object Pattern
Module 2: Computer-System Structures
Distributed Computing Systems
Half-Sync/Half-Async (HSHA) and Leader/Followers (LF) Patterns
Monitor Object Pattern
Chapter 4: Threads.
Module 2: Computer-System Structures
Chapter 4: Threads.
CS703 – Advanced Operating Systems
Chapter 13: I/O Systems.
Presentation transcript:

E81 CSE 532S: Advanced Multi-Paradigm Software Development Venkita Subramonian, Christopher Gill, Guandong Wang, Zhenning Hu, Zhenghui Xie Department of Computer Science and Engineering Washington University, St. Louis Reactor Pattern

Motivating Example: A Logging Server From

Evolving to Concurrent Event Handling Logging Server CONNECT Client1 Port:27098 Client2 Port:26545 CONNECT Goal: process multiple service requests concurrently Port:30000 listen Port:24467 accept Port:25667 accept

Where We’re Starting main() { bind listening port; listen for (;;) { new_conn_socket = accept (); run(handler(new_conn_socket)); // write, read }

Logging Server: Threaded Approach main() { bind listening port; listen for (;;) { new_conn_socket = accept (); // fork a process or spawn a thread for handler thread.run(handler(new_conn_socket)); }

Problems with Threaded Approach Multi-threading may increase code complexity Multi-threading/processing adds overhead –Context switching (especially among processes) –Synchronization for shared data, other resources What if we could make 1 thread responsive? –Better resource utilization by aligning threading strategy to # of available resources (like CPUs) –Also, multi-threading may not be available in all OS platforms (e.g., embedded ones)

Alternative: Event Driven Server handle_connection_request handle_data_read Connection Acceptor Data Reader Event Handlers Inversion of control Hollywood principle – Don’t call us, we’ll call you (“there is no main”) (reusable: e.g., from ACE) Event Dispatching Logic (pluggable: you write for your application) Event Handling Logic

Reactor Pattern (Dispatching Logic) An architectural pattern –Context: event-driven application –Concurrent reception of multiple service requests, but serial processing of each one Dispatch service requests –Calls the appropriate event handler Also known as –Dispatcher, Notifier, Selector (see Java NIO)

Design Forces Enhance scalability Maximize throughput Minimize latency Reduce effort that is needed to integrate new services into server

Solution – Separation of Concerns Application Reactor Event Handlers Event sources De-multiplexing & Dispatching Application logic Synchronous wait Serial Dispatching

Reactor Pattern Structure From a.k.a “the reactor”

Synchronous vs. Reactive Read read() ClientsServer select() ClientsServer read() data HandleSet

Serial Event Dispatching select() Clients Application Event Handlers read() Reactor handle_*() HandleSet

Interactions among Participants Main Program Concrete Event Handler Reactor Synchronous Event Demultiplexer register_handler(handler, event_types) get_handle() handle_events() select() event handle_event()

Implementation De-multiplexer/dispatcher infrastructure –Anonymous de-multiplexing of events to handlers –Assumes specific event handler hook methods Application –Defines concrete event handlers –Handlers perform service-specific processing (“Service Handlers”)

Event Handler Interface Determine type of dispatching target –Objects vs. functions –Can have pointers to either –Command pattern can unify these –E.g., handle_event () Event handling dispatch interface strategy –Single-method dispatch handle_event (handle, event_type) –Multi-method dispatch handle_input (handle) handle_output (handle) handle_timeout (handle) Note: singular, not plural

Reactor Interface Handler registration/deregistration –E.g., register_handler() deregister_handler() Consider visitor, observer patterns Event loop –E.g., handle_events() Note: plural, not singular

Reactor Implementation Reactor implementation hierarchy –Abstract base class or template concept –Concrete platform-specific implementations Synchronous event de-multiplexing mechanism –E.g., WaitForMultipleObjects() on Win32 –E.g., select() or poll() on UNIX platforms Implement a dispatch table Complete concrete reactor implementation –Hook dispatch table into de-mux mechanism

Multiple Reactors A single reactor instance will work in most cases –Sometimes desirable, e.g., for handler serialization –Can use Singleton (e.g., ACE_Reactor::instance() ) Limits on number of OS handles may restrict this –Total available (rarely an issue in a general-purpose OS) –Max a single thread can wait for E.g., 64 in some Win32 platforms –May need multiple reactors, each with its own thread –Note that handlers are not serialized across Reactor instances – treat remote/concurrent reactors similarly

Concrete Event Handlers Implement base interface / model concept Determine policies for handler state –Stateless, stateful, or a combination ACTs (cookies) can help offload some of the state I.e., can keep state outside the handler objects, but index into a data structure, etc. using the ACT Implement event handler functionality –I.e., add application logic to handler methods

Example Resolved: Part 1 Steps performed when a client connects to the logging server From a.k.a. “the reactor”

Example Resolved: Part 2 Steps performed by reactive logging server to for each record From a.k.a. “the reactor”

Variant: Integrated De-multiplexing of Timer and I/O Events Timer-based and I/O-based events in same reactor Extend reactors and event handlers –Register concrete event handlers for some time trigger Relative vs. absolute time triggers Periodic vs. one time invocation –Reactor calls handler’s handle_timeout() method Can use same handler for time and event dispatching E.g., an alert watchdog timer for some logging handler –Various timer strategies E.g., select/WFMO timeout E.g., hardware timer interrupt E.g., polling Pentium tick counter Key trade-offs between portability, overhead and responsiveness

Variant: Re-entrant Reactors Event handlers re-invoke reactor->handle_events() –Result: nested event handlers –E.g., CORBA AMI  nested work_pending() Reactor implementation must be re-entrant –Copy the handle set state onto the run-time stack –Any changes to handle set are local to that nesting level of the reactor –Use thread stack frame to record reactor’s logical “stack frame”

Variant: Thread-Safe Reactor Synchronized reactor –Lock to synchronize access to the reactor’s internal state Multiple threads could register/remove event handlers –Preventing self-deadlock An event handler could register/remove other event handlers or itself –Explicitly notifying a waiting event loop thread Notify the reactor of a change so that the wait handle- set could be updated

Variant: Concurrent Event Handlers Event handlers with their own threads –In addition to event loop thread(s) Related concurrency patterns –the Active Object –the Leader/Followers –the Half-Sync/Half-Async

Variant: Concurrent Event De-multiplexer Event de-multiplexer concurrent in multiple threads E.g., WaitForMultipleObjects() Benefits –Can improve throughput significantly for some applications Drawbacks –Need a thread-safe event de-multiplexer wrapper façade –Less portable (fewer platforms support this) –Implementation can become more complex