Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Distributed Programming: Tutorial 1

Questions & Solutions


1) Define the following terms: distributed computing, distributed system, distributed program,
remote procedure call, Concurrent computing, Grid computing, remote method invocation.
 Distributed Computing: Distributed computing refers to the use of
multiple computers (nodes) working together over a network to
achieve a common goal.
- In distributed computing, tasks are divided among multiple
computers, often geographically dispersed, to improve performanc e,
reliability, and scalability.

 Distributed System: A distributed system is a collection of


independent computers that appear to users as a single coherent
system.
-These computers communicate with each other and coordinate their
actions through message passing, shared memory, or other means.
-Distributed systems can range from small networks of computers to
large-scale, internet-based architectures.

 Distributed Program: A distributed program is an application or


software system designed to run on a distributed computing
environment.
-It consists of multiple components or processes running on
different machines, which communicate and coordinate to perform a
specific task or set of tasks.

 Remote Procedure Call (RPC): Remote Procedure Call is a protocol


that allows a computer program to execute code or procedures on
another computer or server over a network.
-It enables a client program to invoke procedures on a remote
server as if they were local procedures, abstracting away the
details of network communication.

 Concurrent Computing: Concurrent computing involves the


simultaneous execution of multiple tasks or processes to improve
performance, responsiveness, and resource utilization.
-Concurrent computing often involves handling multiple tasks
concurrently, either through multitasking, multithreading, or
multiprocessing.

 Grid Computing: Grid computing is a distributed computing paradigm


that involves the coordinated use of a large number of
heterogeneous computers or resources to solve complex computational
problems.
-Grid computing typically emphasizes resource sharing,
collaboration, and dynamic allocation of computing resources across
multiple administrative domains.
 Remote Method Invocation (RMI): Remote Method Invocation is a Java-
specific mechanism that allows an object on one Java Virtual
Machine (JVM) to invoke methods on an object running in another
JVM, typically on a remote machine.
-RMI provides a way for distributed Java applications to
communicate and interact with each other seamlessly.

2) What are the benefits of distributed systems?


 Scalability: Distributed systems can scale horizontally by adding
more machines to the network, allowing them to handle increasing
workloads.

 Fault Tolerance: Distributed systems can tolerate faults such as


hardware failures or network outages by replicating data or
services across multiple nodes.

 High Availability: By distributing resources across multiple nodes,


distributed systems can ensure that services remain available even
if individual nodes fail.

 Improved Performance: Distributing computation or data processing


tasks across multiple nodes can lead to improved performance by
parallelizing work.

 Geographical Distribution: Distributed systems can span multiple


geographical locations, allowing for better resource utilization
and reduced latency for users located in different regions.

3) Give five examples of distributed systems


 The World Wide Web (WWW): The WWW is a distributed system that
consists of a vast collection of interconnected web servers and
clients. Information is distributed across servers globally, and
users access resources using web browsers.

 Cloud Computing Platforms: Cloud computing platforms such as Amazon


Web Services (AWS), Microsoft Azure, and Google Cloud Platform
(GCP) are distributed systems that provide on-demand access to
computing resources such as servers, storage, and databases over
the internet.
 Distributed File Systems: Examples include the Hadoop Distributed
File System (HDFS) and Google File System (GFS), which distribute
file storage across multiple servers to provide fault tolerance and
scalability for handling large volumes of data.

 Distributed Databases: Distributed databases like Apache Cassandra,


MongoDB, and Spanner distribute data across multiple nodes for
improved scalability, fault tolerance, and performance.

 Peer-to-Peer (P2P) Networks: P2P networks such as BitTorrent and


Napster are distributed systems where peers collaborate to share
resources such as files, bandwidth, or processing power without the
need for centralized servers.

4) Discuss the fundamental distributed systems architecture


 Nodes: Nodes are individual computers or devices that are part of
the distributed system.
-These nodes can range from desktop computers to servers to IoT
devices.

 Communication Infrastructure: Distributed systems rely on a


communication infrastructure to facilitate communication between
nodes.

-This infrastructure can include network protocols, message passing


mechanisms, and middleware.

 Middleware: Middleware is software that sits between the operating


