Site hosted by Angelfire.com: Build your free website today!

Department of Computer Science, UMass Amherst Andrew H. Fagg

Announcements/Reminders

˛ Lab 3 proposal due on Friday.

˛ HW 3 is now out. Note the di®erent due time.

˛ Example code for IPC is available from the web site.

CMPSCI 377: Operating Systems Lecture 12, Page 1

Department of Computer Science, UMass Amherst Andrew H. Fagg

Today: Distributed Systems

What gets harder when we move from a stand alone system to a distributed

environment?

˛ resource sharing

˛ timing (e.g., synchronization)

˛ critical sections

˛ deadlock detection and recovery

˛ failure recovery

CMPSCI 377: Operating Systems Lecture 12, Page 2

Department of Computer Science, UMass Amherst Andrew H. Fagg

Resource Sharing

There are many mechanisms for sharing (hardware, software, data) resources.

˛ Data Migration: moving the data around

˛ Computation Migration: move the computation to the data

˛ Job Migration: moving the job (computation and data) or part of the

job

)The fundamental trade-o® in resource sharing is to complete user

instructions as fast and as cheaply as possible. (Fast and cheap are usually

incompatible.)

CMPSCI 377: Operating Systems Lecture 12, Page 3

Department of Computer Science, UMass Amherst Andrew H. Fagg

Computation versus Communication

˛ If communication is fast and cheap, we can utilize all the resources in the

distributed environment.

˛ If communication is slow and expensive, we should do most processing

locally.

˛ Reality is usually in the middle somewhere.

) we need a quantitative analysis to decide where the cuto®s are.

CMPSCI 377: Operating Systems Lecture 12, Page 4

Department of Computer Science, UMass Amherst Andrew H. Fagg

Data Migration

Data Migration may occur when process at site A accesses a Żle at site B

1. Copy Żle B to process A

˛ Costly if the Żle is large.

˛ Data must be converted to A's data format

˛ Multiple copies can cause consistency problems

˛ All subsequent accesses at A are local

2. Keep Żle at B, access Żle remotely from A

˛ Saves Żle transfer cost

˛ Converting the Żle from A's format to B's may be di±cult to do in

pieces

˛ Single copy of Żle, so no consistency problems

˛ Single \Żle service center" for Żle at B may be a performance

bottleneck

CMPSCI 377: Operating Systems Lecture 12, Page 5

Department of Computer Science, UMass Amherst Andrew H. Fagg

Computation Migration

Computation Migration may occur when it is more e±cient to transfer the

computation itself rather than data.

Example: a small program which produces a short summary of a large Żle,

say \wc" or a database query

˛ Remote Procedure Calls (RPC): suppose A wants to access Żle at site B.

B provides a predeŻned process.

A sends a message to the predeŻned process at B, which performs the

requested action, and sends the result back to A.

CMPSCI 377: Operating Systems Lecture 12, Page 6

Department of Computer Science, UMass Amherst Andrew H. Fagg

Job Migration

˛ Job Migration: perform the job (or parts of the job) at remote sites by

moving the data and computation.

