CS193k, Stanford Handout #12. Threads 4 / RMI

Similar documents
CS193k, Stanford Handout #10. Threads 4 / RMI

CS193k, Stanford Handout #8. Threads 3

Written by: Dave Matuszek

Remote Method Invocation

RMI. Remote Method Invocation. 16-Dec-16

Last Class: Network Overview. Today: Distributed Systems

Object Interaction. Object Interaction. Introduction. Object Interaction vs. RPCs (2)

CS193k, Stanford Handout #13. HW3 RTable. The goal is to create a typical Table/TableModel setup with the tiny variation that...

JAVA RMI. Remote Method Invocation

5.4. Events and notifications

Generic architecture

RMI Example RMI. CmpE 473 Internet Programming RMI

Remote Method Invocation Benoît Garbinato

55:182/22C:182. Distributed Application Frameworks Java RMI, CORBA, Web Services (SOAP)

Remote Method Invocation

Remote Procedure Call

CS555: Distributed Systems [Fall 2017] Dept. Of Computer Science, Colorado State University

Remote Method Invocation. Benoît Garbinato

Distributed Computing

Chapter 4 Remote Procedure Calls and Distributed Transactions

Communication and Distributed Processing

Contents. Java RMI. Java RMI. Java RMI system elements. Example application processes/machines Client machine Process/Application A

Activation of remote objects

Remote Method Invocation

Chapter 4: Processes. Process Concept. Process State

Course Snapshot. The Next Few Classes. Parallel versus Distributed Systems. Distributed Systems. We have covered all the fundamental OS components:

Course Snapshot. The Next Few Classes

Outline. Chapter 4 Remote Procedure Calls and Distributed Transactions. Remote Procedure Call. Distributed Transaction Processing.

CS193j, Stanford Handout #21. Threading 3

Communication and Distributed Processing

