Home > Archive > PerlTk > January 2007 > Forking in win32 causes crash in perl58.dll?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Forking in win32 causes crash in perl58.dll?
|
|
| mnooning 2007-01-16, 7:03 pm |
| Hello,
This is to promulgate/add information to an old, closed out thread that
has the subject line "Forking in win32 causes crash in perl58.dll?".
It was very helpful. Basically it shows that on Windows, fork() and Tk
do not mix. The answer was that Tk was not thread safe.
I would like to supply a stripped down test that highlights a Windows
fork/Tk problem in case Tk ever becomes thread safe. I would also like
to supply yet another work-around.
At the end of this post is the code that will show that the sleep
command in the test code will hang, taking up 98% of my cpu time on a
Windows XP, SP2, perl 5.8.8, Tk ...027 machine. Change the "sleep" to
a "select", as seen commented out, and the code will work fine. For
small, non-production scripts it seems a good solution.
I would not use the fork/Tk work-around in production code. After all,
if the sleep (signal dependent) will no longer work, well then, what
else will no longer work? Too deep a question for me.
If Tk is ever changed so that it is thread safe, and the test works, it
will merit at least some confidence in using fork and Tk in the same
script.
-----------paste test code
#!/usr/bin/perl -w
# file: test_windows_fork_w_tk.pl
# BEWARE: This hangs on Windows XP, SP2, perl 5.8.8, Tk ...027
use Tk;
###################
sub okay_response {
my ($we_top) = @_;
$we_top->destroy;
}
###################
sub pop_up_a_message {
my ( $message, ) = @_;
my $okay_button;
my $we_top = new MainWindow;
$we_top->Label
(
-text => $message . "\n",
-justify => 'left',
)->pack(-padx =>10);
#.....................................................................
$okay_button =
$we_top->Button( -text => "Okay",
-command => [ \&okay_response,
$we_top,
]
)->pack(-ipadx =>10);
#.....................................................................
#########
MainLoop;
#########
}
###################
if (my $pid = fork()) { # Parent
print "110:\n";
}
elsif(defined $pid) { # Child
print "120: Child\n";
exit;
} else {
# Failed to fork
print "130: Failed to fork:$!:\n";
exit;
}
#...........................................
print "140: Hello\n";
pop_up_a_message("445: Hello\n");
sleep(2); # This hangs, taking up 98% of cpu time
# select(undef, undef, undef, 2); # This works
print("150: Done\n");
-----------end paste code
| |
| Robert Hicks 2007-01-17, 4:07 am |
|
mnooning wrote:
> Hello,
>
> This is to promulgate/add information to an old, closed out thread that
> has the subject line "Forking in win32 causes crash in perl58.dll?".
> It was very helpful. Basically it shows that on Windows, fork() and Tk
> do not mix. The answer was that Tk was not thread safe.
>
I was under the impression that fork() didn't work at all on Windows
(with the possible exception of a cygwin install).
Robert
| |
| Dean Arnold 2007-01-17, 4:07 am |
| Robert Hicks wrote:
> mnooning wrote:
>
> I was under the impression that fork() didn't work at all on Windows
> (with the possible exception of a cygwin install).
>
> Robert
>
As of Perl 5.8 (5.6 ?), it is emulated via threads...which is
part of the reason for ithreads' current architecture.
Alas, it is of limited value due to issues such as the OP noted.
One possible alternative is Win32::Process, tho it does
not replicate the parent's environment as fork() does;
rather, its more like fork/exec w/ close-on-exec.
FWIW: I suspect it will require a Herculean effort to make
pTk thread safe (Slaven's excellent skills notwithstanding).
I'm trying to build an alternate solution with Tk::Threaded
(www.presicient.com/tkthrd), but I've got an Augean stable
of my own to clear before its done...tho an early POC
implementation shows much promise.
Dean Arnold
Presicient Corp.
| |
| mnooning 2007-01-17, 7:01 pm |
| > I was under the impression that fork() didn't work at all on Windows
It did not work in the past. I now have Perl 5.8.8 and with things
that are thread-safe (which excludes Tk) it seems to work just fine.
|
|
|
|
|