Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > fork/exec question

Reply
Thread Tools

fork/exec question

 
 
Dave Saville
Guest
Posts: n/a
 
      12-07-2013
In C terms a fork/exec of foo execing bar, where both are executables,
will show in ps two processes foo and bar and IIRC the parent pid of
bar will be foo's.

I have a perl script that forks and execs.

my $pid = fork()
die "Can't fork $!" if ! defined $pid;

unless ( $pid )
{
# child

exec "mplayer.........

}

print "$pid\n";

.....

Assume $pid is 123 If I run a ps I get

pid ppid prog
100 *** cmd
101 100 perl
123 100 perl
124 123 sh
125 124 sh
126 125 mplayer

Which is not what I would have expected.

I would have expected

100 *** cmd
101 100 perl
123 101 mplayer

Or very close. Of course this could be another OS/2 oddity

TIA
--
Regards
Dave Saville
 
Reply With Quote
 
 
 
 
Dave Saville
Guest
Posts: n/a
 
      12-08-2013
On Sun, 8 Dec 2013 05:56:58 UTC, Ben Morrow <(E-Mail Removed)> wrote:

Hi Ben

>
> Quoth "Dave Saville" <(E-Mail Removed)>:
> > In C terms a fork/exec of foo execing bar, where both are executables,
> > will show in ps two processes foo and bar and IIRC the parent pid of
> > bar will be foo's.
> >
> > I have a perl script that forks and execs.
> >
> > my $pid = fork()
> > die "Can't fork $!" if ! defined $pid;
> >
> > unless ( $pid )
> > {
> > # child
> >
> > exec "mplayer.........
> >
> > }
> >
> > print "$pid\n";
> >
> > ....
> >
> > Assume $pid is 123 If I run a ps I get
> >
> > pid ppid prog
> > 100 *** cmd
> > 101 100 perl
> > 123 100 perl
> > 124 123 sh
> > 125 124 sh
> > 126 125 mplayer
> >
> > Which is not what I would have expected.

>
> You're using 1-arg exec (tut). This is roughly equivalent to


Assumption based on an incomplete line of code But in ths case
true.

>
> sub exec {
> my ($cmd) = @_;
>
> if ($cmd =~ /\Q$&*(){}[]'";\\|?<>~`\n/) {
> exec "sh", "-c", $cmd;
> }
> else {
> exec split " ", $cmd;
> }
> }
>
> that is: if using a shell will make a difference, run it with the shell,
> otherwise split on whitespace and run it directly. This is exactly the
> same behaviour as system(), where it more closely matches what a C
> programmer would expect, and it allows you to include shell redirections
> in your command. (And, in fact, you must have done so, or perl wouldn't
> have used the shell.)
>


No redirections in the command given to exec.

> It's better in general to avoid this by using multi-arg system, which
> always runs the command directly. Obviously in that case you have to do
> any redirections yourself, between fork and exec; sometimes it's not
> worth it.


We have established in the past, although possibly not in this NG,
that the OS/2 port often screws up passing parameters when not used in
1 arg mode.

But it still leaves the original question - why is there still perl
and a couple of sh's in the process chain? Or does perl exec never
actually call C exec and just fake it?
--
Regards
Dave Saville
 
Reply With Quote
 
 
 
 
Dave Saville
Guest
Posts: n/a
 
      12-08-2013
On Sat, 7 Dec 2013 16:16:07 UTC, "Dave Saville" <(E-Mail Removed)>
wrote:

> In C terms a fork/exec of foo execing bar, where both are executables,
> will show in ps two processes foo and bar and IIRC the parent pid of
> bar will be foo's.
>
> I have a perl script that forks and execs.
>
> my $pid = fork()
> die "Can't fork $!" if ! defined $pid;
>
> unless ( $pid )
> {
> # child
>
> exec "mplayer.........
>
> }
>
> print "$pid\n";
>
> ....
>
> Assume $pid is 123 If I run a ps I get
>
> pid ppid prog
> 100 *** cmd
> 101 100 perl
> 123 100 perl
> 124 123 sh
> 125 124 sh
> 126 125 mplayer
>
> Which is not what I would have expected.
>
> I would have expected
>
> 100 *** cmd
> 101 100 perl
> 123 101 mplayer
>
> Or very close. Of course this could be another OS/2 oddity



Just run this on a *nix box and I get, almost, what I expected.

1493 1405 bash
1593 1493 perl
1594 1593 sh -c mplayer
1595 1594 mplayer

Ben has explained where the sh comes from ( 1 arg exec ) so I guees it
is an OS/2 funny.
--
Regards
Dave Saville
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      12-08-2013
On 2013-12-08 10:27, Dave Saville <(E-Mail Removed)> wrote:
> On Sun, 8 Dec 2013 05:56:58 UTC, Ben Morrow <(E-Mail Removed)> wrote:
>> Quoth "Dave Saville" <(E-Mail Removed)>:


