Michael T. Jones (mtj++at++babar.asd.sgi.com)
Fri, 29 Nov 1996 09:25:51 -0800
Isn't a global var as visible to all processes as a shared var
located in shared arena ?
The confusion is that this question has a subtle compound answer:
Yes, a global variable is as visible to all processes as a shared
variable located in a shared arena. However, the key question is
"at what time is that global variable visible?"
The answer tells you why global variables will not work as you are
thinking they might.
When you call pfConfig(), multiple copies of your address space are
created, one for each configured process (app, cull(s), draw(s),
isect, dbase, ...). The values of globale variables AT THAT INSTANT
are copied to each of these address spaces and so are available to
each process. However, once pfConfig() returns, the situation is
that since each address space is distinct, further changes to these
global variables will only be seen in the process that makes the
change.
Example:
extern int example = 1;
pfconfig();
[look at example in the app process]
printf("%d\n", example); --> 1
[look at example in the cull process]
printf("%d\n", example); --> 1
[now look at example in the draw process]
printf("%d\n", example); --> 1
now, change example in the app process...
example = 2;
[look at example in the app process]
printf("%d\n", example); --> 2
[look at example in the cull process]
printf("%d\n", example); --> 1
[now look at example in the draw process]
printf("%d\n", example); --> 1
This is because once you have forked the multiple processes, there
is a different pyhsical address in memory for each process even though
they all have the same virtual address.
On the other hand, things in the shared arena have the same physical
and virtual address, so what works is:
extern int *example = (int *)pfAllocateSomeSharedMemoryForMe(sizeof(int));
pfConfig();
now, the pointer has been copied by the pfConfig() fork()ing but that's
fine -- since the shared address is the same in all cases. What we will
do in this example is to access the shared memory through the global
pointer, which makes it all work out properly.
[look at example in the app process]
printf("%d\n", *example); --> 1
[look at example in the cull process]
printf("%d\n", *example); --> 1
[now look at example in the draw process]
printf("%d\n", *example); --> 1
now, change example in the app process...
*example = 2;
[look at example in the app process]
printf("%d\n", *example); --> 2
[look at example in the cull process]
printf("%d\n", *example); --> 2
[now look at example in the draw process]
printf("%d\n", *example); --> 2
Does this make sense to you?
I think we wrote a longer description of it in the PFPG, but I'm not sure
where it is.
Michael
Be seeing you, Phone:415.933.1455 Fax:415.965.2658 MS:8U-590
Michael T. Jones Silicon Graphics, SSG--Advanced Graphics Division
mtj++at++sgi.com 2011 N. Shoreline Blvd., Mtn. View, CA 94039-7311
120 Mario 64 Stars OpenGL/ImageVision/OpenInventor/Performer/Cosmo3D
=======================================================================
List Archives, FAQ, FTP: http://www.sgi.com/Technology/Performer/
Submissions: info-performer++at++sgi.com
Admin. requests: info-performer-request++at++sgi.com
This archive was generated by hypermail 2.0b2 on Mon Aug 10 1998 - 17:54:02 PDT