Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough.

Slides:



Advertisements
Similar presentations
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Advertisements

1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 TY, Sept 2011.
Object-Oriented Software Engineering Concurrent Programming.
Real-time Java Threads Insik Shin Real-time Systems Group University of Pennsylvania.
02/01/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Asynchronous Transfer of Control in the Timesys Reference Implementation.
Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
The Real-Time Java Profile ITV Real-Time Systems Anders P. Ravn Aalborg University February 2006.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 T Yang, Sept 2012.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Threads CSCE 190 – Java Instructor: Joel Gompert Wed, Aug 3, 2004.
1 Web Based Programming Section 8 James King 12 August 2003.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Real-Time, Clocking, and Porting (My Job ) Determining the Real Time Capabilities of various Operating Systems. Writing code to support Real Time Clocking.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Chapter 6 (6.7 & 6.9 only) - Threads & Mutex Threads –A Thread is a basic unit of CPU utilization consisting of a program counter, register set and stack.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading in JAVA
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
CSC 360, Instructor: Kui Wu Thread & PThread. CSC 360, Instructor: Kui Wu Agenda 1.What is thread? 2.User vs kernel threads 3.Thread models 4.Thread issues.
Threads A thread is an alternative model of program execution
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
1 Lecture 6 “Nachos” n nachos overview n directory structure n nachos emulated machine n nachos OS n nachos scheduler n nachos threads.
Chapter 4: Threads Joe McCarthy CSS 430: Operating Systems - Threads1.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
CSE 451: Operating Systems Section 6 Project 2b. Midterm  Scores will be on Catalyst and midterms were handed back on Friday(?) in class  Talk to Ed,
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Multithreading / Concurrency
Java Multithreading.
Concurrency, Processes and Threads
Chapter 4: Threads.
G53SRP: Real Time Threads in RTSJ (part I)
Threads CSSE 332 Operating Systems Rose-Hulman Institute of Technology
Chapter 4: Threads.
Chapter 4: Threads.
Multithreading.
Multithreaded Programming
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Threads Chapter 5 2/23/2019 B.Ramamurthy.
Still Chapter 2 (Based on Silberchatz’s text and Nachos Roadmap.)
Multithreading in java.
Representation and Management of Data on the Internet
Gentle Introduction to Threads
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Concurrency, Processes and Threads
Chapter 4 Threads “Most modern applications are multithreaded”
Lecture 19 Threads CSE /6/2019.
Chapter 4: Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Clocks & Asynchronous Events

Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough  Demo

Clocks Real-Time Specification Classes:  Clock getRealtimeClock() getResolution() getTime() setResolution()

Implementation oClocks are represented by a c struct: struct clockStruct { htimetime; htimeresolution; /*intthreadID; /* ID of the thread instantiating this clock */ struct clockStruct *nextClock;/* linked list */ THREAD TimerQueue; /* queue of threads waiting on this clock */ }; typedef struct clockStruct CLOCK;

Implementation Multiple clocks allow:  Events to be checked at different frequencies  More efficiency

Asynchronous Events An asynchronous event represents something that can happen. It can have a set of handlers associated with it. When the event occurs, the handler is scheduled by the scheduler.

Real-Time Specification The RTSJ includes a number of classes for dealing with Asynchronous Events.  AsyncEvent  AsyncEventHandler  Interruptable

AsyncEvent Methods  addHandler(asyncEventHandler handler)  bindTo(java.lang.String happening)  Fire()

AsyncEventHandler Code which is run in response to an event A java.lang.Runnable with a set of parameters Similar to a RealTimeThread

Basic Problem with a Single Thread Time scheduler Thread 1Thread 2

Basic Problem with a Single Thread Time scheduler Thread 1 Event Occurs Thread 2

Basic Problem with a Single Thread Time scheduler Thread 1 Event Occurs Event Handled Thread 2

This shows how the KVM currently works. Events are put into a queue. Queue is polled periodically at rescheduling. Allows context switching to be implementation independent. Basic Problem with a Single Thread

Problems with this model Asynchronous events should be handled very quickly. If the current thread does not yield, the VM hangs.

Solution When an event happens, the currently executing thread should be switched out of context and the event handler switched in.

Asynchronous Events Thread 1 Scheduler Event Handler Time

