The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Child Killing Parent mark007 UNIX for Advanced & Expert Users 4 03-09-2009 01:57 PM
Parent child Relation !! using awk/sed ??? varungupta UNIX for Advanced & Expert Users 0 01-29-2008 02:24 PM
Parent/Child Processes yoi2hot4ya Shell Programming and Scripting 2 05-31-2006 01:27 PM
kill parent and child larry UNIX for Dummies Questions & Answers 4 01-12-2003 12:18 AM
How hard can it be? ps child/parent velde046 Filesystems, Disks and Memory 2 05-25-2002 04:36 PM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 09-27-2009
xyzt xyzt is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 21
Random numbers in parent/child?

Hi

I'm trying to generate random numbers both in parent process and the child process. But I get the same random number in both processes. Why doesn't it generate different numbers altough I seed random number generator?
Here's my code:

Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

void seedit(void)
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    srand(tv.tv_sec * tv.tv_usec);
}

int main()
{
        int pid;
        seedit();
        pid = fork();
        if (pid==0)
        {
                int r = rand()%10 + 1;
                printf("[CHILD] sleeping %d sec\n", r);
                sleep(r);
        }
        else if (pid>0)
        {
                int r = rand()%10 + 1;
                printf("[PARENT] sleeping %d sec\n", r);
                sleep(r);
        }
        else
        {
        }
}
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0