in reply to Parallel Processing on win10

Btw I am not sure if this is off-topic or not, but could someone please give me a practical concrete example where a fork() is useful? In all of my programs I have ever written in 30 years I have never run into a situation where I would want my program to duplicate itself in memory and start doing the exact same thing twice. I see how this is a COOL feature just like a computer virus is a cool thing, but I don't see why it's necessary or useful.

Replies are listed 'Best First'.
Re^2: Parallel Processing on win10
by haukex (Archbishop) on May 13, 2023 at 07:05 UTC
    a situation where I would want my program to duplicate itself in memory and start doing the exact same thing twice ... I don't see why it's necessary or useful

    The point of fork is not really for the parent and child to do the exact same thing. If you look at its documentation, you'll see that the parent and child get different return values, allowing them to follow different code paths. Typically, the parent will fork multiple children and then manage them, while the children will be doing various tasks. This scheme is widely used in many web servers, see e.g. preforking server. For just two examples of many in Perl, see Parallel::ForkManager and Net::Server.

Re^2: Parallel Processing on win10
by karlgoethebier (Abbot) on May 14, 2023 at 08:45 UTC

    For example, certain calculations could profit from it if one distributes them on several CPUs. What else do you have the expensive gizmos for? Here is a simple, admittedly somewhat pointless example:

    #!/usr/bin/env perl use strict; use warnings; use Math::BigInt; use Parallel::ForkManager; use Time::HiRes qw ( time ); use feature qw(say); my $sleep_period = shift; my $processes = qx(sysctl -n hw.ncpu); chomp $processes; my $pm = Parallel::ForkManager->new($processes); $pm->set_waitpid_blocking_sleep($sleep_period); say qq(processes: $processes); my $start = time; for my $number ( 1 .. 50 ) { $pm->start($number) and next; my $factorial = Math::BigInt->bfac(2000) for 1 ... 10; $pm->finish(0); } $pm->wait_all_children; say qq(fork: ), time - $start;

    See also this eight-year-old thread from which the example comes.

    «The Crux of the Biscuit is the Apostrophe»