Re: [info-performer] Question about fork() processes

Date view Thread view Subject view Author view

From: Paolo Farinelli (paolo++at++sgi.com)
Date: 07/15/2004 14:01:23


Hi Alan,

The "object" array of PhysObjects must be allocated in
shared memory (before forking off your gravity-process).
If the object array is not in shared memory, the gravity
process and your main process will see different 'copies'
of it.

Hope this helps.
Paolo
.

Alan Gifford wrote:

> 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
>
> -----------------------------------------------------------------------
> List Archives, Info, FAQ: http://www.sgi.com/software/performer/
> Open Development Project: http://oss.sgi.com/projects/performer/
> Submissions: info-performer++at++sgi.com
> Admin. requests: info-performer-request++at++sgi.com
> -----------------------------------------------------------------------
> SIGGRAPH 2004 Friends of Performer BOF: Tuesday, August
> 10, 6pm - 8pm Wilshire Grand Hotel, Sierra Ballroom
> -----------------------------------------------------------------------
>
>

-- 
Paolo Farinelli                                           paolo++at++sgi.com
Member of Technical Staff, OpenGL Performer              1-650-933-1808
Silicon Graphics        1600 Amphitheatre Pkwy, Mountain View, CA 94043


Date view Thread view Subject view Author view

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