Re: [SLUG] bash script fork children

From: Mike Branda (mike@wackyworld.tv)
Date: Thu Jan 04 2007 - 12:39:15 EST


On Wed, 2007-01-03 at 21:05 -0500, blee2@tampabay.rr.com wrote:
> Thus Mike Branda hast written on Wed, Jan 03, 2007 at 02:08:53PM -0500, and, according to prophecy, it shall come to pass that:
> > What I would like though is a way to fork multiple children in the loop
> > to start additional instances of convert until 4 are active. Then wait
> > until one exits and then start on the next image. 4 active converts
> > always until all the images are processed.
>
>
> This got under my skin and I spent 2 hours tonight researching, testing,
> and hacking to get something to work.
>

:^) lol... didn't mean to cause a stink....... I think I understand
most of what's in your script. I appreciate the input and I will let
you know what I come up with in the end!

Mike

>
> Try the following:
> ------------------
>
> #!/bin/bash
> # This is a sample program by Bryan Lee ( leebc11 at acm dot org ) that runs multiple commands in parallel in bash.
> # Distributed under GPL version 2
> # Let me know if you find it useful
>
> # Arguments should be specified on the initial command line. They will be used as the first argument for the parallel command one at a time.
>
>
> #This changes how bash handles process notification. It MUST be set.
> set -bm
>
> startprocess () {
> if [ $1 ]
> then
> echo Processing file $1
> xview $1 1>/dev/null & # This is the parallel command
> # OR
> #xview $1 & # Or this without IO redirection
> else
> echo "Out of files"
> echo "Waiting for remaining processes to finish"
> wait # This wait waits for everything else
> echo "All done...Bye, bye."
> exit 255
> fi
> }
>
> #This starts a new process each time a child exits
> trap 'startprocess $1; shift' SIGCHLD
>
>
> echo Starting with $# arguments
>
> #Have one of these for each parallel process you want running
> startprocess $1; shift
> startprocess $1; shift
> startprocess $1; shift
> startprocess $1; shift
>
> wait

-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS). Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 20:27:48 EDT