[fork/exec on OS/2]

>> > Assume $pid is 123 If I run a ps I get
>> >
>> > pid ppid prog
>> > 100 *** cmd
>> > 101 100 perl
>> > 123 100 perl
>> > 124 123 sh
>> > 125 124 sh
>> > 126 125 mplayer
>> >
>> > Which is not what I would have expected.

[...]
>> It's better in general to avoid this by using multi-arg system, which
>> always runs the command directly. Obviously in that case you have to do
>> any redirections yourself, between fork and exec; sometimes it's not
>> worth it.

>
> We have established in the past, although possibly not in this NG,
> that the OS/2 port often screws up passing parameters when not used in
> 1 arg mode.
>
> But it still leaves the original question - why is there still perl
> and a couple of sh's in the process chain? Or does perl exec never
> actually call C exec and just fake it?


Does OS/2 even have an exec system call? I'm pretty sure it doesn't have
fork, so what ps shows you may not be real processes but threads.

hp


--
_ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
|_|_) | | Man feilt solange an seinen Text um, bis
| | | http://www.velocityreviews.com/forums/(E-Mail Removed) | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
 
Reply With Quote
 
Dave Saville
Guest
Posts: n/a
 
      12-08-2013
On Sun, 8 Dec 2013 10:53:04 UTC, "Peter J. Holzer"
<(E-Mail Removed)> wrote:

<Snip>
> Does OS/2 even have an exec system call? I'm pretty sure it doesn't have
> fork, so what ps shows you may not be real processes but threads.


It has both - but fork is *really* slow and inefficient compared to
*nix.
--
Regards
Dave Saville
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      12-08-2013
"Dave Saville" <(E-Mail Removed)> writes:
> On Sun, 8 Dec 2013 10:53:04 UTC, "Peter J. Holzer"
> <(E-Mail Removed)> wrote:
>
> <Snip>
>> Does OS/2 even have an exec system call? I'm pretty sure it doesn't have
>> fork, so what ps shows you may not be real processes but threads.

>
> It has both - but fork is *really* slow and inefficient compared to
> *nix.


I haven't done anyting for or with OS/2 for a long time (17 - 18 years)
but I still remember that OS/2 has neither fork nor exec. The native
call is called DosExecPgm and perform this 'usual' create a new process
to run a different program functionality, cf

http://www.edm2.com/os2api/
http://www.edm2.com/os2api/Dos/DosExecPgm.html

EMX provides a fork call at the library level but it is not really
documented how this works. But considering the lack of OS-support, what
it will very likely do is use DosExecPgm to start a bootstrap program
making an actual of the process which called fork, similar to the way
fork used to work on UNIX(*) prior to virtual memory.




 
Reply With Quote
 
Dave Saville
Guest
Posts: n/a
 
      12-09-2013
On Sun, 8 Dec 2013 18:16:22 UTC, Rainer Weikusat
<(E-Mail Removed)> wrote:

> "Dave Saville" <(E-Mail Removed)> writes:
> > On Sun, 8 Dec 2013 10:53:04 UTC, "Peter J. Holzer"
> > <(E-Mail Removed)> wrote:
> >
> > <Snip>
> >> Does OS/2 even have an exec system call? I'm pretty sure it doesn't have
> >> fork, so what ps shows you may not be real processes but threads.

> >
> > It has both - but fork is *really* slow and inefficient compared to
> > *nix.

>
> I haven't done anyting for or with OS/2 for a long time (17 - 18 years)
> but I still remember that OS/2 has neither fork nor exec. The native
> call is called DosExecPgm and perform this 'usual' create a new process
> to run a different program functionality, cf
>
> http://www.edm2.com/os2api/
> http://www.edm2.com/os2api/Dos/DosExecPgm.html
>
> EMX provides a fork call at the library level but it is not really
> documented how this works. But considering the lack of OS-support, what
> it will very likely do is use DosExecPgm to start a bootstrap program
> making an actual of the process which called fork, similar to the way
> fork used to work on UNIX(*) prior to virtual memory.


Hi Rainer

Apologies - We now have a much later compiler than EMX, based on gcc
4.4.6 and I assumed it's LIBC had an exec(). Never tried it actually
but use fork a fair bit. A quick test on both EMX and the latter
yields the same - a duplicated process tree running the "execed"
program. You are correct about the way fork works - I said it was slow

--
Regards
Dave Saville
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
os.fork and pty.fork Eric Snow Python 0 01-08-2009 06:32 AM
Can't fork exec from webpage, problem is not permissions Patrick Perl 1 05-14-2004 12:03 PM
HP - fork & exec in Threads hang - intermittently Ajay Bakhshi Python 0 05-03-2004 05:44 PM
fork, exec, and disown Benoit Dejean Python 4 02-09-2004 04:34 PM
pipe(), fork(), exec(), ... providing input to and capturing output of a child process Hoegje C++ 2 12-05-2003 04:03 PM



Advertisments