post #1 of 1
Thread Starter 
I'm writing a program using a parent and a child. The parent will create the child and they both generate random numbers. Each time a random number is created it is printed on the screen with the who processed the number (parent or child) and the process ID). Each process sleeps after printing the random number. The child sleep value is one and the parents is 2.

I'm pretty familiar with C++ but not with parent/child processes and using fork() as well as trying to integrate it with UNIX so this is all very confusing to me. Here's where I am so far:
Code:
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h> 

using namespace std;
int main (int argc, char *argv[] ){

int rnum, onum, x;
pid_t pid;

pid = fork();
srand(time(NULL));


if (pid == 0)
        {
                //child process
                rnum = rand()%100+1;
                cout << "Child: PID:" << getpid(); << " Number:" << rnum << endl;
        }
        
        
        else
        {
                //parent process
                rnum = rand()%100+1;
                cout << "Child: PID:" << getpid(); << " Number:" << rnum << endl;
        }
        }

Any help, pointers, or suggestions are GREATLY appreciated because I'm lost.

Thanks
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply