ds prog

5
PROGRAM NO.1 Statement : Program to simulate the functioning of Lamport’s Logical Clock. Logic: Conditions: C1: C1(a)<C1(b) : for two events a and b of same process i C2: C1[i]<C2[j] : for two events a and b of different process i & j Implementation Rule: IR1: C1(a)=C1(a)+d, d=d+1 IR2: C2[j]=max(C2[j] , tm+d) Program: IMPORT JAVA.UTIL.*; PUBLIC CLASS LAMPORTCLOCK { INT C; PUBLIC LAMPORTCLOCK() { C = 1; } PUBLIC INT GETVALUE() { RETURN C; } PUBLIC VOID TICK() { C = C + 1; } PUBLIC VOID SENDACTION() { C = C + 1;

Transcript of ds prog

Page 1: ds prog

PROGRAM NO.1

Statement: Program to simulate the functioning of Lamport’s Logical Clock.

Logic:

Conditions:

C1: C1(a)<C1(b) : for two events a and b of same process iC2: C1[i]<C2[j] : for two events a and b of different process i & j

Implementation Rule:

IR1: C1(a)=C1(a)+d, d=d+1IR2: C2[j]=max(C2[j] , tm+d)

Program:

IMPORT JAVA.UTIL.*;PUBLIC CLASS LAMPORTCLOCK { INT C; PUBLIC LAMPORTCLOCK() { C = 1; } PUBLIC INT GETVALUE() { RETURN C; } PUBLIC VOID TICK() { C = C + 1; } PUBLIC VOID SENDACTION() { C = C + 1; } PUBLIC VOID RECEIVEACTION(INT SRC, INT SENTVALUE) { C = (UTIL.MAX(C, SENTVALUE)) + 1; }}

Page 2: ds prog

PROGRAM NO. 2

Statement: Program to display the IP address and host name of the system.

Program:

import java.net.*;

class InetDemo

{

public static void main(String args[])

{

try

{

InetAddress ia = InetAddress.getLocalHost();

System.out.println("The IP address of local host is "+ia);

ia=InetAddress.getByName(args[0]);

System.out.println("the IP address of "+args[0]+"is"+ia);

}

catch(UnknownHostException ue)

{

System.out.println("There is an error "+ue);

}

}

}

Output:

C:\JAVA\BIN>javac InetDemo.java

C:\JAVA\BIN>java InetDemo CL3SYS5

The IP address of local host is CL3SYS5/192.168.1.175

Page 3: ds prog

PROGRAM NO. 3

Statement: Program to obtain the information about the (a) Host, (b) Port, (c) Protocol.

Program:

import java.lang.* ;

import java.io.*;

import java.net.*;

class ud1

{

public static void main(String args []) throws

MalformedURLException

{

URL url = new URL("http://www.yahoo.com");

try

{

System.out.println("host name is " + url.getHost());

System.out.println("port no. is " + url.getPort());

System.out.println("protocol used is " + url.getProtocol());

}

catch (Exception e)

{

System.out.println("error"+e);

}

}

}

: Output

Page 4: ds prog

C:\JAVA\BIN>javac ud1.java

C:\JAVA\BIN>java ud1 yahoo.com

host name is www.yahoo.com

port no. is

protocol used is http