{ Load balancing: even workload across the distributed system

{ Computational speedup: concurrent (parallel) execution of parts of

the job.

{ Hardware preference: job may match a given piece of hardware

somewhere in the system.

{ Software preference: job may require software only available on a

speciŻc site. For example, site speciŻc license of expensive software.

{ User interaction: may want to hide migration from the user. For

example, in load balancing. May want the user to specify migration

(hardware/software preferences)

CMPSCI 377: Operating Systems Lecture 12, Page 7

Department of Computer Science, UMass Amherst Andrew H. Fagg

Client/Server Model

˛ One of the most common models for structuring distributed computation

is by using the client/server paradigm.

{ A server is a process or collection of processes that provide a service,

e.g., name service, Żle service, database service, etc.

{ The server may exist on one or more nodes.

{ A client is a program that uses the service.

{ A client Żrst binds to the server, i.e., locates it on the network and

establishes a connection.

{ The client then sends the server a request to perform some action. The

server sends back a response.

{ RPC is one common way this structure is implemented.

CMPSCI 377: Operating Systems Lecture 12, Page 8

Department of Computer Science, UMass Amherst Andrew H. Fagg

Stream Sockets

A low-level mechanism for IPC...

˛ Provide a mechanism for bidirectional °ow of bytes between two processes

˛ Guarantee arrival order of the bytes

˛ At the OS level, a socket is attached to a port and is connected to a

speciŻc port on a host

˛ Server:

{ Listens on a speciŻc, known port (on a known host)

{ When a connection request comes in, a new socket is created to handle

the speciŻc connection

{ The listener typically continues to listen for new connections

CMPSCI 377: Operating Systems Lecture 12, Page 9

Department of Computer Science, UMass Amherst Andrew H. Fagg

Stream Sockets: Server Implementation

public class Server

{

private ServerSocket s; // Listening socket

private Socket client; // New socket for comm with the client

private Connection c; // Connection thread (coming slide)

private int counter = 0; // Some arbitrary state

public Server() {

try { // Listening socket

s = new ServerSocket(5155);

}

catch(java.io.IOException e) {

System.out.println(e);

System.exit(1);

};

System.out.println("Server is listening");

CMPSCI 377: Operating Systems Lecture 12, Page 10

Department of Computer Science, UMass Amherst Andrew H. Fagg

Stream Sockets: Server Implementation (cont)

:

:

try {

while(true) { // Loop forever

client = s.accept(); // Accept a new socket conn

c = new Connection(client, counter);

System.out.println("Opening new connection");

c.start(); // Start the handler thread

++counter;

}

}

catch (java.io.IOException e) {

System.out.println(e);

} }

public static void main(String args[]) {

Server serve = new Server();

} }

CMPSCI 377: Operating Systems Lecture 12, Page 11

Department of Computer Science, UMass Amherst Andrew H. Fagg

Stream Sockets: Connection Class Implementation

public class Connection extends Thread {

private Socket outputLine;

private int dat;

public Connection(Socket s, int num) {

outputLine = s; // Remember socket to client

dat = num; } // Remember arbitrary data

public void run() {

try { // String support for output stream

PrintWriter pout =

new PrintWriter(outputLine.getOutputStream(), true);

pout.println("Hello World " + dat); // String output to client

outputLine.close(); // Clean up

}

catch (java.io.IOException e) {

System.out.println(e);

}}};

CMPSCI 377: Operating Systems Lecture 12, Page 12

Department of Computer Science, UMass Amherst Andrew H. Fagg

Stream Sockets: Client Implementation

public class Client {

public Client() {

try {

Socket s = new Socket("localhost", 5155); // Connect to server

// Get buffer of chars

InputStream in = s.getInputStream();

BufferedReader bin =

new BufferedReader (new InputStreamReader(in));

System.out.println(bin.readLine()); // Read a line and print

s.close(); // Clean up

}

catch (java.io.IOException e) {

System.out.println(e); System.exit(1);

}}

public static void main(String args[]) {

Client client = new Client(); }}

CMPSCI 377: Operating Systems Lecture 12, Page 13

Department of Computer Science, UMass Amherst Andrew H. Fagg

Remote Procedure Call

Basic idea:

˛ Servers export

procedures for some

set of clients to call.

˛ To use the server,

the client does a

procedure call.

˛ OS manages the

communication.

(caller)

Server

(callee)

Client

Stub

bundle

return call

receive send

unbundle

Handler

Packet Packet

Handler

Network

return value

bundle

receive

call return

send

Stub

Client Server

arguments arguments

unbundle

return value

CMPSCI 377: Operating Systems Lecture 12, Page 14

Department of Computer Science, UMass Amherst Andrew H. Fagg

Remote Procedure Call: Implementation Issues

For each procedure on which we want to support RPC:

˛ The RPC mechanism uses the procedure signature (number and type of

arguments and return value)

1. To generate a client stub that bundles up the RPC arguments and

sends it o® to the server, and

2. To generate the server stub that unpacks the message, and makes the

procedure call.

CMPSCI 377: Operating Systems Lecture 12, Page 15

Department of Computer Science, UMass Amherst Andrew H. Fagg

Remote Procedure Call: Implementation Issues

Client Stub:

build message

send message

wait for response

unpack reply

return result

Server Stub:

create threads

loop

wait for a command

unpack request parameters

call procedure with thread

build reply with result(s)

send reply

end loop

Comparison between RPC and a regular procedure call

˛ Name of procedure

˛ Parameters

˛ Result

˛ Return address

CMPSCI 377: Operating Systems Lecture 12, Page 16

Department of Computer Science, UMass Amherst Andrew H. Fagg

Remote Procedure Call

˛ How does the client know the right port?

{ The binding can be static - Żxed at compile time.

{ Or the binding can be dynamic - Żxed at runtime.

˛ In most RPC systems, dynamic binding is performed using a name service.

{ When the server starts up, it exports its interface and identiŻes itself to

a network name server

{ The client, before issuing any calls, asks the name service for the

location of a server whose name it knows and then establishes a

connection with the server.

CMPSCI 377: Operating Systems Lecture 12, Page 17

Department of Computer Science, UMass Amherst Andrew H. Fagg

Example: Remote Method Invocation (RMI) in Java

˛ Java provides the following classes/interfaces:

{ Naming: class that provides the calls to communicate with the remote

object registry

{ public static void bind(String name, Remote obj) - Binds a

server to a name.

{ public static Remote lookup(String name) - Returns the server

object that corresponds to a name.

˛ UnicastRemoteObject: supports references to non-replicated remote

objects using TCP, exports the interface automatically when the server

object is constructed

CMPSCI 377: Operating Systems Lecture 12, Page 18

Department of Computer Science, UMass Amherst Andrew H. Fagg

Example: Remote Method Invocation (RMI) in Java

˛ Java provides the following tools:

{ rmiregistry server-side name server

{ rmic: given the server interface, generates client and server stubs that

create and interpret packets

CMPSCI 377: Operating Systems Lecture 12, Page 19

Department of Computer Science, UMass Amherst Andrew H. Fagg

Example: A Server in Java

˛ Server

{ DeŻnes an interface listing the signatures of methods the server will

satisfy

{ Implements each of the methods in the interface

{ Main program for server:

¤ Creates one or more server objects - normal constructor call where

the object being constructed is a subclass of RemoteObject

¤ Registers the objects with the remote object registry

˛ Client

{ Looks up the server in the remote object registry

{ Uses normal method call syntax for remote methods

{ Should handle RemoteException

CMPSCI 377: Operating Systems Lecture 12, Page 20

Department of Computer Science, UMass Amherst Andrew H. Fagg

Example: Hello World Server Interface

Declare the methods that the server provides:

// All servers must extend the Remote interface.

public interface Hello extends java.rmi.Remote {

// Any remote method might throw RemoteException.

// Indicates network failure.

String sayHello() throws java.rmi.RemoteException;

}

CMPSCI 377: Operating Systems Lecture 12, Page 21

Department of Computer Science, UMass Amherst Andrew H. Fagg

Example: Hello World Server

package examples.hello;

import java.rmi.*;

import java.rmi.server.UnicastRemoteObject;

public class HelloImpl extends UnicastRemoteObject implements Hello

{

public HelloImpl() throws RemoteException {

// The superclass constrctor exports the interface and gets a port

super();

}

public String sayHello() throws RemoteException {

// This is the "service" provided.

return "Hello World!";

}

CMPSCI 377: Operating Systems Lecture 12, Page 22

Department of Computer Science, UMass Amherst Andrew H. Fagg

Example: Hello World Server (cont)

public static void main(String args[])

{

// Create and install a security manager

System.setSecurityManager(new RMISecurityManager());

// Construct the server object.

HelloImpl obj = new HelloImpl();

// Register the server with the name server.

Naming.rebind("rmi://myhost/HelloServer", obj);

}

}

CMPSCI 377: Operating Systems Lecture 12, Page 23

Department of Computer Science, UMass Amherst Andrew H. Fagg

Example: Hello World Client

package examples.hello;

import java.awt.*;

import java.rmi.*;

import java.net.*;

public class HelloClient {

String message = "";

CMPSCI 377: Operating Systems Lecture 12, Page 24

Department of Computer Science, UMass Amherst Andrew H. Fagg

Example: Hello World Client (cont)

public HelloClient() {

try {

// Looks up the server using the name server on the host that

// the applet came from.

Hello obj = (Hello)Naming.lookup(

"rmi://" + getCodeBase().getHost() + "/HelloServer");

// Calls the sayHello method on the remote object.

message = obj.sayHello();

} catch (RemoteException e) {

System.out.println("HelloApplet RemoteException caught");

} }

public static void main(String args[]) {

HelloClient obj = new HelloClient();

System.out.println("Returned: " + obj.message);

}

CMPSCI 377: Operating Systems Lecture 12, Page 25

Department of Computer Science, UMass Amherst Andrew H. Fagg

Summary

˛ Data, computation, job migration

˛ Client-Server Model

˛ Mechanism: RPC

{ Most common model for communications in distributed applications.

{ RPC is essentially language support for distributed programming.

{ Relies on a stub compiler to automatically produce client/server stubs

from the signatures

{ RPC is commonly used even on a single node for communication

between applications running in di®erent address spaces.

CMPSCI 377: Operating Systems Lecture 12, Page 26

Department of Computer Science, UMass Amherst Andrew H. Fagg

Next Time

˛ Network Structures (Chapter 14)

CMPSCI 377: Operating Systems Lecture 12, Page 27