0% found this document useful (0 votes)
447 views4 pages

Program No.1: Statement: Program To Simulate The Functioning of Lamport's Logical Clock. Logic

The document contains 3 program examples: 1) Simulates Lamport's logical clock algorithm by incrementing a clock value for each process event and ensuring clock values respect causal ordering. 2) Displays the IP address and host name of the local system or a provided host name. 3) Obtains the host name, port number, and protocol for a given URL by accessing properties of the URL object.

Uploaded by

Mrinal Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
447 views4 pages

Program No.1: Statement: Program To Simulate The Functioning of Lamport's Logical Clock. Logic

The document contains 3 program examples: 1) Simulates Lamport's logical clock algorithm by incrementing a clock value for each process event and ensuring clock values respect causal ordering. 2) Displays the IP address and host name of the local system or a provided host name. 3) Obtains the host name, port number, and protocol for a given URL by accessing properties of the URL object.

Uploaded by

Mrinal Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAM NO.

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;
}
PUBLIC VOID RECEIVEACTION(INT SRC, INT SENTVALUE)
{
C = (UTIL.MAX(C, SENTVALUE)) + 1;
}
}
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
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 :
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

You might also like