0

I have a global variable, X. I then fork and modify X from the child. I want those changes to show up in the parent, but I don't want the parent to have to wait on the child.

How can I do this?

Casey Patton
  • 4,021
  • 9
  • 41
  • 54
  • 3
    You're probably better off threading here (and if you knew me, you'd know how much it hurt to say that), since you want multiple execution contexts to be able to modify data in a single memory instance. – tbert Feb 04 '12 at 19:46

3 Answers3

5

You need to put the variable in shared memory. There are many ways to create shared memory. I'd probably just use mmap, but you could also check out shmget or shm_open.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
1

When you fork a new process that is a separate copy of the address space. It can only see the changes made before the fork.

If you want shared memory for communication between the processes, you have to create that explicitly.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Sorry, my question must be unclear. I realize that they each get a separate copy and I'm trying to get around that and share a variable. As in, how do I share memory between the processes? – Casey Patton Feb 04 '12 at 19:44
1

You cannot.

After forking, those are two separate processes. You will have to make use of some IPC.

Krizz
  • 11,362
  • 1
  • 30
  • 43