Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

by gods
on Feb 11, 2000 at 00:06 UTC ( #3333=superdoc: print w/replies, xml ) Need Help??
Hi,

The demo:
use warnings; #use Inline C => Config => # BUILD_NOISY => 1; use Inline C => <<'EOC'; static int x = 5; void set_x(int z) { x = z; } void get_x(char * id) { printf("%s %d\n", id, x); } EOC get_x('1st call:'); if($pid = fork()) { waitpid($pid,0); } else { set_x(10); get_x('2nd call:'); exit(0); } get_x('3rd call:');
On linux that script fulfils my expectations and outputs:
1st call: 5 2nd call: 10 3rd call: 5
But on Win32, I get:
1st call: 5 2nd call: 10 3rd call: 10
The basic requirement is that the script forks; the child then alters the value of an XS global and exits; that global then still retains its original value.

What options are there that will bring Windows into line with Linux ?
Is there anything that can be done within the C code ?
Perhaps something regarding the way (when/how) the C component is loaded ?

It seems that system() produces a genuine fork, and (as desired) any setting of the global inside a system call is lost when the system call exits.
This is not so straightforward to demo with the Inline::C script, but in the real world scenario I'm dealing with an XS module - and using system instead of fork is one alternative.

But I'm keen to hear what, if any, other options exist - especially any that leave the fork() in place.

Interestingly enough, there's no such problem with *perl* globals. The following script works fine on both Win32 and linux:
use warnings; $g = 5; print "1st call: $g\n"; if($pid = fork()) { waitpid($pid,0); } else { $g = 10; print "2nd call: $g\n"; exit(0); } print "3rd call: $g\n";
It yields (as expected and desired):
1st call: 5 2nd call: 10 3rd call: 5
Cheers,
Rob

In reply to Win32, fork and XS globals by syphilis

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (9)
    As of 2016-09-30 11:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Extraterrestrials haven't visited the Earth yet because:







      Results (563 votes). Check out past polls.