Debian Codegraph @

multiple-processes.sh

raw symbols SCIP
1 #!/bin/bash
2 # parent.sh
3 # Running multiple processes on an SMP box.
4 # Author: Tedman Eng
5
6 # This is the first of two scripts,
7 #+ both of which must be present in the current working directory.
8
9
10
11
12 LIMIT=$1 # Total number of process to start
13 NUMPROC=4 # Number of concurrent threads (forks?)
14 PROCID=1 # Starting Process ID
15 echo "My PID is $$"
16
17 function start_thread() {
18 if [ $PROCID -le $LIMIT ] ; then
19 ./child.sh $PROCID&
20 let "PROCID++"
21 else
22 echo "Limit reached."
23 wait
24 exit
25 fi
26 }
27
28 while [ "$NUMPROC" -gt 0 ]; do
29 start_thread;
30 let "NUMPROC--"
31 done
32
33
34 while true
35 do
36
37 trap "start_thread" SIGRTMIN
38
39 done
40
41 exit 0
42
43
44
45 # ======== Second script follows ========
46
47
48 #!/bin/bash
49 # child.sh
50 # Running multiple processes on an SMP box.
51 # This script is called by parent.sh.
52 # Author: Tedman Eng
53
54 temp=$RANDOM
55 index=$1
56 shift
57 let "temp %= 5"
58 let "temp += 4"
59 echo "Starting $index Time:$temp" "$@"
60 sleep ${temp}
61 echo "Ending $index"
62 kill -s SIGRTMIN $PPID
63
64 exit 0
65
66
67 # ======================= SCRIPT AUTHOR'S NOTES ======================= #
68 # It's not completely bug free.
69 # I ran it with limit = 500 and after the first few hundred iterations,
70 #+ one of the concurrent threads disappeared!
71 # Not sure if this is collisions from trap signals or something else.
72 # Once the trap is received, there's a brief moment while executing the
73 #+ trap handler but before the next trap is set. During this time, it may
74 #+ be possible to miss a trap signal, thus miss spawning a child process.
75
76 # No doubt someone may spot the bug and will be writing
77 #+ . . . in the future.
78
79
80
81 # ===================================================================== #
82
83
84
85 # ----------------------------------------------------------------------#
86
87
88
89 #################################################################
90 # The following is the original script written by Vernia Damiano.
91 # Unfortunately, it doesn't work properly.
92 #################################################################
93
94 #!/bin/bash
95
96 # Must call script with at least one integer parameter
97 #+ (number of concurrent processes).
98 # All other parameters are passed through to the processes started.
99
100
101 INDICE=8 # Total number of process to start
102 TEMPO=5 # Maximum sleep time per process
103 E_BADARGS=65 # No arg(s) passed to script.
104
105 if [ $# -eq 0 ] # Check for at least one argument passed to script.
106 then
107 echo "Usage: `basename $0` number_of_processes [passed params]"
108 exit $E_BADARGS
109 fi
110
111 NUMPROC=$1 # Number of concurrent process
112 shift
113 PARAMETRI=( "$@" ) # Parameters of each process
114
115 function avvia() {
116 local temp
117 local index
118 temp=$RANDOM
119 index=$1
120 shift
121 let "temp %= $TEMPO"
122 let "temp += 1"
123 echo "Starting $index Time:$temp" "$@"
124 sleep ${temp}
125 echo "Ending $index"
126 kill -s SIGRTMIN $$
127 }
128
129 function parti() {
130 if [ $INDICE -gt 0 ] ; then
131 avvia $INDICE "${PARAMETRI[@]}" &
132 let "INDICE--"
133 else
134 trap : SIGRTMIN
135 fi
136 }
137
138 trap parti SIGRTMIN
139
140 while [ "$NUMPROC" -gt 0 ]; do
141 parti;
142 let "NUMPROC--"
143 done
144
145 wait
146 trap - SIGRTMIN
147
148 exit $?
149
150 : <<SCRIPT_AUTHOR_COMMENTS
151 I had the need to run a program, with specified options, on a number of
152 different files, using a SMP machine. So I thought [I'd] keep running
153 a specified number of processes and start a new one each time . . . one
154 of these terminates.
155
156 The "wait" instruction does not help, since it waits for a given process
157 or *all* process started in background. So I wrote [this] bash script
158 that can do the job, using the "trap" instruction.
159 --Vernia Damiano
160 SCRIPT_AUTHOR_COMMENTS