-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathos_project.bash
125 lines (100 loc) · 2.12 KB
/
os_project.bash
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
#!/bin/bash
#this script will help you to find the safe sequence for a deadlock avoidance
#declaring the global variables
# declare -a available
declare -a need
declare -a allocation
declare -a sequence
#prompting the user for the number of processes
echo -n "Please enter the number of processes: "
read n
#prompting the user for the resources
echo -n "Please enter the number of resources: "
read m
#storing the available resources
echo "Please enter the available resources:"
for ((i=1; i<=m; i++ ))
do
read a
available[i]=$a
done
#storing the maximum need matrix
echo "Please enter the maximum need matrix:"
for ((i=1; i<=n; i++ ))
do
echo "Maximum need for process $i:"
for ((j=1; j<=m; j++ ))
do
read b
need[i,j]=$b
done
done
#storing the allocation matrix
echo "Please enter the allocation matrix:"
for ((i=1; i<=n; i++ ))
do
echo "Allocation for process $i:"
for ((j=1; j<=m; j++ ))
do
read c
allocation[i,j]=$c
done
done
#initializing the sequence
for ((i=1; i<=n; i++ ))
do
sequence[i]=0
done
#function to check whether a process can be executed or not
function CheckProcess {
local f=0
for ((j=1; j<=m; j++))
do
if [[ ${need[$1,$j]} -gt ${available[$j]} ]]
then
f=1
break
fi
done
return $f
}
#function to update available resources
function UpdateAvailable {
for ((j=1; j<=m; j++))
do
available[j]=$((${available[$j]} + ${allocation[$1,$j]}))
done
}
#fuction to find the safe sequence
function FindSequence {
local count=0
for ((i=1; i<=n; i++))
do
CheckProcess $i
if [[ $? -eq 0 ]]
then
sequence[$i]=$i
UpdateAvailable $i
count=$(($count + 1))
fi
done
return $count
}
#function to display the safe sequence
function DisplaySequence {
echo -n "The safe sequence is: "
for ((i=1; i<=n; i++))
do
echo -n "${sequence[$i]} "
done
echo ""
}
#executing the functions
FindSequence
if [[ $? -eq $n ]]
then
DisplaySequence
else
echo "System is not in safe state!"
exit 1
fi