How do I get the process ID from a system("x &") call?

zakzebrowski gives a partial answer above, and a bit more background might help you understand how to take it from there.

On *nix systems, when you call system() with a single argument, something magic happens under the covers. To handle shell meta-characters, system() invokes a shell, and the shell processes the argument. This muddies the waters. You clearly don't want the shell's process id, but it's gotten in the way. To get the process id of x, you need a finer grain of control, which can be had by an explicit fork()/exec().

After a fork() the parent process gets the process id of the child. The exec() replaces the child copy of the parent process, reusing the process id. There are a couple of things you need to watch out for. One is that if the exec() fails, the parent still thinks it has a valid process id. You can communicate this back to your parent process by a number of means, including an exit() code.


In reply to Re: How do I get the process ID from a system("x &") call? by dws
in thread How do I get the process ID from a system("x &") call? by zakzebrowski

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.