I did some benchmarking of forking vs threading. Below is a program that compares the running times of programs that fork and that use threads. Two programs are compared: first a program where the children don't do much (exit for the fork, empty sub for the threads). In the second program, the children print a single character to /dev/null.
#!/usr/bin/perl use strict; use warnings; my $perl = "/opt/perl/bin/perl"; my $thrperl = "/opt/perl/bin/thrperl"; my $action = << '--'; open my $fh => "> /dev/null" or die "open: $!"; print $fh "."; close $fh; -- my $prog = << '--'; use strict; use warnings; my $times = shift; my @pids; foreach (1 .. $times) { defined (my $pid = fork) or die "fork: $!"; unless ($pid) { ACTION; exit; } push @pids => $pid; } map {waitpid $_ => 0} @pids; -- my $thrprog = << '--'; use strict; use warnings; use threads; my $times = shift; my @threads; foreach (1 .. $times) { my $thread = threads -> create (sub {ACTION}) or die "threads: $!" +; push @threads => $thread; } map {$_ -> join} @threads; -- my @amounts = (0, 5, 10, 25, 50, 100, 250, 500, 1000, 2500); print STDERR "Empty childs\n"; (my $t_prog = $prog) =~ s/ACTION//; (my $t_thrprog = $thrprog) =~ s/ACTION//; for my $amount (@amounts) { my $format = sprintf "%5d: %%U user, %%S system, %%E elapsed" => $ +amount; system time => '-f', "(F) $format", $perl, '-e', $t_prog, $a +mount; system time => '-f', "(T) $format", $thrperl, '-e', $t_thrprog, $a +mount; } print STDERR "\nChilds writing\n"; ($t_prog = $prog) =~ s/ACTION/$action/; ($t_thrprog = $thrprog) =~ s/ACTION/$action/; for my $amount (@amounts) { my $format = sprintf "%5d: %%U user, %%S system, %%E elapsed" => $ +amount; system time => '-f', "(F) $format", $perl, '-e', $t_prog, $a +mount; system time => '-f', "(T) $format", $thrperl, '-e', $t_thrprog, $a +mount; } __END__ Empty childs (F) 0: 0.01 user, 0.00 system, 0:00.01 elapsed (T) 0: 0.04 user, 0.00 system, 0:00.03 elapsed (F) 5: 0.01 user, 0.00 system, 0:00.01 elapsed (T) 5: 0.09 user, 0.00 system, 0:00.09 elapsed (F) 10: 0.01 user, 0.01 system, 0:00.02 elapsed (T) 10: 0.16 user, 0.00 system, 0:00.15 elapsed (F) 25: 0.01 user, 0.02 system, 0:00.03 elapsed (T) 25: 0.32 user, 0.02 system, 0:00.34 elapsed (F) 50: 0.03 user, 0.04 system, 0:00.06 elapsed (T) 50: 0.60 user, 0.08 system, 0:00.70 elapsed (F) 100: 0.04 user, 0.08 system, 0:00.11 elapsed (T) 100: 1.25 user, 0.11 system, 0:01.42 elapsed (F) 250: 0.04 user, 0.12 system, 0:00.50 elapsed (T) 250: 3.41 user, 0.24 system, 0:03.87 elapsed (F) 500: 0.02 user, 0.14 system, 0:01.16 elapsed (T) 500: 7.37 user, 0.55 system, 0:08.42 elapsed (F) 1000: 0.05 user, 0.19 system, 0:02.49 elapsed A thread exited while 46 threads were running. (T) 1000: 15.02 user, 1.36 system, 0:17.42 elapsed (F) 2500: 0.04 user, 0.71 system, 0:06.73 elapsed Command terminated by signal 9 (T) 2500: 37.39 user, 3.30 system, 2:10.56 elapsed Childs writing (F) 0: 0.06 user, 0.21 system, 0:01.60 elapsed (T) 0: 0.20 user, 0.23 system, 0:01.66 elapsed (F) 5: 0.01 user, 0.01 system, 0:00.03 elapsed (T) 5: 0.12 user, 0.02 system, 0:00.28 elapsed (F) 10: 0.03 user, 0.00 system, 0:00.07 elapsed (T) 10: 0.17 user, 0.04 system, 0:00.37 elapsed (F) 25: 0.02 user, 0.06 system, 0:00.08 elapsed (T) 25: 0.49 user, 0.09 system, 0:01.07 elapsed (F) 50: 0.04 user, 0.21 system, 0:00.46 elapsed (T) 50: 0.73 user, 0.12 system, 0:01.72 elapsed (F) 100: 0.04 user, 0.11 system, 0:00.14 elapsed (T) 100: 1.22 user, 0.06 system, 0:01.29 elapsed (F) 250: 0.08 user, 0.12 system, 0:00.52 elapsed (T) 250: 2.93 user, 0.30 system, 0:04.29 elapsed (F) 500: 0.06 user, 0.20 system, 0:01.30 elapsed (T) 500: 6.29 user, 0.67 system, 0:07.79 elapsed (F) 1000: 0.03 user, 0.24 system, 0:02.50 elapsed A thread exited while 46 threads were running. (T) 1000: 11.52 user, 1.46 system, 0:13.84 elapsed (F) 2500: 0.06 user, 0.72 system, 0:06.78 elapsed Command terminated by signal 9 (T) 2500: 26.72 user, 13.98 system, 3:57.20 elapsed

As we can see, forking is much faster than creating threads. Furthermore, creating more than a thousand processes is not a problem - creating a thousand threads *is*. And what you didn't see was the slow response of my system when the threads were being handled, and neither did you hear the grinding of the disk when dealing with threads (2500 processes however was hardly noticeable).

Abigail


In reply to Re: Why use threads over processes, or why use processes over threads? by Abigail-II
in thread Why use threads over processes, or why use processes over threads? by pg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.