c - Address space for shared libraries loaded multiple times in the same process -


first off, i've found a few references might answer question. while plan on reading them (i.e. after work), i'm still asking here in case answer trivial , not require supplementary knowledge.

here situation: writing shared library (let's call liba.so) needs maintain coherent internal (as in, static variables declared in .c file) state within same process. library used program p (i.e. p compiled -la). if understand far, address space p this:

 ______________ | program p    | | <            | |  variables,  | |  functions   | |  p      | | >            | |              | | <            | |  liba:       | |  variables,  | |  functions   | |  loaded (ie  | |  *copied*)   | |  shared | |  object      | | >            | | <            | |  stuff  | |  other       | |  libraries   | | >            | |______________| 

now p call dlopen("libq.so", ...). libq.so uses liba.so (i.e. compiled -la). since happens within same process, need liba somehow hold same state whether calls come p or q.

what not know how translate in memory. this:

 ______________ | program p    | | <            | |  p stuff     | | >            | |              | | <            | |  liba stuff, | |  loaded p | | >            | => a's code , variables duplicated |              | | <            | |  libq stuff  | |  <           | |   liba stuff,| |   loaded q| |  >           | | >            | |______________| 

... or this?

 ______________ | program p    | | <            | |  p stuff     | |  *liba       | |  *libq       | | >            | |              | | <            | |  liba stuff, | |  loaded p | | >            | => a's code loaded once, q holds sort of pointer |              | | <            | |  libq stuff  | |  *liba       | | >            | |______________| 

in second case, keeping consistent state single process trivial; in first case, require more effort (e.g. shared memory segment, using process id second argument ftok()).

of course, since have limited knowledge on how linking , loading works, diagrams above may wrong. know, shared libraries @ fixed space in memory, , every process accesses same data , code. behaviour depends on how and/or p and/or q compiled. , behaviour not platform independent.

the code segment of shared library exists in memory in single instance per system. yet, can mapped different virtual addresses different processes, different processes see same function @ different addresses (that's why code goes shared library must compiled pic).

data segment of shared library created in 1 copy each process, , initialized whatever initial values specified in library.

this means callers of library not need know if shared or not: callers in 1 process see same copy of functions , same copy of external variables defined in library.

different processes execute same code, have individual copies of data, 1 copy per process.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -