Yes, fork would yield a different process ID, but that doesn't matter... the child would not be re-executing the print $$;... there is hidden behavior (called buffering) underlying the standard print operation which causes something more analogous (not exacly the same) to this:
print "PID is $$"; fork(); ... # that is (or can be) essentially the same as: $buffer = ">> PID is $$"; fork(); print $buffer;
so the both the parent and the child process are printing the value as it was computed in the parent process.

Get it?

Anyway, after looking at what you just had to say about the script re-exec'ing itself, it seems obvious that what is happening is not caused by the phenomenon I was describing (the pid getting caught in the output buffer at time of fork and thereby getting printed twice). The problem is that you are exec'ing the same script again from within the same process (so it's the same pid, even though it's a fresh run of the program).

Understand how exec works: it replaces the contents of the current process with a new program... but it retains the same process (hence the same pid). Exec is, essentially, loading the new program into the current process's memory and calling goto(begin). Now, that means that if you exec the same program that you call you exec from, then it's essentially just pressing the reset-button on your program (but, again, from within the same process, hence the same pid).

Hope that helps.

------------ :Wq Not an editor command: Wq

In reply to Re: Re: Re: main script invoked twice by etcshadow
in thread main script invoked twice by steves

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.