forking an independent process

B

bxb7668

From my perl script I need to be able to launch (fork?) an editor
(notepad on Windows and nedit on UNIX). (Easy so far.) I need the
script to processing and the editor to keep editing totally
independent of each other. The script can end and the editor will stay
open. The editor can be closed and the script will keep running.
Reading up on fork gave me this:

#!/usr/bin/perl
FORK: {
if ($pid = fork) {
$I = 1;
while ( 1 ) {
print "hi $I\n";
sleep 1;
$I++;
}
}elsif (defined $pid) {
exec "nedit junk.txt";
}elsif ( $! =~ /no more process/) {
sleep 5;
redo FORK;
}else {
die "Can't fork";
}
}

When I close the child nedit process, the parent keeps going. But when
I close the parent with a Ctrl+C, it also kills the nedit window. Is
what I'm trying for possible? If so, how?

Thank you,
Brian
 
A

Ala Qumsieh

bxb7668 said:
From my perl script I need to be able to launch (fork?) an editor
(notepad on Windows and nedit on UNIX). (Easy so far.) I need the
script to processing and the editor to keep editing totally
independent of each other. The script can end and the editor will stay
open. The editor can be closed and the script will keep running.

I wouldn't even go with fork() here:

# untested code:
if ($^O eq 'MSWin32') {
system 'start notepad.exe';
} else { # unix
system 'nedit &';
}

Unless I misunderstood you, that should be enough.

--Ala
 
S

Salvador Fandiño

bxb7668 said:
From my perl script I need to be able to launch (fork?) an editor
(notepad on Windows and nedit on UNIX). (Easy so far.) I need the
script to processing and the editor to keep editing totally
independent of each other. The script can end and the editor will stay
open. The editor can be closed and the script will keep running.
Reading up on fork gave me this:

use Proc::Background;

- Salvador
 
B

bxb7668

Thank you Ala. That is exactly what I needed.

Ala Qumsieh said:
I wouldn't even go with fork() here:

# untested code:
if ($^O eq 'MSWin32') {
system 'start notepad.exe';
} else { # unix
system 'nedit &';
}

Unless I misunderstood you, that should be enough.

--Ala
 
C

ctcgag

bxb7668 said:
From my perl script I need to be able to launch (fork?) an editor
(notepad on Windows and nedit on UNIX). (Easy so far.) I need the
script to processing and the editor to keep editing totally
independent of each other. The script can end and the editor will stay
open. The editor can be closed and the script will keep running.
Reading up on fork gave me this:

#!/usr/bin/perl
FORK: {
if ($pid = fork) {
$I = 1;
while ( 1 ) {
print "hi $I\n";
sleep 1;
$I++;
}
}elsif (defined $pid) {
exec "nedit junk.txt";
}elsif ( $! =~ /no more process/) {
sleep 5;
redo FORK;
}else {
die "Can't fork";
}
}

When I close the child nedit process, the parent keeps going. But when
I close the parent with a Ctrl+C, it also kills the nedit window. Is
what I'm trying for possible? If so, how?

Don't kill the parent with ctrl+C. The problem probably lies in the shell
from which you execute the perl script. When you hit ctrl+C, the shell
kills the running perl script and everything else in the its process group,
which includes the nedit children. If the parent ends naturally, or is
killed by signal sent specifically to its pid, then nedit stays open.

You may also be able to circumvent the problem by using
system "nedit blah.txt&"
instead of a fork.

Xho
 

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

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top