digmon has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: fork(), IO::Socket::SSL and rand()
by Fletch (Bishop) on Nov 19, 2008 at 15:14 UTC | |
|
Re: fork(), IO::Socket::SSL and rand()
by ccn (Vicar) on Nov 19, 2008 at 15:21 UTC | |
|
Re: fork(), IO::Socket::SSL and rand()
by Anonymous Monk on Nov 19, 2008 at 15:16 UTC | |
|
Re: fork(), IO::Socket::SSL and rand()
by digmon (Initiate) on Nov 19, 2008 at 15:46 UTC | |
by ikegami (Patriarch) on Nov 19, 2008 at 16:23 UTC | |
by sgt (Deacon) on Nov 19, 2008 at 20:54 UTC |