problem with system() and fork

  • Thread starter gabriele.ravanelli
  • Start date
G

gabriele.ravanelli

Hi, I've got a problem whit fork() and system()
I use the fork() function to fork my program and inside the code I use
the funtion sytem() to call a program and to get the return status of
this one.
the problem is that the function system don't work if I use the fork()
before. In this case the ret valute is always -1. Why system() works
wrong?
Thaks, Gabriele
 
A

A. Sinan Unur

(e-mail address removed) wrote in
Hi, I've got a problem whit fork() and system()
I use the fork() function to fork my program and inside the code I use
the funtion sytem() to call a program and to get the return status of
this one.
the problem is that the function system don't work if I use the fork()
before. In this case the ret valute is always -1. Why system() works
wrong?

Please read the posting guidelines for this group. After that, post a
short, but complete script we can run that still exhibits the problem you
are experiencing.

Sinan
 
J

jack

A. Sinan Unur said:
(e-mail address removed) wrote in


Please read the posting guidelines for this group. After that, post a
short, but complete script we can run that still exhibits the problem you
are experiencing.

Sinan


Sinan,

May I suggest that when you refer people to the posting guidelines,
that you include the link in your response, *before* you 'sign' the
response. Having the link in what appears to be a signature line is an
easy way to not have it noticed. I ignore signature lines because most
of the time they contain gibberish that's supposed to be humorous, PGP
keys, or some religious message which I have no interest in reading.

In short, anything after your name at the end of the comment is likely
to be ignored.
 
P

Paul Lalli

Hi, I've got a problem whit fork() and system()
I use the fork() function to fork my program and inside the code I use
the funtion sytem() to call a program and to get the return status of
this one.
the problem is that the function system don't work if I use the fork()
before. In this case the ret valute is always -1. Why system() works
wrong?

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

print "About to fork...\n";
my $pid = fork();

if ($pid == 0){
print "In the child\n";
my $ret_val = system('ls') == 0 or die "Child cannot execute `ls`:
$?\n";
print "Child's retval: $ret_val\n";
} else {
print "In the parent\n";
my $ret_val = system('ls') == 0 or die "Parent cannot execute `ls`:
$?\n";
print "Parent's retval: $ret_val\n";
}
__END__

$ ./clpm.pl
About to fork...
In the parent
In the child
clpm.pl
Child's retval: 1
clpm.pl
Parent's retval: 1
$

Seems to work fine for me. Perhaps you could post a short-but-complete
program that exhibits the problem you're seeing?

Paul Lalli
 
P

Paul Lalli

Paul said:
#!/usr/bin/perl
use strict;
use warnings;

print "About to fork...\n";
my $pid = fork();

if ($pid == 0){
print "In the child\n";
my $ret_val = system('ls') == 0 or die "Child cannot execute `ls`: $?\n";

Arg. Why didn't I see that precedence error before I posted?
(my $retval = system('ls')) == 0 or die ...
print "Child's retval: $ret_val\n";
} else {
print "In the parent\n";
my $ret_val = system('ls') == 0 or die "Parent cannot execute `ls`: $?\n";

(my $retval = system('ls')) == 0 or die ...
print "Parent's retval: $ret_val\n";
}
__END__

$ ./clpm.pl
About to fork...
In the parent
In the child
clpm.pl
Child's retval: 1 Child's retval: 0
clpm.pl
Parent's retval: 1 Parent's retval: 0
$

Sorry about that.

Paul Lalli
 
A

A. Sinan Unur

A. Sinan Unur wrote:

[original post snipped]
Sinan,

May I suggest that when you refer people to the posting guidelines,
that you include the link in your response, *before* you 'sign' the
response.
....

The reason people put this stuff in their sigs is so they don't
have to type it in or paste it each time.

In addition, the guidelines are posted here very regularly, and people
are expected to at least browse the archives before posting.

When a poster receives a suggestion that s/he read the guidelines, and
s/he cannot see a link, it does not take too much effort to type

comp.lang.perl.misc guidelines

in the Google search box.

Sinan
 
C

ced

Hi, I've got a problem whit fork() and system()
I use the fork() function to fork my program and inside the code I use
the funtion sytem() to call a program and to get the return status of
this one.
the problem is that the function system don't work if I use the fork()
before. In this case the ret valute is always -1. Why system() works
wrong?
Thaks, Gabriele

system does an implicit fork and and, if its child has already been
reaped (by the fork parent for instance, the system return is -1.

Another scenario is setting the CHLD handler to ignore.
Same thing: system will always return -1:

$ perl -e '$SIG{CHLD}="IGNORE";system("sleep 1") == 0 or die $?'
-1 at -e line 1.

perldoc -f wait
perldoc perlipc


hth,
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top