I am using ksh (sh (AT&T Research) 93u+ 2012-08-01) on Cygwin under Windows 7. 
I was writing a test script that would process a file list by creating up to a 
defined number of processes in parallel to handle the task. As each process 
completes, it would send a signal (USR1) back to the parent to indicate that 
another process could be started so to keep the processes at the predefined 
number. The problem I found is that the read of the file list will sometimes 
return nothing instead of the next file causing a premature exit and I am 
wondering why? In my test file list I just have the numbers 1 to 100 with one 
number per line. When I insert a sleep delay (bgproc), it will make it through 
the list though will sometimes end with the message "kill: 6152: no such 
process". Since I only start a process once I have a file name, this shouldn't 
happen and I have a wait at the end so that the parent should exist until all 
files are processed. Any idea why this should be happening?  Example script:

# When signal is sent process count is decremented
function tproc {
    (( i -= 1 ))
}

# function run in background. Uncommenting the delay seems to make things work.
function bgproc {
    # typeset r=$((  (RANDOM % 5 + 1) / 5.0 ))
    # sleep $r

    print $i $file
    kill -USR1 $$
}

trap tproc USR1

typeset -i i pids=1 pmax=4
print "pid=$$\n"

cat filelist.txt |
while true; do
    while (( i < pmax )); do
        read file
        [[ -z $file ]] && break 2

        (( i++ ))
        bgproc &
    done
    sleep .1
done

wait
echo done!

Example output (stops prematurely - should show count to 100):
$ p1
pid=6428

1 1
2 3
done!
$

Initially I had ended the loop with "done < filelist.txt" instead of piping it 
into the while with cat, but this seemed to be worse. I am working on a version 
that will start off the loop with a while read file; do instead to see if this 
handles things better but have not completed it yet. In any case, what is the 
problem with the read and why should the signal be sent after the parent 
completes when I have a wait?

Thanks,
Ben
_______________________________________________
ast-users mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-users

Reply via email to