sh script difficulties (running parallel functions)

2007-11-01 Thread David Naylor
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 !!!
}

#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)

Thank you.

David
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sh script difficulties (running parallel functions)

2007-11-01 Thread Andy Harrison
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1



On 11/1/07, David Naylor  wrote:
   # !!! Somehow wait for over workers to finish before continuing !!!
 }

 #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)


One simplistic way of doing something like that is to make each worker
process write a file out to disk upon completion.  Then have a while
loop that sleeps for a minute and then checks for the existence of
that file and if it finds it, execute other commands.

Probably not the cleanest method, but I think you'll find that
handling forks and signals in a shell script is more trouble than it's
worth.

- --
Andy Harrison
public key: 0x67518262
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: http://firegpg.tuxfamily.org

iD8DBQFHKgQ1NTm8fWdRgmIRAn7eAKCZthrDzv0j7J6urphY3ohm6bSPZgCeIAt6
vhC2Zxw0ZTxw8eT+NZ/Uktg=
=schR
-END PGP SIGNATURE-
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sh script difficulties (running parallel functions)

2007-11-01 Thread Jan Grant
On Thu, 1 Nov 2007, David Naylor wrote:

 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 !!!
 }
 
 #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)

The wait shell builtin is part of what you're after.

You probably will need to trap the signals you're interested in 
catching. Just a

trap 'int=1' INT
wait
trap - INT

if [ x$int = x1 ]; then ... ; fi

should do it.


-- 
jan grant, ISYS, University of Bristol. http://www.bris.ac.uk/
Tel +44 (0)117 3317661   http://ioctl.org/jan/
There's no convincing English-language argument that this sentence is true.
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: sh script difficulties (running parallel functions)

2007-11-01 Thread Dan Nelson
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
[EMAIL PROTECTED]
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]