[San-diego-pm] another bug in perl: rand/OSX?

Michael Casinghino mike at casinghino.com
Thu Jun 3 01:16:17 CDT 2004


This indeed looks like a perl bug. The code runs fine on my linux box,
and I was able to reproduce the problem on OSX 10.3. Adding an srand()
call before rand() call in the child code seems to fix it.

I ran it under ktrace (which is like a systrace for osx) like this:

	ktrace -idt wcusin /usr/bin/perl trand.pl
	kdump -f ktrace.out | less

In my version with the srand call in it, you can definitely see srand
working (it starts like OPEN("/dev/urandom")). In the version without
it, it's either not being called or it functions differently. The
ktrace of a child (without the explicit srand in it) looks like this:

(I added the getpid call to verify that the process id was different)

  1473 perl     CALL  getpid
  1473 perl     RET   getpid 1473/0x5c1
  1473 perl     CALL  fstat(0x1,0xbffff540)
  1473 perl     RET   fstat 0
  1473 perl     CALL  ioctl(0x1,FIODTYPE,0xbffff590)
  1473 perl     RET   ioctl 0
  1473 perl     CALL  write(0x1,0x6000,0x1b)
  1473 perl     GIO   fd 1 wrote 27 bytes
       "child to sleep 3 second(s)
       "
  1473 perl     RET   write 27/0x1b
  1473 perl     CALL  write(0x1,0x6000,0x1)
  1473 perl     GIO   fd 1 wrote 1 byte
       "
       "
  1473 perl     RET   write 1
  1473 perl     CALL  write(0x1,0x6000,0x15)
  1473 perl     GIO   fd 1 wrote 21 bytes
       "mama, I'm so gone!!!
       "
  1473 perl     RET   write 21/0x15
  1473 perl     CALL  exit(0)

So, somehow, in forked processes on osx, the "PL_srand_called"
variable check is failing. (I think that fstat/ioctl business is doing
the $|++ on the dup'd fd.)

If you have the XTools from Apple installed, you could download the
latest version (5.8.4), compile it locally, and run with that to see
if it's been fixed. I was going to do this, but the XTools download
was too *SLOW*.

This is an interesting problem.

-Mike


On Wed, Jun 02, 2004 at 02:15:22PM -0700, C. Abney wrote:
> Maybe my keywords suck and this is a known issue?
> 
> Running this script on a Mac OSX gives very unexpected results:
> 
> =8<============
> #! /usr/bin/perl -w
> # spawn some kids and check up on them periodically without actually
> # waiting to harvest them...
> 
> my ( $pid, @jobs, $n );
> 
> $|++;
> 
> for (0..2)
> {
>         $pid = fork();
>         last if $pid == 0;
>         push @jobs, $pid;
> }
> 
> if ( @jobs && $pid != 0 ) {
>         # parental responsibilities
>         $SIG{CHLD} = 'IGNORE';
>         $n = kill 0, @jobs;
> 
>         print "jobs: @jobs\n";
> 
>         while ( 0 < $n ) {
>                 print "we have $n children, Houston\n";
>                 sleep 2;
>                 $n = kill 0, @jobs;
>         }
>         exit 0;
> } else {
>         my $slp = int rand 20;
> 
> 
>         print "child to sleep $slp second(s)\n";
>         sleep int rand 20;
> 
>         print "\nmama, I'm so gone!!!\n";
>         exit 0;
> =8<============
> 
> It's rand()s behavior on the Mac I'm concerned about.
> 
> from uname:
> Darwin odonata.ucsd.edu 7.3.0 Darwin Kernel Version 7.3.0: Fri Mar  5
> 14:22:55 PST 2004; root:xnu/xnu-517.3.15.obj~4/RELEASE_PPC  Power
> Macintosh powerpc
> 
> perl --version:
> This is perl, v5.8.1-RC3 built for darwin-thread-multi-2level
> (with 1 registered patch, see perl -V for more detail)
> 
> To be explicit:  each child gets the same value returned from rand.
> 
> Yours,
> 
> Charles
> -- 
> Charles Abney
> Polymorphism Research Laboratory, 0603
> UCSD School of Medicine
> 9500 Gilman Dr.
> La Jolla, CA 92093-0603



> _______________________________________________
> San-diego-pm mailing list
> San-diego-pm at pm.org
> http://www.pm.org/mailman/listinfo/san-diego-pm




More information about the San-Diego-pm mailing list