Asynchronous Events Thread 1 Scheduler Event Handler Time

Asynchronous Events Thread 1 Scheduler Event Handler Time

Asynchronous Events Thread 1 Scheduler Event Handler Time

Asynchronous Events Thread 1 Scheduler Event Handler Time

Asynchronous Events Thread 1 Scheduler Event Handler Time

Asynchronous Events using Signals This method for handling asynchronous events uses multiple threads. An external thread sends a signal to the KVM where it is caught by a signal handler. External Thread KVM Thread Signal Handler Signal

Demo Overview TwoThreadsTest.java SimpleThread.java Java.lang.thread sleep(); nativeCore.c Java_lang_Thread_sleep(); Java C

TwoThreadsTest.java class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji").start(); new SimpleThread("Taiti").start(); new SimpleThread("Hawaii").start(); } }

SimpleThread.java class SimpleThread extends Thread { String name; public SimpleThread(String str) { super(); name = new String(str); } public void run() { for (int i = 0; i < 10; i++) { System.out.println(i + " " + name); try { sleep((int)(1000)); } catch (InterruptedException e) {} } System.out.println("DONE! " + name); }

Java/lang/thread.java public static native void sleep(long millis) throws InterruptedException;

Kvm/VmCommon/src/nativeCore.c void Java_java_lang_Thread_sleep(void) { long64 period; popLong(period); /* only block if the time period is not zero */ if (ll_zero_ge(period)) { /* Copy CurrentThread because suspendThread clears it */ THREAD thisThread = CurrentThread; /* Suspend the current thread before we add the timer */ suspendThread(); /* Now add the timer (this is the safe way) */ registerAlarm(thisThread, period, resumeThread); } else { raiseException("java/lang/IllegalArgumentException"); } }

Signal Handlers: registerAlarm() adds a thread to TimerQueue TimerQueue is a Queue of threads sorted by wakeupTime To handle timer events, we start a new thread clockThread which checks the TimerQueue and sends a signal to the JVM when a timer has expired. When a signal is received by the VM, execution is interrupted and the signal handler clock_signal_handler() is called. clock_signal_handler() switches the waiting thread back into context.

Demo Overview (2) RTClock_md(); KVM Thread Signal Handler SIGUSR1 Clock_signal_handler();

Kvm/VmUnix/src/runtime_md.c /*===================================================== * FUNCTION: InitializeRTClock * TYPE: initialization * OVERVIEW: called from StartJVM.c to start a seperate clock * thread for RT clock interrupt * INTERFACE: * parameters: none * returns: none *===================================================*/ void InitializeRTClock() { pthread_t clockThread; pthread_create(&clockThread, NULL, RTClock_md, NULL); printf("* Initialize RTClock done\n"); }

Kvm/VmUnix/src/runtime_md.c /*================================================================= * FUNCTION: RTClock_md * TYPE: Machine dependent function * OVERVIEW: runs in a seperate pthread to send interrupts for timer events *================================================================*/ void *RTClock_md(void *p) { ulong64 now, nextTimer; for(;;) { if (TimerQueue != NULL) { now = CurrentTime_md(); enterSystemCriticalSection(); nextTimer = GET_ULONG(TimerQueue->wakeupTime); exitSystemCriticalSection(); if (ll_compare_le(nextTimer, now)) { kill(0, 10); /* send signal SIGUSR1 */ } } } /* RTClock_md */

Kvm/VmUnix/src/runtime_md.c /*================================================ * FUNCTION: clock_signal_handler * TYPE: RT clock * OVERVIEW: called when we receive a signal from the * clock thread * INTERFACE: * parameters: signal * returns: none *=============================================*/ static void clock_signal_handler(int sig) { reschedule(); }

Kvm/VmCommon/h/events.h #define reschedule() \ printf("Rescheduling...\n"); \ do { \ ulong64 wakeupTime; \ if (!areAliveThreads()) { \ return; /* end of program */ \ } \ checkTimerQueue(&wakeupTime); \ BetterHandleEvent(wakeupTime); \ __ProcessDebugCmds(0); \ } while (!SwitchThread());

OS Limitations Linux does not support some threading functions:  Suspend  Resume These are supported by:  OpenBSD  RT-Linux  Solaris  Timesys Linux