Re: Getting around Win32 limitations
by strat (Canon) on Nov 10, 2002 at 10:25 UTC
|
Another idea might be waiting for perl5.8 (the first beta is out by http://www.activestate.com/). 5.8 allows alarm even under Win32, but I haven't yet tested how good it works...
Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32" | [reply] [Watch: Dir/Any] |
Re: Getting around Win32 limitations
by pg (Canon) on Nov 10, 2002 at 03:39 UTC
|
What provided in this reply is garbage, but a small change should largely improve it. Use waitpid, instead of wait. If you call waitpid in this way: waitpid($child, 1), it will not blocking the main process. You just from time to time, come back and checking return code form this call, if it is -1, it means the child process died, and you get an alarm. This can be wrapped into an implementation of alarm, which is not implemented in active perl.
my $child_pid = fork();
if ($child_pid) {
$ret = wait($child_pid, 1);
do {
... #whatever you want;
} until ($ret == -1);
print "got alarm\n";
} else {
sleep($seconds);
}
| [reply] [Watch: Dir/Any] [d/l] |
|
alarm_pg.pm:
package pg_alarm;
use strict;
sub new {
shift;
my $seconds = shift;
my $self = {};
$self->{CHILD_PID} = fork();
if ($self->{CHILD_PID} == 0) {
sleep($seconds);
}
bless $self;
return $self;
}
sub alarmed {
my $self = shift;
my $result = 0;
if (defined($self->{CHILD_PID})) {
my $ret = waitpid($self->{CHILD_PID}, 1);
if ($ret == -1) {
$result = 1;
}
}
return $result;
}
1;
test.pl:
use pg_alarm;
use strict;
my $a = pg_alarm->new(10);
while (!$a->alarmed()) {
#print "haven't gotten alarm yet\n";
}
print "got alarm\n";
| [reply] [Watch: Dir/Any] [d/l] |
Re: Getting around Win32 limitations
by pg (Canon) on Nov 10, 2002 at 17:28 UTC
|
I realized that, my alarm_pg.pm has a problem. I have to add one line, highted below, so that the child process would stop right after the sleep. Otherwise, the child process will just continue to do whatever the program write for his main process. After a while, you will have lots of child processes running, and killing CPU's.
alarm_pg.pm:
package pg_alarm;
use strict;
sub new {
shift;
my $seconds = shift;
my $self = {};
$self->{CHILD_PID} = fork();
if ($self->{CHILD_PID} == 0) {
sleep($seconds);
exit;
}
bless $self;
return $self;
}
sub alarmed {
my $self = shift;
my $result = 0;
if (defined($self->{CHILD_PID})) {
my $ret = waitpid($self->{CHILD_PID}, 1);
if ($ret == -1) {
$result = 1;
}
}
return $result;
}
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Getting around Win32 limitations
by pg (Canon) on Nov 10, 2002 at 02:46 UTC
|
Too bad, I hate the differences among platforms. Maybe this trick here can help:
sub alarm
my $child_pid = fork();
if ($child_pid) {
wait;
print "received alarm\n";
...
} else {
sleep($seconds);
}
| [reply] [Watch: Dir/Any] [d/l] |
Re: Getting around Win32 limitations
by Brutha (Friar) on Nov 10, 2002 at 18:50 UTC
|
Be carefull with fork and signals under Windows.
I did not get sensible results with that, it blocks or did not return (I did not try waitpid)
so I stuck with the non-portable Win32-API, which changed the whole
design of my application.
All cookbook and other demos do work under Linux at home, but you
have to test the basics under windows.
Start a little test program before you think about a 'normal' design.
Special OS need special treatment.
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: Perl is like (was Re: Getting around Win32 limitations)
by Jenda (Abbot) on Nov 10, 2002 at 16:32 UTC
|
I'd say ... Perl is like sex;, if you are doing it wrong, you may have fun, but the ones that try to do it with YOU will not.
Jenda
| [reply] [Watch: Dir/Any] |
|
And this is relevant how?
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: Getting around Win32 limitations
by bart (Canon) on Nov 12, 2002 at 01:59 UTC
|
OK, fill me in. What File::Find issues? For all I know, File::Find works fine on Win32. Well, I had a case where it was so dead slow... that I made something very similar but based on Win32::API calls, and with an underlying directory entry object. You're welcome to the code, if that is indeed your problem.
| [reply] [Watch: Dir/Any] |