![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
| Display Modes | Rate This Thread |
|
|