Advertisement
war99859

Untitled

Oct 25th, 2023 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.37 KB | None | 0 0
  1. # vOTE
  2. process = int(input("Enter the number of process: "))
  3. processes = list(range(0,process))
  4. processes.remove(int(input("Enter the process which want to go to critical state: ")))
  5. votes = 0
  6. print("Enter 1 if you want to vote, else enter 0")
  7. for i in processes:
  8.     print("Enter your vote process"+str(i)+": ",end='')
  9.     k = int(input())
  10.     if k==1:
  11.         votes+=1
  12. if votes>=(process-1)//2:
  13.     print("The process can go to critical section")
  14. else:
  15.     print("The process can't go to critical section")
  16.  
  17. #tS
  18. n = int(input("Enter number of process: \n"))
  19. timestamp = []
  20. for i in range(n):
  21.     timestamp.append(input())
  22. dict = dict(zip(timestamp, list(map(lambda x : int(x[:2]*60) + int(x[3:]), timestamp))))
  23. print(timestamp.index(sorted(dict)[0]))
  24.  
  25.  
  26. #BERK
  27. a = int(input("Enter the number of machines: [excluding main machine] "))
  28. time = []
  29. main = input("Enter the time in the main system [hh:mm]: ")
  30. main_time_minutes = int(main[:2]) * 60 + int(main[3:])
  31. for i in range(a):
  32.     print("Enter the time in the machine", i + 1, "[hh:mm]: ", end="")
  33.     time.append(input())
  34. for i in range(a):
  35.     machine_time_minutes = int(time[i][:2]) * 60 + int(time[i][3:])
  36.     correction_value = main_time_minutes - machine_time_minutes  
  37.     machine_time_minutes += correction_value
  38.     hours = machine_time_minutes // 60
  39.     minutes = machine_time_minutes % 60
  40.     time[i] = f"{hours:02d}:{minutes:02d} [Correction Value: {correction_value:+}]"
  41. for i in range(a):
  42.     print("Node", i + 1, ": corrected value:", time[i])
  43.  
  44.  
  45.  
  46. #LAM
  47. class Process:
  48.     def __init__(self, id):
  49.         self.id = id
  50.         self.clock = 0
  51.  
  52.     def internal_event(self):
  53.         self.clock += 1
  54.         print(f"Process {self.id} executed internal event. Clock: {self.clock}")
  55.  
  56.     def send_message(self, other_process):
  57.         self.clock += 1
  58.         print(f"Process {self.id} sends message to Process {other_process.id}. Clock: {self.clock}")
  59.         other_process.receive_message(self)
  60.  
  61.     def receive_message(self, from_process):
  62.         self.clock = max(self.clock, from_process.clock) + 1
  63.         print(f"Process {self.id} received messsage from Process {from_process.id}. Clock: {self.clock}")
  64.  
  65. if __name__ == "__main__":
  66.     p1 = Process(1)
  67.     p2 = Process(2)
  68.  
  69.     p1.internal_event()
  70.     p2.internal_event()
  71.  
  72.     p1.send_message(p2)
  73.     p2.send_message(p1)
  74.  
  75.  
  76.  
  77.  
  78. import socket
  79. # Create a socket object
  80. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  81. # Connect to the server using the server IP address and port
  82. client_socket.connect(("127.0.0.1", 12345))
  83. # Send a message to the server
  84. message = input("Enter a message to send to the server: ")
  85. client_socket.send(message.encode())
  86. # Receive the encoded message from the server
  87. encoded_message = client_socket.recv(1024).decode()
  88. print("Received from server:", encoded_message)
  89. # Close the client socket
  90. client_socket.close()
  91.  
  92. import socket
  93. # Create a socket object
  94. server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  95. # Get the local machine name and port
  96. host = '127.0.0.1'
  97. port = 12345
  98. # Bind the socket to a specific address and port
  99. server_socket.bind((host, port))
  100. # Listen for incoming connections
  101. server_socket.listen(1)
  102. print("Server listening on {}:{}".format(host, port))
  103. # Accept a client connection
  104. client_socket, addr = server_socket.accept()
  105. print("Connection established from:", addr)
  106. # Receive data from the client
  107. data = client_socket.recv(1024).decode()
  108. print("Received from client:", data)
  109. message = input("Enter msg:")
  110. # Echo the data back to the client
  111. client_socket.send(message.encode())
  112. # Close the client socket
  113. client_socket.close()
  114.  
  115.  
  116.  
  117. import socket
  118. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  119. client_socket.connect(('127.0.0.1', 4444))
  120. file_list_str = client_socket.recv(1024).decode()
  121. file_list = file_list_str.split("\n")  # Corrected the split character
  122. print("Available files:")
  123. for file_name in file_list:
  124.     print(file_name)
  125. client_socket.close()
  126.  
  127. import os
  128. import socket
  129. server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  130. server_socket.bind(('127.0.0.1', 4444))
  131. server_socket.listen()
  132. print('Server started. Waiting for client connection...')
  133. client_socket, client_address = server_socket.accept()
  134. print('Client connected:', client_address)
  135. file_list = os.listdir()
  136. file_list_str = "\n".join(file_list)
  137. client_socket.send(file_list_str.encode())
  138. client_socket.close()
  139. server_socket.close()
  140.  
  141.  
  142. import xmlrpc.client
  143. # Create a client
  144. client = xmlrpc.client.ServerProxy("http://localhost:8000/")
  145. # Input date of birth
  146. date_of_birth = input("Enter your date of birth (YYYY-MM-DD): ")
  147. # Extract the year from the date_of_birth
  148. year_of_birth = int(date_of_birth.split("-")[0])
  149. # Call the remote function on the server with the year of birth
  150. age = client.calculate_age(year_of_birth)
  151. # Print the result
  152. print(f"Your age is: {age} years")
  153.  
  154. import xmlrpc.server
  155.  
  156. # Create a server
  157. server = xmlrpc.server.SimpleXMLRPCServer(("localhost", 8000))
  158.  
  159. # Function to calculate age
  160. def calculate_age(year_of_birth):
  161.     current_year = 2023  # You can use the actual current year
  162.     age = current_year - year_of_birth
  163.     return age
  164.  
  165. # Register the function
  166. server.register_function(calculate_age, "calculate_age")
  167.  
  168. # Start the server
  169. print("Server is running on port 8000...")
  170. server.serve_forever()
  171.  
  172.  
  173.  
  174. #dead
  175. class ResourceAllocationGraph:
  176.     def init(self, number_of_nodes):
  177.         self.edges = []
  178.         self.visited = [0] * number_of_nodes
  179.  
  180.     def add_edge(self, source, destination):
  181.         self.edges.append((source, destination))
  182.  
  183.     def has_deadlock(self, node):
  184.         if self.visited[node] == 1:
  185.             return True
  186.         if self.visited[node] == 0:
  187.             self.visited[node] = 1
  188.             for edge in self.edges:
  189.                 if edge[0] == node and self.has_deadlock(edge[1]):
  190.                     return True
  191.             self.visited[node] = 2
  192.         return False
  193.  
  194. if name == "main":
  195.     number_of_nodes = 4
  196.     graph = ResourceAllocationGraph(number_of_nodes)
  197.     # Add edges to the resource allocation graph
  198.     graph.add_edge(0, 1)
  199.     graph.add_edge(1, 2)
  200.     graph.add_edge(2, 0)
  201.     graph.add_edge(2, 3)
  202.  
  203.     if any(graph.has_deadlock(node) for node in range(number_of_nodes)):
  204.         print("Deadlock detected.")
  205.     else:
  206.         print("No deadlock detected.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement