0% found this document useful (0 votes)
2 views3 pages

Distributed Systems Java Programs

The document contains Java programs demonstrating concepts in distributed systems, including logical clocks, vector clocks, mutual exclusion, token-based algorithms, and deadlock detection. Each program illustrates a specific algorithm or mechanism used in distributed computing. The examples are simple and aim to provide foundational understanding of these concepts.

Uploaded by

mohanaj
Copyright
© All Rights Reserved
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)
2 views3 pages

Distributed Systems Java Programs

The document contains Java programs demonstrating concepts in distributed systems, including logical clocks, vector clocks, mutual exclusion, token-based algorithms, and deadlock detection. Each program illustrates a specific algorithm or mechanism used in distributed computing. The examples are simple and aim to provide foundational understanding of these concepts.

Uploaded by

mohanaj
Copyright
© All Rights Reserved
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

Distributed Systems Lab - Simple Java

Programs
EXP 7: Logical Clock
public class LogicalClock {
public static void main(String[] args) {
int clock = 0;

for (int i = 1; i <= 5; i++) {


clock++;
[Link]("Event E" + i + " -> Time: " + clock);
}
}
}

EXP 7: Vector Clock


import [Link];

public class VectorClock {


public static void main(String[] args) {
int[] vc = new int[3];

vc[0]++;
[Link]("P1 Event: " + [Link](vc));

vc[1] = [Link](vc[1], vc[0]);


vc[1]++;
[Link]("P2 Receive: " + [Link](vc));

vc[2]++;
[Link]("P3 Event: " + [Link](vc));
}
}

EXP 8: Mutual Exclusion


public class MutualExclusion {
static boolean lock = false;
public static void main(String[] args) {
process(1);
process(2);
}

static void process(int id) {


[Link]("Process " + id + " requesting...");

if (!lock) {
lock = true;
[Link]("Process " + id + " entering CS");
[Link]("Process " + id + " executing...");
lock = false;
[Link]("Process " + id + " leaving CS");
} else {
[Link]("Process " + id + " waiting...");
}
}
}

EXP 9: Token Based Algorithm


public class TokenAlgorithm {
static int token = 1;

public static void main(String[] args) {


for (int i = 1; i <= 3; i++) {
if (token == i) {
[Link]("Process " + i + " enters CS");
token = (i % 3) + 1;
} else {
[Link]("Process " + i + " waiting...");
}
}
}
}

EXP 10: Deadlock Detection


public class DeadlockDetection {
public static void main(String[] args) {
int[][] graph = {
{0,1,0},
{0,0,1},
{1,0,0}
};

if (graph[0][1]==1 && graph[1][2]==1 && graph[2][0]==1)


[Link]("Deadlock Detected");
else
[Link]("No Deadlock");
}
}

You might also like