The problem with the suspension and resumption of work

I have a script driver that controls a job line that can run jobs in parallel or sequentially based on a dependency graph. For instance:

Job Predecessors A null BA CA DB ED, C FE 

The driver starts A in the background and waits for it to complete, suspending the use of bash's built-in suspend . Upon completion, task A sends the SIGCONT driver, which then starts B and C in the background and pauses it again, etc.

The driver has set -m , so job control is enabled.

This works great when the driver itself starts in the background. However, when the driver is invoked in the foreground, the first call to pause work is fine . the second call seems to turn into " exit ", which says " There are stopped jobs ", but does not exit . the third call to pause also turns into " exit " and kills the driver and all children [since it must be taken into account that this is the second converted call to ' exit '].

And this is my question: Is this the expected behavior? If so, why and how do I get around it?

Thanks.

Code fragments below:

Driver:

  for step in $(hash_keys 'RUNNING_HASH') do proc=$(hash_find 'RUNNING_HASH' $step) if [ $proc ] then # added the grep to ensure the process is found ps -p $proc | grep $proc > /dev/null 2>&1 if [ $? -eq 0 ] then log_msg_to_stderr $SEV_DEBUG "proc $proc running: suspending execution" suspend # execution resumes here on receipt of SIGCONT log_msg_to_stderr $SEV_DEBUG "signal received: continuing execution" break fi fi done 

Job:

 ## $$ is the driver PID kill -SIGCONT $$ 
+4
source share
2 answers

I should think that you complicate everything too much, playing with work control and suspension, etc. Here is an example program in which 5 children are constantly working. After a second, he looks to see if someone has retired (much more efficiently than ps | grep, BTW) and, if necessary, launched a new child.

 #!/usr/bin/bash set -o monitor trap "pkill -P $$ -f 'sleep 10\.9' >&/dev/null" SIGCHLD totaljobs=15 numjobs=5 worktime=10 curjobs=0 declare -A pidlist dojob() { slot=$1 time=$(echo "$RANDOM * 10 / 32768" | bc -l) echo Starting job $slot with args $time sleep $time & pidlist[$slot]=`jobs -p %%` curjobs=$(($curjobs + 1)) totaljobs=$(($totaljobs - 1)) } # start while [ $curjobs -lt $numjobs -a $totaljobs -gt 0 ] do dojob $curjobs done # Poll for jobs to die, restarting while we have them while [ $totaljobs -gt 0 ] do for ((i=0;$i < $curjobs;i++)) do if ! kill -0 ${pidlist[$i]} >&/dev/null then dojob $i break fi done sleep 10.9 >&/dev/null done wait 
0
source

Do work tasks complete when they are completed? If so, instead of using suspend and SIGCONT, how about just using wait $PIDS in the script driver?

0
source

All Articles