-
Notifications
You must be signed in to change notification settings - Fork 1
/
deadlock.sh
executable file
·133 lines (111 loc) · 2.97 KB
/
deadlock.sh
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
#!/bin/bash
file="proj2"
cp $file.out $file.tmp
cp $file.tmp $file.out
NZ=$1
NU=$2
TZ=$3
TU=$4
T=$5
if [[ $6 == "-d" ]]; then
echo "Info: automatic deadlock detection disabled"
auto=0
else
auto=1
fi
if [ -z "$NZ" ] || [ -z "$NU" ] || [ -z "$TZ" ] || [ -z "$TU" ] || [ -z "$T" ]; then
echo "Usage: ./deadlock.sh NZ NU TZ TU T (-a)"
echo " NZ: Number of customers"
echo " NU: Number of officials"
echo " TZ: Maximum time in miliseconds, that customers waits after creation and before they"
echo " enters the post office (eventually leaves, when post office is closed)"
echo " TU: Maximum time of official break in miliseconds"
echo " F: Maximum time in miliseconds in which post office is open for new customers"
echo " -d: Disable automatic deadlock detection (optional)"
exit 1
fi
if [ "$NZ" -lt 1 ] || [ "$NU" -lt -1 ]; then
echo "Error: Wrong number of people!" 2>&1
exit 0
fi
if [ "$TZ" -gt 10000 ] || [ "$TZ" -lt 0 ]; then
echo "Wrong customer time!" 2>&1
exit 0
fi
if [ "$TU" -gt 100 ] || [ "$TU" -lt 0 ]; then
echo "Wrong official time!" 2>&1
exit 0
fi
if [ "$T" -gt 1000 ] || [ "$T" -lt 0 ]; then
echo "Wrong office time!" 2>&1
exit 0
fi
noise() {
while true; do true; done
}
start_noise() {
for _ in $(seq $1); do
noise &
done
sleep $((RANDOM % 3 / 10))
}
stop_noise() {
kill $(jobs -p) &>/dev/null
wait
}
silent_kill() {
exec > /dev/null 2>&1
kill "$(jobs -p)"
pgrep proj2 && killall -q proj2
kill -- -$$
find /dev/shm -user "$(whoami)" -delete
}
check() {
while true; do
current=$(date +%s)
last_modified=$(stat -c "%Y" "$file.out")
if [ $(( "$current-$last_modified" )) -gt 1 ]; then
echo ""
echo "Deadlock detected! Output file was not changed in 1 second. Killing program."
silent_kill
exit 0
fi
sleep 2
done
}
find /dev/shm -user "$(whoami)" -delete
echo "Test 1: (no noise)"
echo -n "["
start=$(date +%s%3N)
[ $auto -eq 1 ] && check &
for _ in $(seq 20); do
echo -n "#"
./proj2 "$NZ" "$NU" "$TZ" "$TU" "$T" >/dev/null 2>&1
echo -n "#"
./proj2 "$NZ" "$NU" "$TU" "$TZ" "$T" >/dev/null 2>&1
echo -n "#"
./proj2 "$NZ" "$NU" "$TZ" "$TZ" "$T" >/dev/null 2>&1
echo -n "#"
./proj2 "$NZ" "$NU" "$TU" "$TU" "$T" >/dev/null 2>&1
done
echo "] $(( $(date +%s%3N) - start )) ms"
echo "Test 2: (random noise)"
echo -n "["
start=$(date +%s%3N)
for _ in $(seq 20); do
[ $auto -eq 1 ] && check &
start_noise $((RANDOM % 23))
echo -n "#"
./proj2 "$NZ" "$NU" "$TZ" "$TU" "$T" >/dev/null 2>&1
echo -n "#"
./proj2 "$NZ" "$NU" "$TU" "$TZ" "$T" >/dev/null 2>&1
echo -n "#"
./proj2 "$NZ" "$NU" "$TZ" "$TZ" "$T" >/dev/null 2>&1
echo -n "#"
./proj2 "$NZ" "$NU" "$TU" "$TU" "$T" >/dev/null 2>&1
stop_noise
done
echo "] $(( $(date +%s%3N) - start )) ms"
echo "You are awesome! No deadlock detected!"
rm proj2.tmp
exit 0