DC EXPERIMENTS

 EXP 1 - WAP to study load load distribution in distributed computing

Code :-

import java.util.Scanner;

class Main {
    static void printLoad(int servers, int processes) {
        int each = processes / servers;
        int extra = processes % servers;
        int total = 0;

        for (int i = 0; i < servers; i++) {
            if (extra-- > 0)
                total = each + 1;
            else
                total = each;

            System.out.println("Server " + (char)('A' + i) + " has " + total + " Processes");
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the number of servers and processes: ");
        int servers = sc.nextInt();
        int processes = sc.nextInt();

        while (true) {
            printLoad(servers, processes);

            System.out.print(
                "1. Add Server\n" +
                "2. Remove Server\n" +
                "3. Add Processes\n" +
                "4. Remove Processes\n" +
                "5. Exit\n" +
                "Enter your choice: "
            );

            switch (sc.nextInt()) {
                case 1:
                    System.out.print("How many more servers?: ");
                    servers += sc.nextInt();
                    break;
                case 2:
                    System.out.print("How many servers to remove?: ");
                    servers -= sc.nextInt();
                    break;
                case 3:
                    System.out.print("How many more processes?: ");
                    processes += sc.nextInt();
                    break;
                case 4:
                    System.out.print("How many processes to remove?: ");
                    processes -= sc.nextInt();
                    break;
                case 5:
                    return;
            }
        }
    }
}


EXP - 2
Aim - WAP to study bulley election algorithm
Code -
import java.io.*;

class BullyAlgo {
    int cood, ch, crash;
    int[] prc;
public void election(int n) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("\nThe Coordinator Has Crashed!");
        int flag = 1;
        while (flag == 1) {
            crash = 0;
            for (int i = 0; i < n; i++) {
                if (prc[i] == 0) {
                    crash++;
                }
            }
            if (crash == n) {
                System.out.println("\n*** All Processes Are Crashed ***");
                break;
            } else {
                System.out.println("\nEnter The Initiator");
                int init = Integer.parseInt(br.readLine());
                if ((init < 1) || (init > n) || (prc[init - 1] == 0)) {
                    System.out.println("\nInvalid Initiator");
                    continue;
                }
 for (int i1 = init - 1; i1 < n; i1++) {
                    System.out.println("Process " + (i1 + 1) + " Called For Election");
                }
                System.out.println("");
                for (int i1 = init - 1; i1 < n; i1++) {
                    if (prc[i1] == 0) {
                        System.out.println("Process " + (i1 + 1) + " Is Dead");
                    } else {
                        System.out.println("Process " + (i1 + 1) + " Is Alive");
                    }
                }
                for (int i1 = n - 1; i1 >= 0; i1--) {
                    if (prc[i1] == 1) {
                        cood = (i1 + 1);
                        System.out.println("\n*** New Coordinator: Is " + cood + " ***");
                        flag = 0;
                        break;
                    }
                }
            }
        }
    }
 public void Bully() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter The Number Of Processes:");
        int n = Integer.parseInt(br.readLine());
        prc = new int[n];
        crash = 0;
        for (int i = 0; i < n; i++) {
            prc[i] = 1; // Initially, all processes are alive
        }
        cood = n;

