Reference Variables To A Child Process Created With Fork

Hi!

IN THE FOLLOWING PROGRAM THE VALUE OF j REMAINS UNCHANGED . WHY ? IF I WANT A VARIABLE VALUE TO CHANGE LIKE THIS , IS THERE ANY WAY TO DO IT ?

Or do we have to use shared memory variables?

main()
{
int return_pid, i, total;
int j=1;
total = TOTALRECS+1;

 for \(i=0; i<NUMPROCESSES; i\+\+\)
      if \(fork\(\) == 0\)
          child_code \(i,total,&j\);

 for \(i=0; i<NUMPROCESSES; i\+\+\) 
      return_pid = wait\(0\);
          
 printf \("value of J <%d>\\n", j\);

}

The short answer... yes, you should probably
use shared memory. Once you fork(),
you get a new process with it's own stack and
heap space (among other things).

BTW, I hope your code doesn't really look
like that :eek:

Sounds like a homework problem to me...... Let's not answer homework problems, if we can avoid it. Thanks.