-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fifo.sh
115 lines (94 loc) · 2.04 KB
/
test_fifo.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
#!/bin/sh
n_processes="$1" ; shift
n_writes="$1" ; shift
n_words=8
if test -z "$n_processes" -o -z "$n_writes" ; then
echo "Usage: test_fifo.sh N_PROCESSES N_WRITES"
echo "Performs N_WRITES to fifo from N_PROCESSES running simultaneously."
echo "Tests that no writes were interveined."
echo
exit 1
fi
the_tmpdir="$( mktemp -d )"
the_fifo="${the_tmpdir}/fifo"
mkfifo "$the_fifo"
writer_pids=""
cleanup() {
rm "$the_fifo" 2>/dev/null
eval "kill -KILL $writer_pids 2>/dev/null"
}
trap cleanup EXIT
lpad() {
local s="$1" ; shift
local c="$1" ; shift
local l="$1" ; shift
while test "${#s}" -lt "$((l))" ; do
s="${c}${s}"
done
echo "$s"
}
writer() {
local n="$1" ; shift
local nl="$1" ; shift
local text_to_write=""
local word=""
local j
local nz="$( lpad "$n" '0' "$nl" )"
word="x${nz}"
j="$n_words"
while test "$((j))" -gt 0 ; do
j="$((j-1))"
text_to_write="$text_to_write $word"
done
eval "
local i=0
while test \"\$((i))\" -lt \"$((n_writes))\" ; do
i=\"\$((i+1))\"
echo '${text_to_write}' > \"${the_fifo}\"
done
"
}
nl_processes="${#n_processes}"
i=0
while test "$((i))" -lt "$((n_processes))" ; do
i="$((i+1))"
writer "$i" "$nl_processes" &
writer_pids="$writer_pids $!"
done
toreceive="$(( n_writes * n_processes ))"
receivecount=0
while true ; do
read s
s0="$s"
if test -z "$s" ; then
emptycount="$(( emptycount + 1 ))"
if test "$((emptycount))" -ge 1000 ; then
break
fi
else
emptycount=0
receivecount=$(( receivecount + 1 ))
fi
i=0
while test "$((i))" -lt "$((n_words))" ; do
i="$((i+1))"
s="${s# }"
s="${s#x}"
j=0
while test "$((j))" -lt "$((nl_processes))" ; do
j="$((j+1))"
s="${s#[0123456789]}"
done
done
printf "Received ${receivecount}/${toreceive} lines\r"
if test ! -z "$s" ; then
echo
echo "Error: received interveined input:"
echo " $s0"
echo "Seen as:"
echo " $s"
exit 1
fi
done < "$the_fifo"
echo
echo "Done ($receivecount lines received, none interveined)."