system and application software and facilitates communication and
data management across distributed nodes.
-It provides services such as remote procedure calls (RPC), message
queuing, and distributed object management.

 Concurrency Control: Distributed systems often involve concurrent


execution of multiple processes or threads across different nodes.
-Concurrency control mechanisms ensure that access to shared
resources is properly synchronized to prevent data corruption or
inconsistencies.

 Distributed File System: Distributed file systems allow for the


storage and retrieval of files across multiple nodes in a
distributed manner.
-These systems provide features such as replication, fault
tolerance, and scalability.

 Naming and Directory Services: Naming and directory services


provide mechanisms for locating and identifying resources within
the distributed system.
-They enable services such as naming resolution, distributed object
lookup, and service discovery.

 Security: Security is a critical aspect of distributed systems


architecture, involving measures such as authentication,
authorization, encryption, and access control to protect data and
resources from unauthorized access and malicious attacks.

 Fault Tolerance and Reliability: Distributed systems must be


designed to tolerate failures of individual nodes or components
without compromising the overall system's reliability.

-Techniques such as redundancy, replication, and error detection


and recovery mechanisms are employed to achieve fault tolerance.

 Scalability: Scalability is the ability of a distributed system to


handle increasing workloads or accommodate growing numbers of users
or nodes while maintaining performance and responsiveness.
-Scalability considerations include load balancing, partitioning,
and distributed caching.

 Consistency and Replication: Distributed systems must maintain


consistency of data across multiple replicas or copies of data
stored on different nodes.
-Replication strategies and consistency models are used to ensure
that data remains consistent and coherent despite concurrent
updates and network partitions.

5) Discuss the issues and challenges of distributed systems


 Network Communication: Distributed systems rely on network
communication between nodes, which introduces challenges such as
latency, bandwidth limitations, packet loss, and network
congestion.
-Ensuring efficient and reliable communication is crucial for the
performance and stability of distributed systems.
 Concurrency and Synchronization: Coordinating concurrent activities
across multiple nodes in a distributed system can lead to issues
such as race conditions, deadlocks, and data inconsistency.
-Implementing proper synchronization mechanisms and distributed
algorithms to manage concurrent access to shared resources is
essential.

 Fault Tolerance and Reliability: Distributed systems are


susceptible to various types of failures, including node failures,
network partitions, and communication failures.
-Designing fault-tolerant mechanisms such as replication,
redundancy, and error recovery protocols is important to ensure
system reliability and availability.

 Consistency and Replication: Maintaining consistency of data across


distributed nodes while allowing for scalability and performance is
a challenging task.
-Choosing an appropriate consistency model and replication strategy
based on the specific requirements of the application is crucial.

 Load Balancing and Scalability: Distributing workload evenly across


nodes and ensuring scalability to accommodate increasing load are
important concerns in distributed systems.
-Load balancing techniques such as round-robin, least-connections,
or dynamic load balancing algorithms help optimize resource
utilization and improve system performance.

 Security and Privacy: Distributed systems are vulnerable to various


security threats such as unauthorized access, data breaches, and
malicious attacks.
-Implementing robust security mechanisms such as encryption,
authentication, access control, and secure communication protocols
is essential to protect sensitive data and ensure system integrity.

 Heterogeneity and Interoperability: Distributed systems often


consist of heterogeneous hardware, operating systems, programming
languages, and communication protocols.
-Ensuring interoperability and seamless integration between diverse
components requires careful design, standardization, and middleware
technologies.
 Management and Monitoring: Managing and monitoring distributed
systems involve challenges such as configuration management,
resource allocation, performance tuning, and troubleshooting.
-Utilizing monitoring tools, logging mechanisms, and automated
management systems help administrators effectively manage and
maintain distributed systems.

6) What is an n-tier architecture?


 An n-tier architecture, also known as multi-tier architecture, is a
software design pattern that divides an application into logical
layers or tiers, with each tier responsible for specific
functionality.

 Typically, it consists of three or more tiers: presentation tier


(client), application tier (middle tier), and data tier (backend).
 The presentation tier handles user interaction and interface