1 interface TemperatureSensor extends java.rmi.remote 2 { 3 public double gettemperature() throws java.rmi.remoteexception; 4 public void

RMI (Remote Method Invocation) Over the year, there have been 3 different approaches to application development:

IBD Intergiciels et Bases de Données

03 Remote invocation. Request-reply RPC. Coulouris 5 Birrel_Nelson_84.pdf RMI

RMI. (Remote Method Invocation)

The UNIVERSITY of EDINBURGH. SCHOOL of INFORMATICS. CS4/MSc. Distributed Systems. Björn Franke. Room 2414

How are classes loaded into the Java Virtual Machine (JVM)? from the local file system (CLASSPATH). by an instance of ClassLoader

CSci Introduction to Distributed Systems. Communication: RPC In Practice

Distributed Systems. Distributed Object Systems 2 Java RMI. Java RMI. Example. Applet continued. Applet. slides2.pdf Sep 9,

Activation of remote objects

RPC flow. 4.3 Remote procedure calls IDL. RPC components. Procedure. Program. sum (j,k) int j,k; {return j+k;} i = sum (3,7); Local procedure call

JAVA RMI Java, summer semester

Desarrollo de Aplicaciones en Red RMI. Introduction. Considerations. Considerations. RMI architecture

Reflection/RMI 4/28/2009

THE UNIVERSITY OF WESTERN AUSTRALIA SAMPLE EXAM QUESTIONS 2007 WITH SOLUTIONS SCHOOL OF COMPUTER SCIENCE CITS3213 CONCURRENT PROGRAMMING (PART II)

RMI Case Study. A Typical RMI Application

Distributed Software Systems

Modulo II Socket, RMI e Corba

presentation DAD Distributed Applications Development Cristian Toma

Verteilte Systeme (Distributed Systems)

Applications. RMI, RPC and events. Request reply protocol External data representation. Operating System

Distributed object component middleware I - Java RMI

Distributed object component middleware I - Java RMI

Chapter 15: Distributed Communication. Sockets Remote Procedure Calls (RPCs) Remote Method Invocation (RMI) CORBA Object Registration

Sockets and RMI. CS151 Chris Pollett Dec. 5, 2005.

DISTRIBUTED OBJECTS AND REMOTE INVOCATION

Remote Objects and RMI

2018/2/5 话费券企业客户接入文档 语雀

Distributed Programming in Java. Distribution (2)

5 Distributed Objects: The Java Approach

IJESRT. http: //

Distributed Objects. Remote Method Invokation

Handouts. 1 Handout for today! Recap. Homework #2 feedback. Last Time. What did you think? HW3a: ThreadBank. Today. Small assignment.

Distributed Systems. 02r. Java RMI Programming Tutorial. Paul Krzyzanowski TA: Long Zhao Rutgers University Fall 2017

CS193k, Stanford Handout #16

Introduction & RMI Basics. CS3524 Distributed Systems Lecture 01

Component-Based Software Engineering

CS 5523 Operating Systems: Remote Objects and RMI

CS 351 Design of Large Programs Threads and Concurrency

Oracle WebLogic Server

BEA WebLogic. Server. Programming WebLogic RMI

Communication Basics, RPC & RMI. CS403/534 Distributed Systems Erkay Savas Sabanci University

Lab 2 : Java RMI. request sayhello() Hello interface remote object. local object. response "Hello world"

Chapter 5 Distributed Objects and Remote Invocation

4:40pm - 6:10pm (90 min)

RPC and RMI. 2501ICT Nathan

Distributed Systems COMP 212. Lecture 10 Othon Michail

EDA095 Remote Method Invocation

Outline. EEC-681/781 Distributed Computing Systems. The OSI Network Architecture. Inter-Process Communications (IPC) Lecture 4

Remote Method Invocation in Java

Distributed Systems. The main method of distributed object communication is with remote method invocation

CS108, Stanford Handout #22. Thread 3 GUI

DS 2009: middleware. David Evans

Message Passing vs. Distributed Objects. 5/15/2009 Distributed Computing, M. L. Liu 1

DISTRIBUTED COMPUTING

Middleware Labs: Java RMI

Distributed Systems. 5. Remote Method Invocation

Agent and Object Technology Lab Dipartimento di Ingegneria dell Informazione Università degli Studi di Parma. Distributed and Agent Systems RMI

Concurrent Object-Oriented Programming

A Typical RMI Application

EDA095 Remote Method Invocation

Overview of Web Services API

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 22: Remote Procedure Call (RPC)

A Typical RMI Application. Case Study

Java RMI Activation: A running example We have the following classes: MyRemoteInterface: the remote interface. Client: the client that invokes a

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

Distributed Applications Programming. Lab 4

BEA WebLogic Server. Programming WebLogic RMI

Lecture 17 Java Remote Method Invoca/on

Lecture VI: Distributed Objects. Remote Method Invocation

Transcription:

CS193k, Stanford Handout #12 Spring, 99-00 Nick Parlante Threads 4 / RMI Semaphore1 Semaphore1 from last time uses the count in a precise way to know exactly how many threads are waiting. In this way, the notify() is never dropped. Conservation of notify The notify() "conserves" the permission to go -- passing from one thread to another. if wait We use if() logic on the waiting side -- when the notify comes through, one waiter can unblock, even though the count may still be negative. Semaphore2 More straightforward implementation -- use the classic while-wait structure. decr() does not make the count negative. notify() is not conserved, but the logic is much simpler. Semaphore2 Code /* Alternate, more readable implementation of counting Semaphore -- uses the classic wait pattern: while (!cond) wait(); In this version, decr() does not move the count < 0, although the semaphore may be constructed with a negative count. This version is slightly less precise than above, since the notify() does not know if there is a matching wait(). This is not a big deal -- a notify() with no matching wait() is cheap. The precise semaphore counts the waits(), so it's notify() has a matching wait(). */ class Semaphore2 { private int count; public Semaphore2(int value) { count = value; // Try to decrement. Block if count <=0. // Returns on success or interruption. // The Semaphore value may be disturbed by interruption. public synchronized void decr() { while (count<=0) try { wait();

2 catch (InterruptedException inter) { Thread.currentThread().interrupt(); return; count--; // Increase, unblocking anyone waiting. public synchronized void incr() { count++; notify(); /* Use two Semaphores to get A and B to take turns. This code works. */ class TurnDemo2 { Semaphore2 ago = new Semaphore2(1); // a gets to go first Semaphore2 bgo = new Semaphore2(0); // <cut> public void demo() { final Semaphore2 finished = new Semaphore2(-1); new Thread() { public void run() { for (int i = 0; i< 10; i++) {b(); finished.incr();.start(); new Thread() { public void run() { for (int i = 0; i< 10; i++) {a(); finished.incr();.start(); finished.decr(); System.out.println("All done!"); // Alternative: init Semaphore to 0 and decr() twice Q1: if vs. while Q: What if the decr() used an if instead of a while? A: This would suffer from barging: another thread makes the count 0 inbetween when the notify() happens and the wait() unblocks. Q2: if (count==1) notify();

Q: what if the incr() tried to be clever and only do the notify when making the 0->1 transition. A: this won't work because we might have three threads blocked at count==0. Suppose incr() happens three times. We need three notifies, not just one. Q3 Interruption? Q: When decr() returns, do you have the lock or not? A: These implementations, you may or may not have the lock. Suppose the interrupt comes through not in the wait() but in the count--. It would be possible to write the Semaphore so it returned something from decr() to indicate if the lock is now held, but this makes the client side more messy everywhere. Wait for threads to finish The demo example now has a "finished" Semaphore that the launcher uses to wait for the children. Alternately, could init the Semaphore to 0 and decr twice. Or, could use join() twice. 3

4 Remote Method Invocation Interact with objects on other machines as if they were local Local "stub" object -- proxy for real remote object Advantages Sockets Easier than sockets -- just looks like message send Simple Scales -- you can interact with an object on your machine or somewhere else with practically the same code. Performance: OK, not great Doing your own socket based communication would be faster. CORBA CORBA is a language-neutral, platform-neutral -- things are expressed in the language independent Interface Description Language (IDL) CORBA provides lots of data transport, but does not move code CORBA is partly useful, and partly it's a management check-off item since it gives the appearance of portability and replaceability RMI provides consistency by just using Java everywhere RMI can actually move code back and forth -- Corba handles cross-language compatibility, but it does not move code from one place to another. JINI JINI is very much based on the idea of "mobile code". Your CD player sends UI code that presents the CD players interface to your Palm Pilot. The UI code then runs on the Palm. In this way, the Palm works with all devices -- even ones it does not know about. ==RMI Structure FooRemote Interface Interface off java.rmi.remote Everything throws RemoteException Defines methods visible on client side The client will actually hold a "stub" object that implements FooRemote Client messages on the stub get sent back to the real server object. FooServer Subclass off UnicastRemoteObject Implement FooRemote

This is the "real" object Implements the messages mentioned in FooRemote Can store state and implement other messages not visible in FooRemote Live/Remote vs. Serialization Remote Remote objects use RMI so there really is just one object. Messages sent on the remote stub tunnel back to execute against the one real server object. Non-Remote = Serialized Non remote arguments and return values use serialization to move copies back and forth. rmic tool Looks at the.class for the real object (FooServer) and generates the "stub" and "skel" classes used by the RMI system rmic FooServer -> produces FooServer_Stub.class and FooServer_Skel.class User code never mentions these classes by name-- they just have to be present in runtime space of the client and server so the RMI impl can use them. Stub Used on the client side as a proxy for the real object Skeleton Used on the server side to get glue things back to the real object Which Classes in Which Runtime Client: FooRemote, FooServer_Stub Server: FooRemote, FooServer, FooServer_Stub, FooServer_Skel (i.e. everything) One directory Our low-budget solution: build and run everything in one directory, but launch the client and server jobs on separate machines. Do not need a CLASSPATH set at all -- we'll rely on the "current directory" for both server and client 5

6 Abbreviated Code FooRemote public interface FooRemote extends java.rmi.remote { public String[] doit() throws RemoteException; public void install(piperemote pipe) throws RemoteException; Foo Server public class FooServer extends UnicastRemoteObject implements FooRemote {... public String[] doit() throws RemoteException { Log.print("FooServer: doit start"); serverinternal(); if (pipe!= null) pipe.send("sent on the pipe"); String[] result = new String[2]; result[0] = "hello"; result[1] = " there"; return(result);...

7 // Client.java import java.rmi.*; import java.math.*; public class Client { public static void main(string args[]) { if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); try { String name = "//" + args[0] + "/" + FooRemote.SERVICE; FooRemote foo = (FooRemote) Naming.lookup(name); String[] result = (String[]) foo.doit(); // key line Log.print("Client: result ID " + result.hashcode()); Log.print("Client: received from server -- " + result[0] + result[1]); PipeRemote pipe = new PipeServer(); Log.print("Client: pipe ID " + pipe.hashcode()); foo.install(pipe); foo.doit(); catch (Exception e) { System.err.println("FooClient exception: " + e.getmessage()); e.printstacktrace(); Log.print("Client: done"); PipeServer / PipeRemote Server on client Send to server Server messages its PipeRemote, it executes back here on the client where the real PipeServer is held.