demerphq has asked for the wisdom of the Perl Monks concerning the following question:
I've got some questions about multi-processing and threading and stuff under Win32. My questions are primarily aimed at 5.6.1 and not the new threading stuff in 5.8.x. In short im curious what the differences are of using fork() and using system(1,...) on a multi CPU Win32 box. My understanding is that fork() on win32 is implemented using threading whereas system(1,...) will spawn a full new process. So does this mean for instance that if a forked process dies it takes down the whole thing? As far as I can tell under system(1,...) it won't. Likewise various hazy questions regarding processor affinity and the like come to mind.
Anyway, part of this is motivated because of a buffer overrun bug with system(1,STRING) where string can be no more than 257 characters or perl segfaults. Can i replace my use of this command with forking safely? Bear in mind each process needs DB connections etc so if forking has problem there i cant use it.
Anyway, here is an example of system(1,STRING) and potentially of the buffer overrun bug:
use strict; use warnings; use POSIX qw(:sys_wait_h); my $segfault=0; # set this to 188 to make perl segfault under 5.6.1 print $$,$/; $|++; my @pid; foreach my $i (0..3) { my $cmd='perl -le "print qq(hello world!); sleep ' .(10+$i*2) .(';' x $segfault ) .'; print qq(foo); " >NUL 2>&1'; print length($cmd),$/; $pid[$i]=system(1,$cmd); } my @wait=(0) x 4; my $c=0; while (grep !$_,@wait) { print(qq(running... @{[$c++]} [@wait]\r)); foreach my $i (0..3) { $wait[$i]||=waitpid($pid[$i],&WNOHANG); } sleep(1); } print(qq(Finished: @{[$c++]} [@wait]\r));
Thanks in advance for your help and time.
First they ignore you, then they laugh at you, then they fight you, then you win.
-- Gandhi
• Update:
Added the missing use() that confused browserUk
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Multi- Processing / threading under Win32
by BrowserUk (Patriarch) on Sep 01, 2004 at 17:42 UTC | |
|
Re: Multi- Processing / threading under Win32
by ikegami (Patriarch) on Sep 02, 2004 at 00:48 UTC | |
|
Re: Multi- Processing / threading under Win32
by BrowserUk (Patriarch) on Sep 01, 2004 at 19:05 UTC | |
by demerphq (Chancellor) on Sep 01, 2004 at 19:08 UTC |