display, the application tier contains business logic and
application processing, and the data tier manages data storage and
retrieval.

 Additional tiers may be added for scalability, such as caching or


load balancing tiers. N-tier architectures promote modularity,
scalability, and maintainability in distributed systems by
separating concerns and enabling independent development and
deployment of each tier.

7) What is middleware? Provide a classification service for middleware.


 Middleware is software that acts as a bridge or intermediary
between different systems, applications, or components in a
distributed computing environment. It provides a set of services
and abstractions that facilitate communication, integration, and
interoperability among disparate systems.

Classification of Middleware:
 Communication Middleware: This type of middleware focuses on
facilitating communication between distributed components or
systems. Examples include message-oriented middleware (MOM), remote
procedure call (RPC) middleware, and distributed object middleware.

 Transaction Middleware: Transaction middleware ensures the


integrity and consistency of distributed transactions across
multiple systems.
-It provides mechanisms for coordinating and managing transactional
operations in a distributed environment.

 Integration Middleware: Integration middleware enables the


integration of heterogeneous systems and applications by providing
tools and services for data transformation, routing, and mediation.
-Enterprise service bus (ESB) and message brokers are examples of
integration middleware.

 Concurrency Control Middleware: Concurrency control middleware


manages concurrent access to shared resources in distributed
systems to prevent data corruption and ensure consistency.
-It includes techniques such as distributed locking,
synchronization, and concurrency control protocols.

 Security Middleware: Security middleware provides authentication,


authorization, encryption, and other security services to protect
data and resources in distributed environments.
-It ensures confidentiality, integrity, and availability of
information exchanged between distributed components.

 Management Middleware: Management middleware offers tools and


services for monitoring, controlling, and administering distributed
systems.
-It includes capabilities for performance monitoring, resource
allocation, fault detection, and configuration management.

 Quality of Service (QoS) Middleware: QoS middleware ensures that


distributed applications meet predefined quality of service
requirements, such as reliability, availability, scalability, and
performance.
-It provides mechanisms for QoS monitoring, enforcement, and
adaptation.

8) What is a TP Monitor? What advantages does a TP Monitor bring to an OLTP environment?


 A TP Monitor, short for Transaction Processing Monitor, is a software
system responsible for managing and coordinating transactions in an
OLTP (Online Transaction Processing) environment. It provides a
centralized platform for handling concurrent transactions from
multiple clients efficiently and reliably.
Advantages of TP Monitors in an OLTP environment include :

 Transaction Management: TP Monitors ensure ACID (Atomicity,


Consistency, Isolation, Durability) properties for transactions,
maintaining data integrity and reliability.

 Concurrency Control: They manage concurrent access to shared


resources, preventing conflicts and ensuring data consistency.

 Scalability: TP Monitors facilitate horizontal scalability by


distributing transaction processing across multiple servers or
nodes, allowing systems to handle increasing workloads.

 Fault Tolerance: They incorporate mechanisms for fault detection,


recovery, and transaction rollback, ensuring system availability
and reliability.

 Performance Optimization: TP Monitors optimize transaction


processing performance through techniques such as connection
pooling, caching, and load balancing.

9) What is Cloud computing?


 Cloud computing refers to the delivery of computing services,
including servers, storage, databases, networking, software, and
more, over the internet ("the cloud").

 These services are provided on-demand and can be accessed and


utilized by users or organizations as needed without requiring
direct management of the underlying infrastructure.

 Cloud computing offers scalability, flexibility, and cost-


effectiveness by allowing users to pay only for the resources they
use.

 It typically involves three service models:

 Infrastructure as a Service (IaaS),

 Platform as a Service (PaaS), and

 Software as a Service (SaaS), catering to various levels of


abstraction and management responsibilities for users.

10) Discuss the five essential characteristics of cloud computing.


 On-Demand Self-Service: Users can provision computing resources
such as server instances, storage, and network bandwidth as
needed, without requiring human intervention from the service
provider.

 Broad Network Access: Cloud services are accessible over the


network through standard mechanisms, allowing users to access
resources using a variety of devices such as laptops,
smartphones, and tablets.

 Resource Pooling: Cloud computing resources are pooled together


