Skip to contentSkip to conversation

tools: gen_eth_addr: add getpid() to time(0) to avoid duplicated seed

PATCH New Josh Wu 4 participants 4 messages Sep 16, 2015 – Sep 28, 2015

lore
Share & tools
lore

Josh Wu

author
#0 lore raw

PATCH v2 1/1 tools: gen_eth_addr: add getpid() to time(0) to avoid duplicated seed

As 'time(0) | getpid()' will have a lot of duplicated value. It is not a expected behavior. We expect different value for the seed when when run it in many times.

So this patch will left shift the getpid() and add to time(0). That avoid duplicated value.

Test command is like:
% RUN=0; while [ $RUN -lt 10000 ]; do tools/gen_eth_addr; RUN=$(($RUN+1)); done | sort | uniq | wc -l 10000

This patch is incorporated with suggestions made by Wolfgang Denk and Andreas Bie?mann. Thanks them a lot.

---

Changes in v2:
- left shift and add the getpid() value instead of removing it.

 tools/gen_eth_addr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/gen_eth_addr.c +1 -1
index bf9d935..5fa3e0c 100644
@@ -15,7 +15,7 @@ main(int argc, char *argv[])
 {
     unsigned long ethaddr_low, ethaddr_high;
 
-    srand(time(0) | getpid());
+    srand(time(0) + (getpid() << 8));
 
     /*
      * setting the 2nd LSB in the most significant byte of
Signature · 1 line
1.9.1
#1 lore raw

PATCH v2 1/1 tools: gen_eth_addr: add getpid() to time(0) to avoid duplicated seed

Quoted 2 lines · Josh Wu

As 'time(0) | getpid()' will have a lot of duplicated value. It is not a expected behavior. We expect different value for the seed when when run

when when ... could be fixed when applied

Quoted 10 lines

it in many times.

So this patch will left shift the getpid() and add to time(0). That avoid duplicated value.

Test command is like:
% RUN=0; while [ $RUN -lt 10000 ]; do tools/gen_eth_addr; RUN=$(($RUN+1)); done | sort | uniq | wc -l 10000

Quoted 4 lines

This patch is incorporated with suggestions made by Wolfgang Denk and Andreas Bie?mann. Thanks them a lot.

Quoted 22 lines

---

Changes in v2:
- left shift and add the getpid() value instead of removing it.

 tools/gen_eth_addr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/gen_eth_addr.c +1 -1
index bf9d935..5fa3e0c 100644
@@ -15,7 +15,7 @@ main(int argc, char *argv[])
 {
     unsigned long ethaddr_low, ethaddr_high;
 
-    srand(time(0) | getpid());
+    srand(time(0) + (getpid() << 8));
 
     /*
      * setting the 2nd LSB in the most significant byte of
#2 lore raw

PATCH v2 1/1 tools: gen_eth_addr: add getpid() to time(0) to avoid duplicated seed

In reply to Josh Wu

Dear Josh Wu,

Quoted 15 lines · In message <[email protected]> you wrote:

As 'time(0) | getpid()' will have a lot of duplicated value. It is not a expected behavior. We expect different value for the seed when when run it in many times.

So this patch will left shift the getpid() and add to time(0). That avoid duplicated value.

Test command is like:
% RUN=0; while [ $RUN -lt 10000 ]; do tools/gen_eth_addr; RUN=$(($RUN+1)); done | sort | uniq | wc -l 10000

This patch is incorporated with suggestions made by Wolfgang Denk and Andreas Bie?mann. Thanks them a lot.

Quoted 8 lines

---

Changes in v2:
- left shift and add the getpid() value instead of removing it.

 tools/gen_eth_addr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

I ran this version through a total of 80,000 runs ; there were no duplicated addresses found in this test.

Thanks!

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
"Oh dear, I think you'll find reality's on the blink again."
- Marvin The Paranoid Android
#3 lore raw

PATCH v2 1/1 tools: gen_eth_addr: add getpid() to time(0) to avoid duplicated seed

In reply to Josh Wu
Quoted 12 lines · Josh Wu

As 'time(0) | getpid()' will have a lot of duplicated value. It is not a expected behavior. We expect different value for the seed when when run it in many times.

So this patch will left shift the getpid() and add to time(0). That avoid duplicated value.

Test command is like:
% RUN=0; while [ $RUN -lt 10000 ]; do tools/gen_eth_addr; RUN=$(($RUN+1)); done | sort | uniq | wc -l 10000

Quoted 7 lines

This patch is incorporated with suggestions made by Wolfgang Denk and Andreas Bie?mann. Thanks them a lot.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20150928/71531e23/attachment.sig>