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

hey guys i have written a simple perl script to test the perl threading ability as follow, you can see i span 3 threads each simply sleeps for 20 secs. but i end up seeing 5 indentical PROCESSES running in my box, those are supposingly threads now ended up as forked children and i really have no idea why.... the cose itself ============================================
#!/usr/bin/perl use Config; $Config{useithreads} || die "Recompile Perl with threads to run this p +rogram."; use threads; #use threads::shared; #use Thread::Running; #use Thread::Signal; #use Thread::Queue; sub ithread { my $i; my $arg=shift||''; print "just received - $arg\n"; foreach(1...20) { print $i++, "\n"; sleep(1); } } foreach(1...3) { my $thread = threads->create( "ithread", "arg" ); } foreach(1...30) { sleep(1); print "running..\n"; } exit;
========================================== output of ps -aux ============================================ root 12882 1.0 0.0 11752 3504 pts/0 S 21:07 0:00 /usr/bin/perl /root/threads.pl root 12885 0.0 0.0 11752 3504 pts/0 S 21:07 0:00 /usr/bin/perl /root/threads.pl root 12886 0.0 0.0 11752 3504 pts/0 S 21:07 0:00 /usr/bin/perl /root/threads.pl root 12887 0.0 0.0 11752 3504 pts/0 S 21:07 0:00 /usr/bin/perl /root/threads.pl root 12888 0.0 0.0 11752 3504 pts/0 S 21:07 0:00 /usr/bin/perl /root/threads.pl r =============================================

Replies are listed 'Best First'.
Re: (supposingly) threads now being fork()'ed...
by polettix (Vicar) on Jul 21, 2005 at 07:59 UTC
    This makes perfectly sense, at least if you're in Linux - try using ps afx to see the process list. In Linux, every thread shows up as a different process, even if the creation process uses the clone system call that makes the right thing. This allows the creation of user space thread libraries, even if it has drawbacks which make the implementation depart significantly from POSIX in some areas.

    When you use threads in Linux, a special control thread is created to manage all the other subthreads; moreover, you have to take into account the main thread, i.e. the one that starts it all:

    my-test-program.pl # Main thread \_ my-test-program.pl # Control thread \_ my-test-program.pl # 1st subthread \_ my-test-program.pl # 2nd subthread \_ my-test-program.pl # 3rd subthread
    so they sum up to 5.

    As a side note, please take a look to the Writeup Formatting Tips.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: (supposingly) threads now being fork()'ed...
by GrandFather (Saint) on Jul 21, 2005 at 05:43 UTC

    Under Windows using 5.8.6 I get the following (using a slightly modified version of your code):

    just received - 1 0 1 just received - 2 0 2 just received - 3 0 3 1 1 1 2 running.. 1 3 2 1 2 2 running.. 2 3 3 1 3 2 running.. 3 3 4 1 4 2 running.. 4 3 5 1 5 2 5 3 running.. 6 1 6 2 running.. 6 3 7 1 7 2 running.. 7 3 8 1 8 2 running.. 8 3 9 1 9 2 running.. 9 3 running.. running.. running.. running.. running.. running..

    Perl is Huffman encoded by design.