to serve multiple users or clients, with physical and virtual
resources dynamically assigned and reassigned according to
demand. This pooling enables economies of scale and efficient
resource utilization.

 Rapid Elasticity: Cloud services can rapidly scale up or down to


accommodate changing workload demands.

-This elasticity allows users to quickly and easily scale


resources such as processing power, storage, and bandwidth to
meet fluctuating requirements.

 Measured Service: Cloud computing resources are monitored,


controlled, and reported transparently, allowing both the
service provider and the consumer to track resource usage.
-This enables pay-per-use billing models and provides visibility
into resource consumption for optimization and cost management
purposes.

11) Discuss the three main service models of cloud computing


 Infrastructure as a Service (IaaS): In IaaS, cloud providers
offer virtualized computing resources over the internet.

-This includes virtual machines, storage, and networking


infrastructure. Users can rent these resources on-demand and
scale them up or down as needed.

-With IaaS, users have more control over the underlying


infrastructure, including operating systems, applications, and
development frameworks.

 Platform as a Service (PaaS): PaaS provides a platform allowing


customers to develop, run, and manage applications without the
complexity of building and maintaining the underlying
infrastructure.
-Cloud providers offer a set of development tools, middleware,
databases, and runtime environments as services.

-Developers can focus on building and deploying applications


without worrying about infrastructure management tasks such as
provisioning servers, storage, or networking.

 Software as a Service (SaaS): SaaS delivers software


applications over the internet on a subscription basis.

-Users access these applications through a web browser or API


without needing to install or maintain any software locally.

-The software is centrally hosted and managed by the cloud


provider, who handles tasks such as software updates,
maintenance, and security.

-Examples of SaaS applications include email services, customer


relationship management (CRM) systems, and productivity tools.

12) Compare and contrast the four main deployment models for the cloud.
a) Public Cloud:
- Public cloud services are offered by third-party providers
over the internet.

- Resources are shared among multiple tenants, providing


scalability and cost-effectiveness.

- Examples include Amazon Web Services (AWS), Microsoft Azure,


and Google Cloud Platform (GCP).

- Advantages: Cost-effective, scalable, and no need for upfront


infrastructure investment.

- Disadvantages: Less control over security and compliance,


potential for vendor lock-in.

b) Private Cloud:
- Private cloud infrastructure is operated solely for a single
organization.

- It can be located on-premises or hosted by a third-party


service provider.

- Offers more control over security, compliance, and


customization.
- Examples include on-premises data centers or dedicated cloud
infrastructure from providers like VMware.

- Advantages: Greater control and customization, higher


security and compliance.

- Disadvantages: Higher initial investment, limited scalability


compared to public cloud.

c) Hybrid Cloud:
- Hybrid cloud combines public and private cloud resources,
allowing data and applications to be shared between them.

- Organizations can leverage the scalability of public cloud


while maintaining sensitive data on-premises.

- Offers flexibility to adapt workload placement based on


requirements.

- Examples include Azure Stack, AWS Outposts, and Google


Anthos.

- Advantages: Flexibility, scalability, and ability to leverage


existing infrastructure.

- Disadvantages: Complex to manage, potential integration


challenges.

d) Community Cloud:
- Community cloud infrastructure is shared among several
organizations with common concerns (e.g., industry-specific
regulations, security requirements).

- Offers collaboration and resource sharing within a specific


community.

- Managed and maintained by one or more organizations or a


third-party provider.

- Examples include cloud services tailored for specific


industries like healthcare or finance.

- Advantages: Cost sharing, collaboration, and tailored


services.
- Disadvantages: Limited scalability, potential for conflicts
among community members.

Exercise
We have two services implanted in from of objects: ObjectString and

ObjectCalculation. The first realizes string operations and the second arithmetic
operations. For simplicity, ObjectString has only one method NbOccurrences which

counts the number of occurrences of a character within a string (word), and

ObjectCalculation has a single method add which sums two numbers. The code of these

methods is given hereafter.

