Globar Variables in Shared Libraries

This is a discussion on Globar Variables in Shared Libraries within the C++ Programming forums, part of the General Programming Boards category; when a shared library exports some global variables: are those variables shared among all processes or does each process get ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    Question Globar Variables in Shared Libraries

    when a shared library exports some global variables: are those variables shared among all processes or does each process get its own copy of the variables?

    so the question is: if process 1 writes to a global variable of a shared library, and process 2 reads from that variable - does process 2 read what process 1 wrote? id say no.
    signature under construction

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    A global variable is a global variable.
    Meaning anything that can access it, can change it. So if one thing changes it, it is also changed in the other.
    Woop?

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    >> id say no.
    You'd be correct. Each process/dll gets its own copy of the global.

    gg

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Oh processes I really got to start reading the whole post
    Woop?

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    thx for reply

    but is it also possible to share one instance of global vars of some shared libary among all processes?
    i mean do commonly used systems (windows, linux, ...) have support for that?


    edit:
    don't tell me how to find a work around - i dont need it anyway - i am just writing a little script language with some library support - and i just want to know how far i could go
    signature under construction

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Both Linux and Windows support many types of inter-process communication (IPC), one of which is the concept of "shared memory" that allows multiple processes to have acces to the same chunk of memory.

    In other words - yes.

    gg

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    and another thingy:

    library functions who want to access "their" global variables cant use absolute or relative addresses to them - they need to use pointers since the globals could be in different locations in different processes.

    so is something like:
    Code:
    int gv;
    
    int func() {
      return gv;
    }
    actually rewritten as:

    Code:
    struct GLOBALS {
      return gv;
    };
    
    int func(struct GLOBALS *p_g) {
      return p_g->gv;
    }
    so do library functions depend on that the pointer to the globals they need is in some register or on the stack?

    or am i completely wrong?
    signature under construction

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    >> library functions who want to access "their" global variables...
    Access to variables within a library will always be on a per-module basis (process or dll). It doesn't matter if the global is a pointer or a structure or within a structure.

    The only way to have a "PC-global" variable is to use some form of IPC. For example, one easy form of IPC is to simply use files.

    gg

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    uh i think you didnt read really read my question

    i asked wheter all library functions which need globals are actually rewritten by the compiler.

    as all methods (member functions of classes) are "rewritten" so that they have the this pointer passed as first argument.

    MyClass::MyMethod(void);
    basically becomes :
    MyMethod(MyClass *this);

    is the same scheme applied to globals on shared libraries?


    does anyone know a good link for shared libraries under the hood?
    i just couldnt find a site which explained the details.
    signature under construction

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    >> ...methods...have the this pointer passed as first argument....is the same scheme applied to globals on shared libraries?
    No. The compiler doesn't do anything like that. As I said, each module gets its own copy of global data. If it's a static library, then each module get its own copy of the code as well. If its a shared or dynamic library, then there is only one copy of the code, but each module still gets its own copy of global data.

    In the case where there's only one copy of the code (ie. a shared library), then for each module using that library, the OS will typically load a module's copy of the library's global data at the same virtual address space in all modules. Then when the shared library's code is loaded into memory, all references to global data are "fixed up" to use the virtual address in which the data was loaded in each module. This way, when the library code is running within the context of a particular module, references to the libraries global data will auto-magically use that modules copy of that data.

    For static libraries, the linker simply puts a copy of the code and data in each module that links with the library.

    To understand the full end-to-end solution for an operating system, you'll need to understand:
    - Executable file formats (PE for Windows, ELF for Linux, etc...)
    - How your linker works.
    - How the OS loads these files into memory.
    - How the OS supports the concept of shared libraries (DLL's for Windows, Shared Objects for Linux)

    gg

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    ah ok... its loaded into the same virtual address space for each process!
    darn... its that simple...
    thxalot
    signature under construction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. MinGW thread-safe runtime libraries
    By Mario F. in forum C++ Programming
    Replies: 3
    Last Post: 08-21-2006, 08:15 AM
  3. XP Shared Drive Problem(?)
    By Davros in forum Tech Board
    Replies: 2
    Last Post: 10-06-2002, 01:37 PM
  4. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 01:39 PM
  5. Shared memory in Linux: B-TREE of structures
    By zahid in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 10:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21