DC 1
DC 1
DC 1
def time(s):
ans = s.split(":")
return int(ans[0]) * 60 + int(ans[1]) + int(ans[2])/60
init_val = time(n)
dict_nodes = {}
ls = []
for i in range(nodes):
print(f"Enter the clock time for {i + 1} node : ")
x = input()
ls.append(time(x))
dict_nodes[chr(97 + i)] = []
print()
for key in dict_nodes.keys():
print(f"{key} : {dict_nodes[key]}")
print()
print(ls)
for i in range(nodes):
val = -float('inf')
for i in range(nodes):
diff = ls[i] - init_val
dict_nodes[chr(97 + i)].append(diff)
if diff < 0:
val = max(val, diff)
for i in range(nodes):
if val != -float('inf'):
ls[i] += abs(val)
print()
print(*ls)
print()
for key in dict_nodes.keys():
print(f"{key} : {dict_nodes[key]}")
for i in range(nodes):
avg = sum(dict_nodes[chr(97 + i)]) / nodes
if avg >= 0:
ls[i] -= abs(avg)
else:
ls[i] += abs(avg)
print()
print(ls)
def get_time(val):
hours = int(val // 60)
mins = int(val - hours * 60)
seconds = int((val - int(val)) * 60)
return str(hours) + ":" + str(mins) + ":" + str(seconds)
for i in range(nodes):
print(f'Time for Node {chr(97 + i)} : ', get_time(ls[i]))
Multithreading
def time(s):
ans = s.split(":")
return int(ans[0]) * 60 + int(ans[1]) + int(ans[2])/60
ls = []
for i in range(slaves):
print(f"Enter the clock time for slave {i + 1} : ")
x = input()
print(time(x))
ls.append(time(x))
def get_time(val):
hours = int(val // 60)
mins = int(val - hours * 60)
seconds = int((val - int(val)) * 60)
return str(hours) + ":" + str(mins) + ":" + str(seconds)
masterAdjustedTime=master_time+avg_time
print(masterAdjustedTime)
print(get_time(masterAdjustedTime))
return adjusted_times,masterAdjustedTime
ans, masterAdjustedTime=berkeley_sync(masterTime,ls)
print(ans)
for i in range(slaves):
print(f"Time for slave {i+1} : {get_time(ls[i]+ans[i])}")
Server:
import java.io.*;
import java.net.*;
// Start a separate thread for reading server terminal input and sending to client
new Thread(() -> {
try {
String serverMessage;
while ((serverMessage = serverInput.readLine()) != null) {
out.println("Server: " + serverMessage);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
import java.io.*;
import java.net.*;
import java.util.Scanner;
class Pro {
int id;
boolean act;
Pro(int id) {
this.id = id;
this.act = true;
}
}
public class Bully {
int TotalProcess;
Pro[] process;
int i = -1;
for(i = initBy; i < TotalProcess - 1; i++){
if(this.process[i].act){
System.out.println("Process " + i + " passes Election(" + i + ")" + " to " +(i + 1));
}else{
System.out.println("Process " + (i-1) + " passes Election("
+ (i - 1) + ")" + " to " +(i + 1));
}
}
int coordinatorID = -1; if(this.process[i].act) {
coordinatorID = i;
}else{
coordinatorID = i - 1;
}
System.out.println("Process " + coordinatorID + " is the process with max ID");
while (true) {
System.out.println("Enter the ID of the node requesting access to the
critical section (1-8), or 'exit' to quit:");
String input = scanner.nextLine();
if (input.equalsIgnoreCase("exit")) {
break;
}
int nodeID;
try {
nodeID = Integer.parseInt(input);
if (nodeID < 1 || nodeID > 8) {
System.out.println("Invalid node ID. Please enter a number
between 1 and 8.");
continue;
}
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid node ID or
'exit'.");
continue;
}
List<Integer> req = new LinkedList<>();
req.add(nodeID);
System.out.println("Initially:");
printFunction();
for (int a : req) {
System.out.println("--------- Process " + a + " -----------");
request(a, a);
}
}
scanner.close();
}
private static void request(int holder, int req) {
Node n = list.get(holder - 1);
n.q.add(req);
System.out.println("Queue of " + n.id + ": " + n.q);
if (n.holder != n.id)
request(n.holder, n.id);
else
giveToken(n.id);
}
private static void giveToken(int nid) {
Node n = list.get(nid - 1);
int next = n.q.remove();
n.holder = next;
if (next != nid)
giveToken(next);
else
printFunction();
}
private static void printFunction() {
for (Node n : list) {
System.out.println("id: " + n.id + " Holder: " + n.holder);
}
}
}
class Node {
int id;
int holder;
Queue<Integer> q;
def main():
processes = int(input("number of processes : "))
resources = int(input("number of resources : "))
max_resources = [int(i) for i in input("maximum resources : ").split()]
sequence=[]
print(s)
if __name__ == '__main__':
main()
Load Balancing:
import java.util.Scanner;
class LoadBalancer{
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 Servers 2.Remove Servers 3.Add Processes 4.Remove Processes 5.Exit: ");
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;
}
}
}
}