public int add (int a, int b) {

return a+b;

}
public int NbOccurrences(String c, String mot) {

int length=OriginalString.length();

int Nb=0;

for (int i=0; i<length; i++){


if ((OriginalString.substring(i, i+1)).equals(c))

Nb++;

}
return Nb;

1. We wish these methods to be remotely accessible. Give the structure of the interfaces that will
be share by clients and servers knowing that each method belongs to a distinct object.
To make the methods `add` and `NbOccurrences` remotely accessible, we
need to define interfaces for both `ObjectString` and
`ObjectCalculation`. These interfaces will be shared by clients and
servers to ensure consistency in method signatures and communica tion.
Below is the structure of the interfaces:

// Interface for ObjectString

public interface ObjectStringInterface extends java.rmi.Remote {

// Method to count the number of occurrences of a character within a


string

public int NbOccurrences(String c, String mot) throws


java.rmi.RemoteException;

// Interface for ObjectCalculation

public interface ObjectCalculationInterface extends java.rmi.Remote {

// Method to add two numbers

public int add (int a, int b) throws java.rmi.RemoteException;

Explanation:

- Both interfaces extend the `java.rmi.Remote` interface, indicating


that they are intended for remote method invocation.

- Each interface declares methods corresponding to the operations


provided by the respective objects (`ObjectString` and
`ObjectCalculation`).

- Each method includes the `throws java.rmi.RemoteException` clause to


handle remote invocation exceptions.

- These interfaces will be implemented by the server-side classes


(`ObjectStringImpl` and `ObjectCalculationImpl`) and will be used by
clients to invoke remote methods.

2. Complete the code of these methods to handle remote exceptions.


To handle remote exceptions in the provided code, we need to catch
`RemoteException` within each method and rethrow it. Here's the modified
code:

import java.rmi.RemoteException;
public class ObjectCalculationImpl implements ObjectCalculationInterface
{

public int add(int a, int b) throws RemoteException {

try {

return a + b;

} catch (Exception e) {

throw new RemoteException("Error occurred during addition: "


+ e.getMessage());

public class ObjectStringImpl implements ObjectStringInterface {

public int NbOccurrences(String c, String mot) throws


RemoteException {

try {

int length = mot.length();

int Nb = 0;

for (int i = 0; i < length; i++) {

if (mot.substring(i, i + 1).equals(c))

Nb++;

return Nb;

} catch (Exception e) {

throw new RemoteException("Error occurred during


NbOccurrences: " + e.getMessage());

}
Explanation:

- In each method (`add` and `NbOccurrences`), I've wrapped the method


body within a try-catch block to catch any exceptions that may occur
during execution.

- If an exception occurs, a `RemoteException` is thrown with an


appropriate error message.

- This ensures that any exceptions occurring during the remote method
invocation are properly handled and propagated back to the client.

3. Given the fact that, any java method called by a remote java program must belong to a remote
object, write the java classes that represent the object ObjectString and the object
ObjectCalculation respectively.
-To represent the objects `ObjectString` and `ObjectCalculation` as
remote objects in Java, we can use Java Remote Method Invocation (RMI).
Each object should implement a remote interface that defines the methods
accessible remotely. Here are the Java classes for the remote objects:

import java.rmi.Remote;

import java.rmi.RemoteException;

// Remote interface for ObjectString

public interface ObjectStringInterface extends Remote {

int NbOccurrences(String c, String mot) throws RemoteException;

// Remote interface for ObjectCalculation

public interface ObjectCalculationInterface extends Remote {

int add(int a, int b) throws RemoteException;

// Implementation of ObjectString

import java.rmi.RemoteException;

import java.rmi.server.UnicastRemoteObject;

public class ObjectStringImpl extends UnicastRemoteObject implements


ObjectStringInterface {

protected ObjectStringImpl() throws RemoteException {

super();
}

@Override

public int NbOccurrences(String c, String mot) throws


RemoteException {

int length = mot.length();

int Nb = 0;

for (int i = 0; i < length; i++) {

if (String.valueOf(mot.charAt(i)).equals(c))

Nb++;

return Nb;

// Implementation of ObjectCalculation

import java.rmi.RemoteException;

import java.rmi.server.UnicastRemoteObject;

public class ObjectCalculationImpl extends UnicastRemoteObject


implements ObjectCalculationInterface {

protected ObjectCalculationImpl() throws RemoteException {

super();

@Override

public int add(int a, int b) throws RemoteException {

return a + b;

In the above code:

- `ObjectStringInterface` and `ObjectCalculationInterface` are remote


interfaces defining the methods that can be invoked remotely.
- `ObjectStringImpl` and `ObjectCalculationImpl` are the implementations
of the remote objects. They extend `UnicastRemoteObject` to make them
available for remote invocation.

These classes can be used to create remote instances of `ObjectString`


and `ObjectCalculation` objects, which can then be accessed remotely by
Java RMI clients.

4. If the objects ObjectString and ObjectCalculation are to be deployed on the computer


user.coltech.cm, what are the other java classes that must be deployed on that computer? Give
their general structure.
 To deploy the objects ObjectString and ObjectCalculation on the
computer user.coltech.cm, we would also need to deploy the following
Java classes:

i. ObjectStringImpl.java:

import java.rmi.RemoteException;

import java.rmi.server.UnicastRemoteObject;

public class ObjectStringImpl extends UnicastRemoteObject implements


ObjectString {

public ObjectStringImpl() throws RemoteException {

super();

@Override

public int NbOccurrences(String c, String mot) throws


RemoteException {

int length = mot.length();

int Nb = 0;

for (int i = 0; i < length; i++) {

if ((mot.substring(i, i + 1)).equals(c))

Nb++;

return Nb;

}
}

ii. ObjectCalculationImpl.java :

import java.rmi.RemoteException;

import java.rmi.server.UnicastRemoteObject;

public class ObjectCalculationImpl extends UnicastRemoteObject


implements ObjectCalculation {

public ObjectCalculationImpl() throws RemoteException {

super();

@Override

public int add(int a, int b) throws RemoteException {

return a + b;

5. Write the commands to type on the computer user.coltech.cm to make available the methods
add and NbOccurrences for remote invocation on the port 2001.
To make the methods `add` and `NbOccurrences` available for remote
invocation on port 2001 on the computer `user.coltech.cm`, you would
typically follow these steps:

i. Compile the Java classes (`ObjectString` and `ObjectCalculation`)


containing the methods.

>> javac ObjectString.java ObjectCalculation.java

ii. Generate stubs and skeletons for the classes.

>> rmic ObjectString

>> rmic ObjectCalculation


iii. Start the RMI registry on port 2001.

>> rmiregistry 2001 &

iv. Bind the remote objects to the RMI registry.

java -Djava.rmi.server.codebase=file:/path/to/classes/ -
Djava.rmi.server.hostname=user.coltech.cm ObjectStringImpl

java -Djava.rmi.server.codebase=file:/path/to/classes/ -
Djava.rmi.server.hostname=user.coltech.cm ObjectCalculationImpl

 Replace `/path/to/classes/` with the actual path to the directory


containing the compiled classes.
 Now, the methods `add` and `NbOccurrences` should be available for
remote invocation on port 2001 on the `user.coltech.cm` computer .

6. Write the java client program that will run on another computer.
To create a Java client program that interacts with the remote services
ObjectString and ObjectCalculation, we'll use Remote Method Invocation
(RMI) for communication between the client and the server. Below is the
Java client program:

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

import java.rmi.RemoteException;

public class Client {

public static void main(String[] args) {

try {

// Get the registry

Registry registry = LocateRegistry.getRegistry("server_hostname", 1099);


// Replace "server_hostname" with the actual hostname of the server

// Lookup for ObjectCalculation

ObjectCalculation calculationService = (ObjectCalculation)


registry.lookup("ObjectCalculation");

// Perform arithmetic operation

int result = calculationService.add(5, 10);


System.out.println("Result of addition: " + result);

// Lookup for ObjectString

ObjectString stringService = (ObjectString)


registry.lookup("ObjectString");

// Perform string operation

String searchString = "a";

String inputString = "banana";

int occurrences = stringService.NbOccurrences(searchString, inputString);

System.out.println("Number of occurrences of '" + searchString + "': " +


occurrences);

} catch (Exception e) {

System.err.println("Client exception: " + e.toString());

e.printStackTrace();

}}

You might also like