Running non-blocking commands.

M

Mike Kamzyuk

Hello. I use perl on windows. Please don't post anything related to
unix/linux/bsd/etc.

Here's my problem. I have a script which is trying to run another
script. I need to run the second script without blocking the first
one. The second script needs to share nothing with the first one -
that is, it should have its own STDIN, STDOUT, PID, etc. I don't need
to access anything about the second script from the first one (although
it's not harmful either). I also don't need to access anything about
the first perl script from the second one (although that's also not
harmful).

How can this be accomplished? (Short of writing a c++ exe that does it
and calling it from the first script :)

P.S.
Perl cookbook's example doesn't work. I've tried it way too many
times, trust me.
Forking appears to hang both processes. I've done an obvious if
statement there.


Thanks in advance,

Mike.
 
U

usenet

Mike said:
P.S.
Perl cookbook's example doesn't work. I've tried it way too many
times, trust me.

Yeah, you can't trust anything that Tom Christiansen guy writes. But
humor us... present the code you tried to run and tell us what happened
when you ran it.
 
A

A. Sinan Unur

Hello. I use perl on windows. Please don't post anything related to
unix/linux/bsd/etc.

Here's my problem. I have a script which is trying to run another
script. I need to run the second script without blocking the first
one. The second script needs to share nothing with the first one -
that is, it should have its own STDIN, STDOUT, PID, etc. I don't need
to access anything about the second script from the first one
(although it's not harmful either). I also don't need to access
anything about the first perl script from the second one (although
that's also not harmful).

How can this be accomplished? (Short of writing a c++ exe that does
it and calling it from the first script :)

Lucky for you, one has already been written. It is called start.
P.S.
Perl cookbook's example doesn't work.

Which example are you talking about?

In what way does it not work.
I've tried it way too many times, trust me.

I won't until you have shown exactly what you are doing, which example
you are talking about, and in what way "it doesn't work".
Forking appears to hang both processes. I've done an obvious if
statement there.

That does not mean anything.
Thanks in advance,

Please read the posting guidelines for this group to learn how you can
help yourself, and help others help you.

Sinan
 
M

Mike Kamzyuk

Here's the example I used. Assume $cmd is the command i'm trying to
run.

FORK:
if ($pid = fork) {
#parent here. $pid is now the child id.
# do nothing.
}
elsif (defined $pid) {
#child here
`$cmd`;
die "finished running child"; # i don't care about the exit code.
}
elsif ($! = EAGAIN) {
#supposedly i can recover
sleep(2);
redo FORK;
}
else {
die "can't fork.";
}


Now, here's the result i'm seeing.

Suppose this is the first script:
..... #stuff called A
the forking call
..... #stuff called B.

The second script completes in full, but does not die.
The first script does not run any code from B.

Oh, if i kill the second script during the hang, the first one proceeds
to B.
 
U

usenet

Mike said:
Here's the example I used....

I see you've ignored Sinan's advice to read the posting guidelines.
These guidelines are for your benefit - they help people help you
better. If you had read them, you would have seen this:

You further ignored the guidelines advice:
Had you "use warnings", you would have been made aware of a syntax
problem in your code.
Now, here's the result i'm seeing.
Suppose this is the first script:
.... #stuff called A
the forking call
.... #stuff called B.

That doesn't mean anything.

You may wish to read up on the difference between using backticks and
the "system" function.

Since you did not post a complete program, I made some assumptions.
This almost works (I left your syntax error intact to encourage you to
discover it for yourself with the "use warnings" pragma):

#!/usr/bin/perl
use warnings;
use strict;

FORK:
if (my $pid = fork) {
#parent here. $pid is now the child id.
my $cmd = "ls /var/tmp >/tmp/junk.$$.txt";
system $cmd;
print "finished running parent $$"; # i don't care about the exit
code.
}

elsif (defined $pid) {
#child here
my $cmd = "ls /var/tmp >/tmp/junk.$$.txt";
system $cmd;
die "finished running child $$\n"; # i don't care about the exit
code.
}

elsif ($! = EAGAIN) {
#supposedly i can recover
sleep(2);
redo FORK;
}

else {
die "can't fork.";
}


When I run this (without the syntax error), I get:

finished running child 37896
finished running parent 3188

and, indeed, I get two files:
/tmp/junk.37896.txt
/tmp/junk.3188.txt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top