lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
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
#!/system/bin/sh
# Usage: spin_n_threads.sh <num_threads> [<nice>]
#        spin_n_threads.sh kill
 
TGID_FILE=/data/local/tmp/spin_n_threads_tgid.txt
 
spin_loop() {
  while :
  do
    NUM=$(expr 1 + 1)
  done
}
 
clean_up() {
  trap - SIGINT SIGTERM SIGKILL
  kill -- -$$
}
 
NUM_THREADS=1
if [ ! -z ${1} ]; then
  if [ ${1} == "kill" ]; then
    TGID=$(cat ${TGID_FILE})
    kill -- -${TGID}
    exit 0
  fi
 
  if [ ${1} -gt 1 ]; then
    NUM_THREADS=${1}
  else
    exit 0
  fi
fi
 
if [ ! -z ${2} ]; then
 renice -n ${2} -p $$
fi
 
# Register cleanup on trap
trap clean_up SIGINT SIGTERM SIGKILL
for i in $(seq 1 $NUM_THREADS ); do
  spin_loop &
done
 
echo $$ > ${TGID_FILE}