BernieC has asked for the wisdom of the Perl Monks concerning the following question:

I'm a little confused by this statement in the perlfork page
The fork() emulation is implemented at the level of the Perl interpreter. What this means in general is that running fork() will actually clone the running interpreter and all its state, and run the cloned interpreter in a separate thread, beginning execution in the new thread just after the point where the fork() was called in the parent. We will refer to the thread that implements this child "process" as the pseudo-process.
I'm not sure what "separate thread" means. I have a six processor win10 system and I have a very CPU intensive program that I can easily carve into parallel processes. If I just fork() them, will these process be able to run in parallel on separate processors?

Replies are listed 'Best First'.
Re: Parallel Processing on win10
by marioroy (Prior) on May 12, 2023 at 18:56 UTC
    If I just fork() them, will these process be able to run in parallel on separate processors?

    Yes. For fork(), the limit is 64 pseudo-processes on the Windows platform; $^O eq 'Win32'. There's no limit using threads.

Re: Parallel Processing on win10
by eyepopslikeamosquito (Archbishop) on May 12, 2023 at 23:15 UTC

    > I have a six processor win10 system and I have a very CPU intensive program that I can easily carve into parallel processes

    I advise you to stay away from Perl's fork emulation on Windows, too many surprises and general weirdness. There are better alternatives. If you provide more detail on the specific problem you are trying to solve, we will be able to better advise you.

    In case it's of use, I keep a list of general references on this topic: Threading and Concurrency References

    Update: I see from my list of references that you asked a similar question last year: Parallel processing on Windows ... and another one earlier this year: fork() on win10. It would have been nice if you'd mentioned that in your post and told us what you've tried from the responses to those threads ... and told us more about your CPU intensive program.

Re: Parallel Processing on win10
by ikegami (Patriarch) on May 14, 2023 at 15:49 UTC

    Yes, it creates a real thread, which can be assigned to a different CPU by the OS.

    BUT! You'd be better off avoiding the very poor emulation of fork. This could be done, for example, by using threads directly.

Re: Parallel Processing on win10
by eyepopslikeamosquito (Archbishop) on May 14, 2023 at 00:18 UTC

    > I have a very CPU intensive program that I can easily carve into parallel processes

    Please tell us more about this program:

    • Did you write it yourself? Do you have its source code?
    • What language is it written in? How many lines of code?
    • What do you use it for?

Re: Parallel Processing on win10
by harangzsolt33 (Deacon) on May 13, 2023 at 03:21 UTC
    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.
      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.

      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»