delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2011/01/11/05:50:29

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-1.6 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,RFC_ABUSE_POST,T_TO_NO_BRKTS_FREEMAIL
X-Spam-Check-By: sourceware.org
MIME-Version: 1.0
Date: Tue, 11 Jan 2011 11:50:14 +0100
Message-ID: <AANLkTim2Wz20AdnhMJ9kgjW7x+hsm0PMLY6dJj+sBQzs@mail.gmail.com>
Subject: drand48() (and erand48) returns only zeros and pthread application problems - in cygwin 1.7.8 returns always same values
From: jdzstz - gmail dot com <jdzstz AT gmail DOT com>
To: cygwin AT cygwin DOT com
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

About all the problems with drand48, explained in:
   * http://www.cygwin.com/ml/cygwin/2010-12/msg00460.html

Christopher Faylor fixed it in 20101229 snapshot:
http://www.cygwin.com/snapshots/winsup-changelog-20101226-20101229

I have realized that the fix does not work as expected, it is used
thread memory address to initialize srand48 with srand48 ((long int)
&x);

--- winsup-src-20101226/cygwin-snapshot-20101226-1/winsup/cygwin/cygtls.cc	2010-02-28
15:55:33.000000000 +0000
+++ winsup-src-20101229/cygwin-snapshot-20101229-1/winsup/cygwin/cygtls.cc	2010-12-29
06:34:24.000000000 +0000
@@ -94,4 +95,5 @@ _cygtls::init_thread (void *x, DWORD (*f
       local_clib._current_locale = "C";
       locals.process_logmask = LOG_UPTO (LOG_DEBUG);
+      srand48 ((long int) &x);
     }

Now, drand48 doesnot returns zero in thread, but the problem is that
in every execution the adress should be the same, because it returns
always the same values.
The test code can be found in:
http://www.cygwin.com/ml/cygwin/2010-12/msg00459.html

$ uname -a
CYGWIN_NT-5.1 ES-H0TJT2J 1.7.8s(0.234/5/3) 20101229 01:34:45 i686 Cygwin

TEST 1  => seed=2 ; threads=2
$ ./drand48threadtest 2 2
Init srand48 with: 2
Start main
Main: Creating thread #0 - drand48: 0.912433
        Executing thread #0.0 - drand48: 0.985887
Main: Creating thread #1 - drand48: 0.159083
        Executing thread #0.1 - drand48: 0.212671
        Executing thread #1.0 - drand48: 0.789110
End main
        Executing thread #0.2 - drand48: 0.460958
        Executing thread #1.1 - drand48: 0.513941
        Executing thread #1.2 - drand48: 0.049337

TEST 2 => seed=3; threads=2
$ ./drand48threadtest 3 2
Init srand48 with: 3
Start main
Main: Creating thread #0 - drand48: 0.783235
        Executing thread #0.0 - drand48: 0.985887
Main: Creating thread #1 - drand48: 0.863673
        Executing thread #0.1 - drand48: 0.212671
        Executing thread #1.0 - drand48: 0.789110
End main
        Executing thread #0.2 - drand48: 0.460958
        Executing thread #1.1 - drand48: 0.513941
        Executing thread #1.2 - drand48: 0.049337

TEST 3=> seed=2 threads=1
$ ./drand48threadtest 2 1
Init srand48 with: 2
Start main
Main: Creating thread #0 - drand48: 0.912433
        Executing thread #0.0 - drand48: 0.985887
        Executing thread #0.1 - drand48: 0.212671
End main
        Executing thread #0.2 - drand48: 0.460958

Main thread generated numbers depends on seed, that is specified in
first parameter, but in other threads it generates same numbers in
diferent executions because thread seed is calculated from memory
address:
        Executing thread #0.0 - drand48: 0.985887
        Executing thread #0.1 - drand48: 0.212671
        Executing thread #1.0 - drand48: 0.789110
        Executing thread #1.1 - drand48: 0.513941

The problem root was on newlib implementation, but I think we can make
a easy workaround changing srand48 line:

-      srand48 ((long int) &x);
+     srand48(random());

Is it possible to call random() in that point?

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019