Chapter Three
Naming and Threads
1 11/13/2024
Contents
Naming and Name Services
Remote Invocation
Processes and Threads
2 11/13/2024
Naming and Name Services
Names or Codes, or Numbers
Names (when meaningful) are easier to remember than codes or
numbers.
Number (or sequence codes) are more useful for structuring data and
locating resources by a program..
Example: IPv4
Naming Services
In a distributed system, names are used to refer to a wide variety of
resources such as:
Computers, services, remote objects, and files, as well as users.
Naming is fundamental issue in DS design as it facilitates
communication and resource sharing.
3 11/13/2024
Naming and Name Services
In a Distributed System, a Naming Service is a specific service whose
aim is to provide a consistent and uniform naming of resources, thus
allowing other programs or services to localize them and obtain the
required metadata for interacting with them.
The major operation that a name service supports is to resolve names.
Key benefits
Resource localization- Resources are accessed using identifier or
reference
Uniform naming
Device independent address (e.g., you can move domain name/web
site from one server to another server seamlessly).
4 11/13/2024
Naming and Name Services
The role of names and name services
Currently, different name systems are used for each type of resource:
resource name identifies
file pathname file within a given file system
process process id process on a given computer
port port number IP port on a given computer
Uniform Resource Identifiers (URI) offer a general solution for any type of resource.
There two main classes:
1. URL Uniform Resource Locator (URL)
typed by the protocol field (http, ftp, nfs, etc.)
part of the name is service-specific
resources cannot be moved between domains
2. URN Uniform Resource Name (URN)
requires a universal resource name lookup service - a DNS-like system for all
resources
5 11/13/2024
Naming and Name Services
6 11/13/2024
Naming and Name Services
Navigation
Navigation is the act of chaining multiple Naming Services in order to
resolve a single name to the corresponding resource.
Iterative navigation
The client computer must determine which server this is.
NS2
2
Name
Client 1 NS1 servers
3
NS3
A client iteratively contacts name servers NS1–NS3 in order to resolve a name
7 11/13/2024
Naming and Name Services
Server controlled navigation
Recursive:
it is performed by the naming server
the server becomes like a client for the next server
Non recursive:
it is performed by the client or the first server
the server bounces back the next hop to its client
8 11/13/2024
Naming and Name Services
Domain name system
Rapidly resolves domain names to IP addresses
Basic DNS algorithm for name resolution (domain name -> IP number)
Look for the name in the local cache
Try a superior DNS server, which responds with:
another recommended DNS server
the IP address (which may not be entirely up to date
Directory services
Directory services can help with above situation: they store collections
of bindings and attributes and also looks up entries that match attribute-
based specs.
9 11/13/2024
Remote Invocation
invoking the method (service ) available in remote computer.
Some remote method concepts are
RPC (Remote Procedure Call)
Remote Method Invocation (RMI)
Local vs. Remote method invocation
10 11/13/2024
Remote Invocation
Reading Assignment
Read more RPC (Remote Procedure Call)
11 11/13/2024
Remote Method Invocation (RMI)
RMI is the action of invoking a method of a remote interface on a remote
object.
It is used to develop applications that communicate between virtual machines
RMI is a means of doing distributed computing
RMI hides the underlying mechanism of transporting method arguments and
return values across the network.
Example
Server returns sum of two numbers
Client calls the add() method of the server and passes two numbers
We will need four files
AddServerIntf.java
AddServerImpl.java
AddServer.java
AddClient.java
12 11/13/2024
Remote Method Invocation (RMI)
Programming with RMI
Programming with RMI involves:
Define a remote interface
Provide an implementation of the remote interface
Develop a Server
Develop a client
Generate stubs and skeletons
Start the RMI registry
Run the client and server
13 11/13/2024
Remote Method Invocation (RMI)
Step1: Defining a remote interface
AddServerIntf.java
A remote interface is an interface that declares a set of methods that may be
invoked from a remote Java virtual machine.
Must extend java.rmi.Remote
All methods must throw RemoteException
import java.rmi.*;
public interface AddServerIntf extends Remote
{
double add (double d1, double d2) throws RemoteException;
}
14 11/13/2024
Remote Method Invocation (RMI)
Step 2: Providing an implementation of the remote interface
Implement the methods that can be invoked remotely
The implementation class must extend UnicastRemoteObject that "exports" the
Server Object.
AddServerImpl.java
import java.rmi.*;
import java.rmi.server.*;
public class AddServerImpl extends UnicastRemoteObject implements
AddServerIntf
{
public AddServerImpl() throws RemoteException{}
public double add (double d1, double d2) throws RemoteException
{
return d1+d2;
}
15 11/13/2024
}
Remote Method Invocation (RMI)
Step 3: Developing a Server
The remote object must register itself with the RMI naming registry
AddServer.java
import java.rmi.*;
public class AddServer
{
public static void main(String[] args)
{ try{
AddServerImpl addServerImpl=new AddServerImpl();
Naming.rebind("AddServer",addServerImpl);
} catch(Exception e){System.out.println(e);}
}
16 } 11/13/2024
Remote Method Invocation (RMI)
Step 4: Developing a client
A reference to the remote object is obtained by the client by looking up the registry
The method is invoked using the reference. AddClient.java
import java.rmi.*;
public class AddClient {
public static void main(String[] args)
{ try{
String addServerURL = "rmi://"+args[0]+"/AddServer";
AddServerIntf addServerIntf= (AddServerIntf)Naming.lookup(addServerURL);
double d1=Double.valueOf(args[1]).doubleValue();
double d2=Double.valueOf(args[2]).doubleValue();
System.out.println("Sum= "+addServerIntf.add(d1,d2));
} catch(Exception e){System.out.println(e);}
}}
17 11/13/2024
Remote Method Invocation (RMI)
Step 5: Generate stubs and skeletons
Use the rmic compiler
rmic AddServerImpl
Stub
resides on client
does marshalling and unmarshalling
Provides interface of the server
Skeleton
Resides on server
does marshalling and unmarshalling
18 11/13/2024
Remote Method Invocation (RMI)
Step 6: Start the RMI registry
Simple Name Repository. It is a naming service that allows clients to
obtains references to remote objects
Server binds a name with an object implementation
Client can query the registry for checking the availability of a server
object
Step 6: Run Server Program and Client Programs
Run the server program then run the client program in order to get the
result
19 11/13/2024
Processes and Threads
Process (program in execution):
Unit of resource management for operating system.
Execution environment:
an address space
higher level resources
CPU context
Expensive to create and manage.
The solution is thread
20 11/13/2024
Processes and Threads
Threads (lightweight process):
Individual and separate unit of execution that is part of a process
multiple threads can work together to accomplish a common goal
Arise from the need for concurrent activities to share resources within
one process.
Enable to overlap computation with input and output.
Allow concurrent processing of client requests in servers – each
request handled by one thread.
Easier to create and destroy.
21 11/13/2024
Processes and Threads
Advantages of threads
easier to program
1 thread per task
can provide better performance
thread only runs when needed
multiple threads can share resources
utilize multiple processors if available
Disadvantages
multiple threads can lead to deadlock
much more on this later
overhead of switching between threads
22 11/13/2024
Processes and Threads
Life Cycle of a Thread
New born, Running, Blocked and Dead
Java Threads
Java threads are managed by the JVM.
Java threads may be created by:
Extending Thread class (at language-level)
must implement the run() method
thread ends when run() method finishes
call start() to get the thread ready to run
Implementing the Runnable interface
virtually identical to extending Thread class
must still define the run()method
setting up the threads is slightly different
23 11/13/2024
Processes and Threads
Threads States
New Born – New thread
Running – Active Thread
Blocked – Suspended or Sleep or waiting
Dead – killed
Programs using setPriority(), sleep()
24 11/13/2024
Processes and Threads
Multithread
Multithreading is the ability of a program or an operating system
process to manage its use by more than one user at a time and to
even manage multiple requests by the same user without having to
have multiple copies of the programming running in the computer.
Each user request for a program or system service (and here a user
can also be another program) is kept track of as a thread with a
separate identity.
25 11/13/2024
i o n!
te nt
r a t
y o u
s fo r
a nk
Th
26 11/13/2024