Re: Practical, efficient uses for fork()
by jwest (Friar) on Jun 29, 2001 at 21:42 UTC
|
Anywhere that you might block on some resource (a disk access,
a network connection, etc) where you can easily get other things
done while you're waiting for the resource to free up would be
a good place to start. If the results of the routine that
is blocking do not need to be considered immediately, this is a
great place to think about forking.
For a questionable example, you might have a CGI that
sends an email when the form is submitted. You could
potentially dispatch the mail sending process, which could
take an inordinate amount of time for reasons beyond your control,
via fork(). This way, the user interface responds rapidly,
while the back-end processing could take as long as it needs
to get the job done.
Personally, I don't find myself forking or creating threads all that often, so I'm
extremely curious as to what other monks have to say. But, I hope
this gives you at least one more valid example.
--jwest
-><- -><- -><- -><- -><-
All things are Perfect
To every last Flaw
And bound in accord
With Eris's Law
- HBT; The Book of Advice, 1:7
| [reply] |
Re: Practical, efficient uses for fork()
by the_slycer (Chaplain) on Jun 29, 2001 at 21:42 UTC
|
I've used it in a ReadKey while loop.
ie:
while ($cmd = ReadKey(0)){
if ($cmd =~ /K/){
#fork and do something, continue with loop
}
#check for other keys either forking more or killing procs etc
}
| [reply] [d/l] |
Re: Practical, efficient uses for fork()
by Aighearach (Initiate) on Jun 30, 2001 at 02:46 UTC
|
I use fork in all my daemons. It looks pretty much the same in every one... (you will find it suspiciously similiar to the man pages, also... ;)
use POSIX qw( setsid ctime );
if ( $DAEMON ) {
my $log = "/var/log/$0.log";
chdir '/' or die "Can't chdir to /: $!";
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, ">>$log" or die "Can't write to $log: $!";
{
defined(my $pid = fork) or die "Can't fork: $!";
exit if $pid; # kill the parent to detach
}
setsid or die "Can't start a new session: $!";
open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
}
print "Started $0 at ".ctime(time);
-- Snazzy tagline here
| [reply] [d/l] |
Re: Practical, efficient uses for fork()
by kschwab (Vicar) on Jun 30, 2001 at 07:29 UTC
|
I've used fork in a somewhat more specialized version
of my Fork::Queue node. I'm using it
in an error event processor. Basically, I don't want
the clients that are sending events to have to wait
on the server to process the event.
I fork off one child, who can work the queue of events
while the main process is free to handle client requests
as fast as possible.
I also use fork() when I need simple multitasking, and
don't want to serialize file i/o ( or some other long-running or blocking operation).
As an example, I have
a unix box with 3 web servers on it. I have a perl script
that grabs lines from each web server log and grinds out
some stats. So, the parent forks off three children, each
with a pipe back to the parent. Each child process grinds
on the individual stats, then passes a small amount of
aggregate stats back to the parent over the pipe. The parent
can then spit out "global" stats for the box.
This runs, as you can imagine, almost three times faster than doing it serially. | [reply] |
Re: Practical, efficient uses for fork()
by PsychoSpunk (Hermit) on Jun 29, 2001 at 21:16 UTC
|
I used fork() in wackyass, my webcam archiving system.
ALL HAIL BRAK!!! | [reply] |
Re: Practical, efficient uses for fork()
by Anonymous Monk on Jun 29, 2001 at 22:30 UTC
|
fork() while 1;
makes a very practical and efficient resource hog :) | [reply] [d/l] |
Re: Practical, efficient uses for fork()
by oakbox (Chaplain) on Jun 29, 2001 at 23:28 UTC
|
I use them when dealing with CGI scripts. Some intensive operations that a client wants to have total control over (no cron jobs) I handle by using forks.
For instance, the client wants to run a billing process when he mails an issue of his magazine. It takes a long time to run credit card transactions on 85 accounts. I show him a thank you page, then get on with the processing in the 'background'.
Richard - oakbox
www.oakbox.com
"If what I'm saying doesn't make sense, that's because sense cannot be made, it's something that must be sensed"-J.S. Hall | [reply] |
Re: Practical, efficient uses for fork()
by stiqs (Initiate) on Jun 29, 2001 at 21:51 UTC
|
Actually, java1.4 beta has added a regex bundle that takes-in perl like regexes. I haven't done much with it, but it should work similar to the perlTools bundle. | [reply] |
Re: Practical, efficient uses for fork()
by busunsl (Vicar) on Jul 02, 2001 at 11:46 UTC
|
I use fork in my db-stress program. The purpose is to put a heavy load on a DBMS and this is best done by several processes.
Apart from that I use fork for servers that must be able to accept more than one connection at a time. | [reply] |
Re: Practical, efficient uses for fork()
by Zaxo (Archbishop) on Jul 02, 2001 at 12:22 UTC
|
A child may be forked to send a signal to the parent at intervals. That can provide a heartbeat for real-time scheduling, a testbench for the parent's sig handlers, or a source of random activity for a simulation. The advantage of fork() in these applications is that the signal is independent of the parent's state of execution.
After Compline, Zaxo
| [reply] |