I have an issue with rand() while fork()ing and using IO::Socket::SSL.

In all forked processes rand() gives me the same set of random numbers. If I call rand() several times I get random numbers as expected, but in all forked processes I get the same random set.

If I remove IO::Socket::SSL the set is random in all processes.

I've reduced the case down to this:

use strict; use IO::Socket::SSL; foreach (1 .. 5) { my $pid = fork(); next if $pid; random(); random(); exit; } sub random { print sprintf("Process %d: %d", $$, int(rand(100000))), "\n"; }

If I run the script I get this output:

$ perl fork_rand.pl Process 13788: 32768 Process 13788: 3778 Process 13789: 32768 Process 13789: 3778 Process 13790: 32768 Process 13790: 3778 Process 13791: 32768 Process 13791: 3778 Process 13792: 32768 Process 13792: 3778

I would expect the numbers to be random in all processes -- not return the same set of numbers.

If I remove IO::Socket::SSL everything works as expected:

$ perl fork_rand.pl Process 13806: 94050 Process 13806: 14790 Process 13807: 89099 Process 13807: 93200 Process 13808: 48447 Process 13808: 14001 Process 13809: 28378 Process 13809: 71603 Process 13810: 44245 Process 13810: 86800

Is this a known bug?

I've tested on both Linux and FreeBSD, with latest version of IO::Socket::SSL and Perl 5.8.8.


In reply to fork(), IO::Socket::SSL and rand() by digmon

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.