فهرست منبع

- random number are seeded in all the processes (both srand() and srandom())

Andrei Pelinescu-Onciul 19 سال پیش
والد
کامیت
279377197c
3فایلهای تغییر یافته به همراه15 افزوده شده و 1 حذف شده
  1. 1 1
      Makefile.defs
  2. 1 0
      main.c
  3. 13 0
      pt.c

+ 1 - 1
Makefile.defs

@@ -67,7 +67,7 @@ MAIN_NAME=ser
 VERSION = 0
 PATCHLEVEL = 10
 SUBLEVEL =   99
-EXTRAVERSION = -dev61
+EXTRAVERSION = -dev62
 
 SER_VER = $(shell expr $(VERSION) \* 1000000 + $(PATCHLEVEL) \* 1000 + \
 			$(SUBLEVEL) )

+ 1 - 0
main.c

@@ -1271,6 +1271,7 @@ try_again:
 	seed+=getpid()+time(0);
 	DBG("seeding PRNG with %u\n", seed);
 	srand(seed);
+	srandom(rand()+time(0));
 	DBG("test random number %u\n", rand());
 
 	/*register builtin  modules*/

+ 13 - 0
pt.c

@@ -43,6 +43,7 @@
 #include "sr_module.h"
 
 #include <stdio.h>
+#include <time.h> /* time(), used to initialize random numbers */
 
 #define FORK_DONT_WAIT  /* child doesn't wait for parent before starting 
 						   => faster startup, but the child should not assume
@@ -144,6 +145,8 @@ int fork_process(int child_id, char *desc, int make_sock)
 {
 	int pid, child_process_no;
 	int ret;
+	unsigned int new_seed1;
+	unsigned int new_seed2;
 #ifdef USE_TCP
 	int sockfd[2];
 #endif
@@ -181,6 +184,8 @@ int fork_process(int child_id, char *desc, int make_sock)
 	
 	
 	child_process_no = *process_count;
+	new_seed1=rand();
+	new_seed2=random();
 	pid = fork();
 	if (pid<0) {
 		lock_release(process_lock);
@@ -190,6 +195,8 @@ int fork_process(int child_id, char *desc, int make_sock)
 		/* child */
 		is_main=0; /* a forked process cannot be the "main" one */
 		process_no=child_process_no;
+		srand(new_seed1);
+		srandom(new_seed2+time(0));
 #ifdef PROFILING
 		monstartup((u_long) &_start, (u_long) &etext);
 #endif
@@ -265,6 +272,8 @@ int fork_tcp_process(int child_id, char *desc, int r, int *reader_fd_1)
 	int sockfd[2];
 	int reader_fd[2]; /* for comm. with the tcp children read  */
 	int ret;
+	unsigned int new_seed1;
+	unsigned int new_seed2;
 	
 	/* init */
 	sockfd[0]=sockfd[1]=-1;
@@ -308,6 +317,8 @@ int fork_tcp_process(int child_id, char *desc, int r, int *reader_fd_1)
 	
 	
 	child_process_no = *process_count;
+	new_seed1=rand();
+	new_seed2=random();
 	pid = fork();
 	if (pid<0) {
 		lock_release(process_lock);
@@ -317,6 +328,8 @@ int fork_tcp_process(int child_id, char *desc, int r, int *reader_fd_1)
 	if (pid==0){
 		is_main=0; /* a forked process cannot be the "main" one */
 		process_no=child_process_no;
+		srand(new_seed1);
+		srandom(new_seed2+time(0));
 #ifdef PROFILING
 		monstartup((u_long) &_start, (u_long) &etext);
 #endif