-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (59 loc) · 2.25 KB
/
Copy pathmain.py
File metadata and controls
77 lines (59 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import sys
import json
import re
from input_handler import read_input, validate_input
from safety_algo import run_safety_check
# Convert a python object to nicely formatted JSON
# This version also collapses simple arrays onto one line for cleaner output
def pretty_json(obj):
# First convert the object to indented JSON text
text = json.dumps(obj, indent=2)
# Helper function used by re.sub to collapse multi-line
def collapse_array(match):
# Get the inside content of the matched array
content = match.group(1)
# Split the lines, clean whitespace, and remove trailing commas
items = [item.strip().rstrip(',') for item in content.splitlines()]
# Rebuild the array on one line
return "[" + ", ".join(items) + "]"
# Replace simple multi-line arrays with single-line versions
return re.sub(r'\[\n([^\[\]]*?)\n\s*\]', collapse_array, text)
# Main driver function for the progam
def main():
# The program should be run with exactly one input file argument
if len(sys.argv) != 2:
print(json.dumps({"error": "Usage: python3 main.py input.json"}))
sys.exit(1)
# Get the input file name from the command line
input_file = sys.argv[1]
try:
# Read input from JSON file
data = read_input(input_file)
# Validate input structure and values
validate_input(data)
# Run the safety algorithm
result = run_safety_check(
data["available"],
data["allocation"],
data["max"]
)
# Build the final output in the required JSON format
if result["state"] == "SAFE":
output = {
"state": "SAFE",
"safe_sequence": result["safe_sequence"]
}
else:
output = {
"state": "DEADLOCK",
"deadlocked_processes": result["deadlocked_processes"]
}
# Print the final JSON output
print(pretty_json(output))
except Exception as e:
# If any error happens, print it as JSON and exit with an error code
print(json.dumps({"error": str(e)}))
sys.exit(1)
# Run main() only when this file is executed directly
if __name__ == "__main__":
main()