        do {
            System.out.println("\n\t1. Crash A Process");
            System.out.println("\t2. Recover A Process");
            System.out.println("\t3. Display New Coordinator");
            System.out.println("\t4. Exit");
            ch = Integer.parseInt(br.readLine());
            switch (ch) {
                case 1:
                    System.out.println("\nEnter A Process To Crash");
                    int cp = Integer.parseInt(br.readLine());
                    if ((cp > n) || (cp < 1)) {
                        System.out.println("Invalid Process! Enter A Valid Process");
                    } else if ((prc[cp - 1] == 1) && (cood != cp)) {
                        prc[cp - 1] = 0;
                        System.out.println("\nProcess " + cp + " Has Been Crashed");
                    } else if ((prc[cp - 1] == 1) && (cood == cp)) {
                        prc[cp - 1] = 0;
                        election(n);
                    } else {
                        System.out.println("\nProcess " + cp + " is Already Crashed");
                    }
                    break;
 case 2:
                    System.out.println("\nCrashed Processes Are:\n");
                    for (int i = 0; i < n; i++) {
                        if (prc[i] == 0) {
                            System.out.println(i + 1);
                            crash++;
                        }
                    }
                    System.out.println("Enter The Process You Want To Recover");
                    int rp = Integer.parseInt(br.readLine());
                    if ((rp < 1) || (rp > n)) {
                        System.out.println("\nInvalid Process. Enter A Valid ID");
                    } else if ((prc[rp - 1] == 0) && (rp > cood)) {
                        prc[rp - 1] = 1;
                        System.out.println("\nProcess " + rp + " Has Recovered");
                        cood = rp;
                        System.out.println("\nProcess " + rp + " Is The New Coordinator");
                    } else if (crash == n) {
                        prc[rp - 1] = 1;
                        cood = rp;
                        System.out.println("\nProcess " + rp + " Is The New Coordinator");
                        crash--;
                    } else if (prc[rp - 1] == 0 && rp < cood) {
                        prc[rp - 1] = 1;
                        System.out.println("\nProcess " + rp + " Has Recovered");
                    } else {
                        System.out.println("\nProcess " + rp + " Is Not A Crashed Process");
                    }
                    break;

                case 3:
                    System.out.println("\nCurrent Coordinator is " + cood);
                    break;

                case 4:
                    System.exit(0);
                    break;

                default:
                    System.out.println("\nInvalid Entry!");
                    break;
            }
        } while (ch != 4);
    }
    public static void main(String args[]) throws IOException {
        BullyAlgo ob = new BullyAlgo();
        ob.Bully();
    }
}

EXP - 3
Aim - WAP to study RMI in Distributed Computing
Code :-
AddInterface.java :-
import java.rmi.*;
public interface AddInterface extends Remote {
   public int sum(int n1, int n2) throws RemoteException;
}

 

Add.java
import java.rmi.*;
import java.rmi.server.*;

public class Add extends UnicastRemoteObject implements AddInterface {
    int num1, num2;
public Add() throws RemoteException {
    }

    public int sum(int n1, int n2) throws RemoteException {
        num1 = n1;
        num2 = n2;
        return num1 + num2;
    }
}

 


AddServer.java
import java.rmi.Naming;
public class AddServer {
    public static void main(String[] args) {
        try {
            // Bind the remote object to the RMI registry with the name "Add"
            Naming.rebind("Add", new Add());
            System.out.println("Server is connected and waiting for the client...");
        } catch (Exception e) {
            System.out.println("Server could not connect: " + e);
       
    }
}

 

AddClient.java
import java.rmi.Naming;
public class AddClient {
    public static void main(String[] args) {
        try {
            // Look up the remote object (Add) in the RMI registry
            AddInterface ai = (AddInterface) Naming.lookup("//localhost/Add");
           // Call the remote method 'sum' and print the result
            System.out.println("The sum of 2 numbers is: " + ai.sum(10, 2));
        } catch (Exception e) {
            // Handle any exceptions
            System.out.println("Client Exception: " + e);
        }
    }
}


EXP  - 4
Aim - Develop a client server application which implements Name Server. Let the client like a web browser sends a request containing a hostname, then a piece of software such as name server resolver sends a request to the name server to obtain the IP address of a hostname.

Code -  

import java.net.*;  
import java.io.*;  
import java.util.*;  
public class DNS  
{  
public static void main(String[] args)  
{  
int n;  
BufferedReader in = new BufferedReader(new 
InputStreamReader(System.in)); do  
{  
System.out.println("\n Menu: \n 1. DNS 2. Reverse DNS 3. Exit \n"); 
System.out.println("\n Enter your choice");  
n = Integer.parseInt(System.console().readLine());  
if(n==1)  
{  
try  
{  
System.out.println("\n Enter Host Name ");  
String hname=in.readLine();  
InetAddress address;  
address = InetAddress.getByName(hname);  
System.out.println("Host Name: " + 
address.getHostName()); System.out.println("IP: " + 
address.getHostAddress());  
}  
catch(IOException ioe)  
{  
ioe.printStackTrace();  
}  
}  
if(n==2)  
{  
try  
System.out.println("\n Enter IP address");  
String ipstr = in.readLine();  
InetAddress ia = InetAddress.getByName(ipstr);  
System.out.println("IP: "+ipstr);  
System.out.println("Host Name: " +ia.getHostName());  
}  
catch(IOException ioe)  
{  
ioe.printStackTrace();  
}  
}  
}while(!(n==3));  
}} 

