DC Lab - Group 13
DC Lab - Group 13
Client:
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
// Connect to the server running on localhost, port 12345
Socket socket = new Socket("localhost", 12345);
System.out.println("Connected to server.");
Output:
Server
"C:\Program Files\Java\jdk-19\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ
IDEA Community Edition 2022.3.3\lib\idea_rt.jar=52854:C:\Program Files\JetBrains\IntelliJ
IDEA Community Edition 2022.3.3\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8
-Dsun.stderr.encoding=UTF-8 -classpath C:\Users\rohra\IdeaProjects\DC\out\production\DC
Expt1Server
Server is running...
Client connected.
process 2: hi
hello
process 2: how are you
im fine
Client
"C:\Program Files\Java\jdk-19\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ
IDEA Community Edition 2022.3.3\lib\idea_rt.jar=52857:C:\Program Files\JetBrains\IntelliJ
IDEA Community Edition 2022.3.3\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8
-Dsun.stderr.encoding=UTF-8 -classpath C:\Users\rohra\IdeaProjects\DC\out\production\DC
Expt1Cleint
Connected to server.
Enter message to send to server (or type 'exit' to quit): hi
process 1: Server: hello
Enter message to send to server (or type 'exit' to quit): how are you
process 1: Server: im fine
Enter message to send to server (or type 'exit' to quit):
try {
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("New client connected: " + clientSocket);
Scanner clientScanner = new
Scanner(clientSocket.getInputStream());
String clientName = clientScanner.nextLine();
synchronized (writers) {
writers.add(new
PrintWriter(clientSocket.getOutputStream(), true));
connectedClients.add(clientName);
}
broadcastFromServer(clientName + " has joined the chat.");
broadcastFromServer("new member has joined !!");
Thread clientHandler = new Thread(new
ClientHandler(clientSocket, clientName));
clientHandler.start();
}
} finally {
serverSocket.close();
}
}
@Override
public void run() {
try {
Scanner scanner = new Scanner(clientSocket.getInputStream());
while (scanner.hasNextLine()) {
String message = scanner.nextLine();
if (message.equals("exit")) {
break;
} else if (message.startsWith("/whisper")) {
handleWhisper(message);
} else {
broadcast(clientName + ": " + message);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
synchronized (writers) {
writers.removeIf(writer -> writer.equals(clientSocket));
connectedClients.remove(clientName);
}
broadcastFromServer(clientName + " has left the chat.");
}
}
Client:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class GroupChatClient {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String clientName = scanner.nextLine();
Socket socket = new Socket("localhost", 5000);
try {
PrintWriter writer = new PrintWriter(socket.getOutputStream(),
true);
writer.println(clientName);
Thread readerThread = new Thread(new ClientReader(socket));
readerThread.start();
while (true) {
String message = scanner.nextLine();
writer.println(message);
if (message.equals("exit")) {
break;
}
}
} finally {
socket.close();
}
}
private static class ClientReader implements Runnable {
private Socket socket;
public ClientReader(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
Scanner scanner = new Scanner(socket.getInputStream());
while (scanner.hasNextLine()) {
String message = scanner.nextLine();
System.out.println(message);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
System.out.println("Disconnected from the server.");
}
}
}
}
Client:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class GroupChatClient {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String clientName = scanner.nextLine();
Socket socket = new Socket("localhost", 5000);
try {
PrintWriter writer = new PrintWriter(socket.getOutputStream(),
true);
writer.println(clientName);
Thread readerThread = new Thread(new ClientReader(socket));
readerThread.start();
while (true) {
String message = scanner.nextLine();
writer.println(message);
if (message.equals("exit")) {
break;
}
}
} finally {
socket.close();
}
}
private static class ClientReader implements Runnable {
private Socket socket;
public ClientReader(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
Scanner scanner = new Scanner(socket.getInputStream());
while (scanner.hasNextLine()) {
String message = scanner.nextLine();
System.out.println(message);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
System.out.println("Disconnected from the server.");
}
}
}
}
Output:
Server
"C:\Program Files\Java\jdk-19\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ
IDEA Community Edition 2022.3.3\lib\idea_rt.jar=52943:C:\Program Files\JetBrains\IntelliJ
IDEA Community Edition 2022.3.3\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8
-Dsun.stderr.encoding=UTF-8 -classpath C:\Users\rohra\IdeaProjects\DC\out\production\DC
expt2server
Group Chat Server is running...
New client connected: Socket[addr=/127.0.0.1,port=52951,localport=5000]
New client connected: Socket[addr=/127.0.0.1,port=49758,localport=5000]
Client
"C:\Program Files\Java\jdk-19\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ
IDEA Community Edition 2022.3.3\lib\idea_rt.jar=52946:C:\Program Files\JetBrains\IntelliJ
IDEA Community Edition 2022.3.3\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8
-Dsun.stderr.encoding=UTF-8 -classpath C:\Users\rohra\IdeaProjects\DC\out\production\DC
expt2client
Enter your name: group 13
group 13 has joined the chat.
new member has joined !!
hello
group 13: hello
new group has joined the chat.
new member has joined !!
new group: hello from here
Experiment - 03
Output:
Server Clock = 03:00
Client Clock 1 = 03:25
Client Clock 2 = 02:50
t1 - s = 25
t2 - s = -10
(st1 + st2 + 0)/3 = 5
t1 adjustment = -20
t2 adjustment = 15
Synchronized Server Clock = 03:05
Synchronized Client1 Clock = 03:05
Synchronized Client2 Clock = 03:05
Experiment - 04
Output:
Last login: Sat Feb 22 18:00:52 on ttys000
yash@Yashs-MacBook-Air ~ % cd desktop
yash@Yashs-MacBook-Air desktop % javac App.java
yash@Yashs-MacBook-Air desktop % java App
Enter no of processes
5
Process no 4 fails
Election Initiated by
2
Process 2 pass Election(2) to 3
Process 3 pass Election(3) to 0
Process 0 pass Election(0) to 1
Process 3 becomes coordinator
Process 3 pass Coordinator (3) message to process 0
Process 0 pass Coordinator (3) message to process 1
Process 1 pass Coordinator (3) message to process 2
End Of Election
yash@Yashs-MacBook-Air desktop %
Experiment - 05
for i in adj_matrix:
print(*i)
def find_parent(req_process):
request_queue[req_process].append(req_process)
for i in range(n):
if (adj_matrix[req_process][i] == 1):
parent = i
request_queue[parent].append(req_process)
break
if (token[parent] == 1):
return parent
else:
parent = find_parent(parent)
return parent
parent = find_parent(req_process)
while (token[req_process] != 1):
child = request_queue[parent][0]
request_queue[parent].pop(0)
holder[parent] = child
holder[child] = 0
token[parent] = 0
token[child] = 1
print("\nParent process {} has the token and sends the token
to the reqeust process {}".format(parent, child))
print("\nRequest Queue:", request_queue)
parent = child
if (len(request_queue[parent]) == 0):
print("\nRequest queue of process {} is empty. Therefore
Release Critical Section".format(parent))
print("\nHolder:", holder)
Output:
Raymond Tree based Mutual Exclusion
10000
10000
10000
01000
01000
Parent process 0 has the token and sends the token to the reqeust process 1
Parent process 1 has the token and sends the token to the reqeust process 4
Holder: {0: 1, 1: 4, 2: 0, 3: 1, 4: 4}
Experiment - 06
instances = []
print()
for i in range(r):
instance = int(input("Enter the instances of resource type R{}:
".format(i+1)))
instances.append(instance)
max = []
print("\nEnter the Max matrix for each process:")
for i in range(p):
max_i = [int(item) for item in input("P{}:
".format(i+1)).split()]
max.append(max_i)
alloc = []
print("\nEnter the Allocated matrix for each process:")
for i in range(p):
alloc_i = [int(item) for item in input("P{}:
".format(i+1)).split()]
alloc.append(alloc_i)
completed = []
for i in range(p):
completed.append(0)
sum = []
for i in range(r):
sum.append(0)
for i in range(p):
for j in range(r):
sum[j] += alloc[i][j]
avail = []
for i in range(r):
avail.append(instances[i] - sum[i])
need = []
print("\nNeed matrix: ")
for i in range(p):
need_i = []
print("P{}: ".format(i + 1), end="")
for j in range(r):
print(max[i][j] - alloc[i][j], end=" ")
need_i.append(max[i][j] - alloc[i][j])
print()
need.append(need_i)
count = 0
safeSequence = []
start = 0
while True:
process = -1
for i in range(start, p):
if completed[i] == 0:
process = i
for j in range(r):
if (avail[j] < need[i][j]):
process = -1
break
if process != -1:
break
if process != -1:
safeSequence.append(process + 1)
count += 1
for j in range(r):
avail[j] += alloc[process][j]
alloc[process][j] = 0
max[process][j] = 0
completed[process] = 1
if (count == p):
print("\nThe system is in a Safe State.")
print("Safe Sequence : < ", end="")
for i in range(p):
print("P{}".format(safeSequence[i]), end=" ")
print(">")
else:
print("\nThe system is in an Unsafe State.")
Output:
Enter the no. of processes: 5
Enter the no. of resources: 3
Need matrix:
P1: 7 4 3
P2: 1 2 2
P3: 6 0 0
P4: 0 1 1
P5: 4 3 1
Experiment - 07
def rrlb(n_servers,n_processes):
lst = []
for i in range(n_processes):
lst.append((i%n_servers)+1)
print()
for i in range(n_servers):
print("Server {} has {}
processes".format(i+1,lst.count(i+1)))
while True:
rrlb(n_servers, n_processes)
choice = int(input("\n1.Add Server\n2.Remove Server\n3.Add
Process\n4.Remove Process\n5.Exit\n\nEnter your choice: "))
if choice == 1:
n_servers += 1
elif choice == 2:
n_servers -= 1
elif choice == 3:
n_processes += 1
elif choice == 4:
n_processes -= 1
else:
break
Output:
Enter no. of servers: 3
Enter no. of processes: 7
1.Add Server
2.Remove Server
3.Add Process
4.Remove Process
5.Exit
1.Add Server
2.Remove Server
3.Add Process
4.Remove Process
5.Exit
1.Add Server
2.Remove Server
3.Add Process
4.Remove Process
5.Exit
1.Add Server
2.Remove Server
3.Add Process
4.Remove Process
5.Exit
1.Add Server
2.Remove Server
3.Add Process
4.Remove Process
5.Exit