-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.py
208 lines (179 loc) · 5.83 KB
/
4.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# p = int(input(" Give number of processes -> "))
# r = int(input("Give number of resources -> "))
# max_r = [int(i) for i in input("Give the maximum available for each resources ->").split()]
# print("\nGive the already allocated resources for each process")
# c_allocation = [[int(i) for i in input(f"Process <{j + 1}> -> ").split()] for j in range(p)]
# print("\nGive the maximum resources for each process")
# max_n = [[int(i) for i in input(f"Process <{j + 1}> : ").split()] for j in range(p)]
# allocated = [0] * r
# for i in range(p):
# for j in range(r):
# allocated[j] += c_allocation[i][j]
# print(f"\ntotal allocated resources : {allocated}")
# available = [max_r[i] - allocated[i] for i in range(r)]
# for num in available:
# if num < 0:
# print("The allocation is more than the given number of resources")
# exit(0)
# print(f"total available resources : {available}\n")
# running = [True] * p
# c = p
# temp=0
# while c != 0:
# s =True
# for i in range(temp,p):
# if running[i]:
# executing = True
# for j in range(r):
# if max_n[i][j] - c_allocation[i][j] > available[j]:
# executing = False
# break
# if executing:
# print(f"Process {i } is executing")
# running[i] = False
# c -= 1
# temp=i
# s = True
# for j in range(r):
# available[j] += c_allocation[i][j]
# break
# else:
# s= False
# if(i==p-1):
# temp=0
# break
# if not s:
# print("It is in unsafe state")
# break
# print(f"It is in safe state\nAvailable resources -> {available}\n")
def needmatrix(need,maximum,allocated):
for i in range(len(processes)):
for j in range(R):
need[i][j] = maximum[i][j] - allocated[i][j]
def safestate(processes,available,maximum,allocated):
need = [[0 for j in range(R)] for i in range(len(processes))]
needmatrix(need,maximum,allocated)
finish = [0 for i in range(len(processes))]
s_sequence = [1 for i in range(len(processes))]
work = [i for i in available]
count = 0
while(count<len(processes)):
found = False
for p in range(len(processes)):
if(finish[p]==0):
executing = True
for j in range(R):
if(need[p][j]>work[j]):
executing = False
break
if(j==R-1 and executing):
for k in range(R):
work[k] += allocated[p][k]
s_sequence[count] = p
count +=1
finish[p] =1
found = True
if(found==False):
print("Not in safestate\n")
return False
exit()
print("System is in safe state\n")
print(f"Safe Sequence: {s_sequence}")
# # available instances of resources
# available = [2,1,3]
# # maximum resources needed by processes
# maximum = [
# [3,5,3],
# [3,2,2],
# [4,0,2],
# [2,2,2],
# [4,3,3]
# ]
# # resources allocated to processes
# allocated = [
# [0,1,0],
# [2,0,0],
# [3,0,2],
# [2,1,1],
# [0,2,2]
# ]
P = int(input("Enter number of processes -> "))
R = int(input("Enter number of resources -> "))
processes = [i for i in range(P)]
max_r = [int(i) for i in input("Enter maximum available for each resources ->").split()]
print("\nEnter the already allocated resources for each process")
allocated = [[int(i) for i in input(f"Process <{j}> -> ").split()] for j in range(P)]
print("\nEnter the maximum resources for each process")
maximum = [[int(i) for i in input(f"Process <{j}> : ").split()] for j in range(P)]
tallocated = [0] * R
for i in range(P):
for j in range(R):
tallocated[j] += allocated[i][j]
print(f"\nTotal allocated resources : {tallocated}")
available = [max_r[i] - tallocated[i] for i in range(R)]
for num in available:
if num < 0:
print("Allocation is greater than the given number of resources")
exit(0)
print(f"Total available resources : {available}\n")
safestate(processes,available,maximum,allocated)
# P, R = 3,1
# def needmatrix(need,maximum,allocated):
# for i in range(len(processes)):
# for j in range(R):
# need[i][j] = maximum[i][j] - allocated[i][j]
# def safestate(processes,available,maximum,allocated):
# need = [[0 for j in range(R)] for i in range(len(processes))]
# needmatrix(need,maximum,allocated)
# finish = [0 for i in range(len(processes))]
# s_sequence = [0 for i in range(len(processes))]
# work = [i for i in available]
# count = 0
# while(count<len(processes)):
# found = False
# for p in range(len(processes)):
# if(finish[p]==0):
# for j in range(R):
# if(need[p][j]>work[j]):
# break
# if(j==R-1):
# for k in range(R):
# work[k] += allocated[p][k]
# s_sequence[count] = p
# count +=1
# finish[p] =1
# found = True
# if(found==False):
# print("System is not is safestate\n")
# return False
# exit()
# print("System is in safestate\n")
# print(f"Safe Sequence: {s_sequence}")
# processes = [i for i in range(P)]
# # available instances of resources
# available = [2]
# # maximum resources needed by processes
# maximum = [
# [10],
# [4],
# [9],
# ]
# # resources allocated to processes
# allocated = [
# [5],
# [2],
# [3],
# ]
# safestate(processes,available,maximum,allocated)
# while(1):
# x = input("Add another process (y/n): ")
# if(x=='y'):
# t = int(input("Enter pid: "))
# processes += [t]
# maxm = list(map(int,input("Enter maximum resources needed: ").split(',')))
# allo = list(map(int,input("Enter allocated resources: ").split(',')))
# maximum.append(maxm)
# allocated.append(allo)
# safestate(processes,available,maximum,allocated)
# else:
# break