Creating Child Processes in C++

clamum

Lifer
Feb 13, 2003
26,256
406
126
I've created a child process (using C++) that computes a given number of places in the Fibonacci sequence. For instance, if the user enters 5, it will put the first 5 numbers of the Fibonacci sequence in an array and the parent process will then print these values out when the child process has finished its computation.

The problem is, when the parent process goes to print out the sequence, it prints out random large integers. I put couts in the child process to make sure the correct values are being inserted into the array, and they are.

The parent process has a wait() command (my book says the command is wait(NULL) but that gives me a compiler error) so it should be waiting for the child process to finish to avoid any synchronization issues.

Also, the array is contained in a simple struct which is advised by the book.

My code is below, thanks for any help. It looks like a lot of code but it's real simple.

EDIT: I figured out the problem... some of the code I used is based off the book; the lines that say
// pointer to the shared memory segment
char *shared_memory;

are not needed, and instead sMem should be a pointer to a shared_data and the shmat function should return a casted shared_data pointer and store it in sMem.

Dumb mistake. :)
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Wow... forking a process for recursive non-memoized fib is going to be REEEEEEEEEEEEEEEEEEEEEEEEEEALLY slow.