[info-performer] Question about fork() processes

Date view Thread view Subject view Author view

From: Alan Gifford (ag-devel++at++houston.rr.com)
Date: 07/14/2004 14:11:35


I took this example of using fork() from the complex.C example program
found in /usr/share/Performer/src/pguide/libpf/C++.

I have a simple gravity simulator, where a Gravity object interacts with
some PhysObject objects. The PhysObjects represent physical objects,
and if I want the Gravity object to calculate effects on those objects,
I add them by doing Gravity::addObject(PhysObject*). This is all taken
care of earlier in the program. The problem starts when this code executes:

  if (forkedGrav) {
    pid_t grav_pid = 0;
    double gravTime, gravLast;
    gravLast = pfGetTime();
    if ((grav_pid = fork()) < 0)
      pfNotify(PFNFY_FATAL, PFNFY_SYSERR, "Fork of gravity process
failed.");
    else if (grav_pid)
      pfNotify(PFNFY_NOTICE,PFNFY_PRINT,"Gravity running in forked
process %d",
           grav_pid);
    else if (!grav_pid) {
      while(1) {
        gravTime = pfGetTime();
        grav.setObjects((gravTime - gravLast) * timeScale);
        gravLast = gravTime;
      }
    }
  }

  while (!Shared->exitFlag) {
    pfSync();
    pfFrame();
    t = pfGetTime();

    if (!forkedGrav) {
      grav.setObjects((t - lastTime) * timeScale);
    }

    for (int i=0; i < objCount; i++) {
      object[i].getPosition(position[i]);
      dcs[i]->setTrans(position[i]->vec[0],
               position[i]->vec[1], position[i]->vec[2]);
    }
    lastTime = t;
  }

The variable "grav" is my Gravity object, and the array "object" is my
array of PhysObjects.

When forkedGrav == 0, the program runs fine, but when I set forkedGrav
to 1, I don't get any movement from my objects.

Since the PhysObjects on which the Gravity object operates are all
declared, initialized, and their pointers already sent to the Gravity
object, it seems that the forked process should be aware of them, and
should be operating on those variables.

My understanding was that anything already declared in the process which
forks will be accessible to the forked process. After seeing these
results though, I am inclined to believe that while the data may be
accessible, it is only accessing a copy of those variables, and not the
same memory addresses of the parent process.

Is this right?

Thanks,
Alan


Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Thu Jul 15 2004 - 01:12:09 PDT