EXP - 5
Aim - To develop a client server application this implements chat server
Code :-

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Gridlayout;

public class CCLogin implements ActionListener {

    JFrame frame1;
    JTextField tf;
    JButton button;
    JLabel heading, label;

    public static void main(String[] args) {
        new CCLogin();
    }

    public CCLogin() {
        this.frame1 = new JFrame("Login Page");
        this.tf = new JTextField(10);
        this.button = new JButton("Login");

        this.heading = new JLabel("Chat Server");
        this.heading.setFont(new Font("Impact", Font.BOLD, 40));

        this.label = new JLabel("Enter your Login Name");
        this.label.setFont(new Font("Serif", Font.PLAIN, 24));

        JPanel localJPanel = new JPanel();

        this.button.addActionListener(this);

        localJPanel.add(this.heading);
        localJPanel.add(this.label);
        localJPanel.add(this.tf);
        localJPanel.add(this.button);

        this.heading.setBounds(30, 20, 280, 50);
        this.label.setBounds(20, 100, 250, 60);
        this.tf.setBounds(50, 150, 150, 30);
        this.button.setBounds(70, 190, 90, 30);

        this.frame1.add(localJPanel);
        localJPanel.setLayout(null);

        this.frame1.setSize(300, 300);
        this.frame1.setVisible(true);
        this.frame1.setDefaultCloseOperation(3);
    }

    public void actionPerformed(ActionEvent paramActionEvent) {
        String str = "";

        try {
            str = this.tf.getText();
            this.frame1.dispose();
            Client1 c1 = new Client1(str);
            c1.main(null);
        } catch (Exception localIOException) {
            // Handle exception
            localIOException.printStackTrace();
        }
    }
}


EXP -6 
Aim -  WAP to study Berkeley Clock synchronization algorithm
Code -
import java.io.*; 
import java.util.*; 
public class Berkley { 
// Function to calculate the time difference between Time Server and Nodes 
float diff(int h, int m, int s, int nh, int nm, int ns) { 
int dh = h - nh; 
int dm = m - nm; 
int ds = s - ns; 
int diff = (dh * 60 * 60) + (dm * 60) + ds; 
return diff; 
// Function to calculate the average of all time differences 
    float average(float diff[], int n) { 
        int sum = 0; 
        for (int i = 0; i < n; i++) { 
            sum += diff[i]; 
        } 
        float average = (float) sum / (n + 1); 
        System.out.println("The average of all time differences is " + average); 
        return average; 
    } 
 
    // Function to synchronize the clocks based on the average time difference 
    void sync(float diff[], int n, int h, int m, int s, int nh[], int nm[], int ns[], float average) 
        for (int i = 0; i < n; i++) { 
            diff[i] += average; 
 
            int dh = (int) diff[i] / (60 * 60); 
            diff[i] %= (60 * 60); 
            int dm = (int) diff[i] / 60; 
            diff[i] %= 60; 
            int ds = (int) diff[i]; 
 
            nh[i] += dh; 
            if (nh[i] > 23) { 
                nh[i] %= 24; 
            } 
 
            nm[i] += dm; 
            if (nm[i] > 59) { 
                nh[i]++; 
                nm[i] %= 60; 
            } 
 
            ns[i] += ds; 
            if (ns[i] > 59) { 
                nm[i]++; 
                ns[i] %= 60; 
            } 
 
            if (ns[i] < 0) { 
                nm[i]--; 
                ns[i] += 60; 
            } 
        } 
 
        // Adjust Time Server clock 
        h += (int) (average / (60 * 60)); 
        if (h > 23) { 
            h %= 24; 
        } 
 
        m += (int) (average / (60 * 60));  
        if (m > 59) { 
            h++; 
            m %= 60; 
        } 
 
        s += (int) (average % (60 * 60));  
        if (s > 59) { 
            m++; 
            s %= 60; 
        } 
 
        if (s < 0) { 
            m--; 
            s += 60; 
        } 
 
        // Print synchronized clocks 
        System.out.println("The synchronized clocks are:"); 
        System.out.println("Time Server -> " + h + ":" + m + ":" + s); 
        for (int i = 0; i < n; i++) { 
            System.out.println("Node " + (i + 1) + " -> " + nh[i] + ":" + nm[i] + ":" + ns[i]); 
        } 
    } 
 
