Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 1 Nov 2007 11:59:58 -0500
From:      Dan Nelson <dnelson@allantgroup.com>
To:        David Naylor <blackdragon@highveldmail.co.za>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: sh script difficulties (running parallel functions)
Message-ID:  <20071101165958.GG3109@dan.emsphone.com>
In-Reply-To: <b53f6f940711010931i7067aad0y8c0d2c76aeec3248@mail.gmail.com>
References:  <b53f6f940711010931i7067aad0y8c0d2c76aeec3248@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
In the last episode (Nov 01), David Naylor said:
> Hi,
> 
> I am having a hard time getting (very complex script for me) to work.  The
> basic idea is that this script runs a bunch of tarkets, many of which are
> time consuming but low on resources (such as downloading files).  Now if I
> run the tarkets all at once (given some dependancy issues) it greatly speeds
> up the process (about 5 time speed increase).  However I do not know how to
> do this using sh...
> 
> Example
> 
> #!/bin/sh
> 
> worker1() {
>   # Copy some files
> }
> 
> worker2() {
>   # Download some files
> }
> 
> worker3() {
>   # Do something else
> }
> 
> ..... # and so on
> 
> run_jobs() {
>   worker1 &
>   worker2 &
>   worker3 &
>   # !!! Somehow wait for over workers to finish before continuing !!!
> }

You can use the "wait" shell builtin to wait for all children.
 
> #Finished
> 
> Furthermore, how can signals be handled such that the signals get
> accumulated and once all the other workers have finished the signals get
> passed on (appropriately)

You mean trapping ^C in the parent so that you can't stop the script
until all the workers are done?  That's more complicated to do in a
shell script.  Masking ^C completely is easy using the "trap" builtin ,
but catching it and then doing a "kill -INT $$" after all your workers
have completed is more complicated since the "wait" command will exit
when a signal is received, and I don't think it will tell you why it
exited (all children done, or got a signal).

-- 
	Dan Nelson
	dnelson@allantgroup.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20071101165958.GG3109>