[SLUG] bash script fork children

From: Mike Branda (mike@wackyworld.tv)
Date: Wed Jan 03 2007 - 14:08:53 EST


So I've been playing with ImageMagick to batch process psd files into
flattened jpgs. I have a bash script that runs a for loop with
"convert" on a directory. What I have noticed is that in xosview on my
Dual Intel Hyper Threaded box ( shows up as 4 cpu's cause of the HT ),
only one ( or one half? ) CPU is doing all the work. Makes sense
because of the whole user space kernel space thing and I don't really
need both CPU's working on pieces of the same image anyway.

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.

i.e.

mbranda:~> pstree

├─kdeinit─┬
│ ├─konsole───bash───image_resize.sh─┬─convert
                                             |─convert
                                             |─convert
                                             |─convert

Right now I just create 4 different dirs and run 4 instances of the
shell script ( which calls convert ) on them. This makes the batching
of a few hundred images go much faster. I know there's got to be a way
to fork more instances and count how many are active. I figure the for
loop will have to be smart in some way to fork and move on to the next
image in $dir until the max of 4 instances is reached.

I see

mbranda:~> apropos fork

fork (2) - create a child process
vfork (2) - create a child process and block parent
fork (3p) - create a new process
vfork (3p) - create a new process; share virtual memory

Any ideas or suggestions? Anybody actually use the fork command in a
script before?

Thanks!

Mike Branda Jr.

-----------------------------------------------------------------------
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:04 EDT