    public static void main(String[] args) throws IOException { 
        Berkley b = new Berkley(); 
        Date date = new Date(); 
        BufferedReader obj = new BufferedReader(new 
InputStreamReader(System.in)); 
 
        System.out.println("Enter number of nodes:"); 
        int n = Integer.parseInt(obj.readLine()); 
 
        int h = date.getHours(); 
        int m = date.getMinutes(); 
        int s = date.getSeconds(); 
 
        int nh[] = new int[n]; 
        int nm[] = new int[n]; 
        int ns[] = new int[n]; 
 
        // Input times for each node 
        for (int i = 0; i < n; i++) { 
            System.out.println("Enter time for node " + (i + 1)); 
            System.out.print("Hours: "); 
            nh[i] = Integer.parseInt(obj.readLine()); 
            System.out.print("Minutes: "); 
            nm[i] = Integer.parseInt(obj.readLine()); 
System.out.print("Seconds: "); 
ns[i] = Integer.parseInt(obj.readLine()); 
// Send the Time Server's time to the nodes and calculate the differences 
System.out.println("Time Server sent time " + h + ":" + m + ":" + s + " to nodes."); 
float diff[] = new float[n]; 
for (int i = 0; i < n; i++) { 
diff[i] = b.diff(h, m, s, nh[i], nm[i], ns[i]); 
System.out.println("Node " + (i + 1) + " sent time difference of " + (int) diff[i] + 
" to Time Server."); 
// Calculate the average time difference and synchronize clocks 
float average = b.average(diff, n); 
b.sync(diff, n, h, m, s, nh, nm, ns, average); 


EXP - 7
Aim - WAP to demonstrate the implementation of a multi-threaded application using 
Java.

Code :-
class Threads extends Thread { 
// Constructor for the Threads class 
Threads() { 
super("User Threads"); 
System.out.println("User thread is created: " + this); 
start();  // Start the thread 
// The run() method that defines the task of the child thread 
public void run() { 
try { 
for (int i = 1; i <= 7; i++) { 
System.out.println("Printing the count of Child Thread: " + i); 
Thread.sleep(800);  // Sleep for 800 milliseconds 
} catch (InterruptedException e) { 
System.out.println("User thread Interrupted"); 
System.out.println("Child thread run is over"); 
public class Multithreading { 
public static void main(String args[]) { 
Threads th = new Threads();  // Create and start the child thread 
try { 
// The parent thread will continue to run as long as the child thread is alive 
while (th.isAlive()) { 
System.out.println("Parent thread will run till the Child thread is alive"); 
Thread.sleep(1500);  // Parent thread sleeps for 1500 milliseconds 
} catch (InterruptedException e) { 
System.out.println("Parent thread interrupted"); 
System.out.println("Parent thread's run is over"); 
}




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.