On Wed, 2005-09-07 at 12:12 +0200, Nicolas Cannasse wrote:

> Another question about dynamic libraries on Linux :
> Is library code shared among several processes ?

Yes. 

> We have the following scheme :
> - Apache starts and load  mod_neko.ndll and libneko.so
> - It then forks around 200 subprocesses
> - Each process load the neko bytecode which when initialized load several
> other .ndlls (they are .so)
> Does the code section of theses .ndll is shared among the 200 processes or
> not ?

Yes. In general:

Shared libraries are loaded into of pagefile memory, 
which maps to the same real memory pages when paged in. 

Each process might map these pages to different virtual addresses, 
but the code is fully shared. Because of the different 
mapping addresses, the code must be relocatable 
(meaning, it can execute at any address,
in binary form, NOT that it can be relocated to any address
by patching). gcc provides -fPIC switch for this. AFAIK Ocaml
native code compiler does not generate relocatable i386,
but I'm not sure.

The problem comes with writable data, due to C being brain dead.
Static writable data has to be created for *each* process:
separate memory for each one. Each thread then has
a separate stack as well (plus any tls).

The handling of shared *read only* data is linker/OS/client dependent.
Stuff like string literals can typically be put into
a read-only section, but it is up to the compiler/linker,
and may depend on switches: K&R C allowed writing into
string literals: ISO C does not, but tools like gcc allow
you to choose.

You should also note: even non-relocatable binary programs
can use the same memory. All modern memory managers provide
a fork() which duplicates the process, but uses copy
on write pages. This means read only pages get shared
on such systems anyhow: forking typically creates a process
with the same addresses, so even absolute addressing works.

Thus, if Apache spawns 200 processes, they're all going
to be using shared readonly memory anyhow, whether or not
shared libraries are used. Shared libraries allow
*different* processes to load libraries and share the memory,
for example two different neko applications will share the
run time code. For example, two different Web pages served
by these processes may load the same library at different
points, different times, and different logical addresses,
but the physical pages will be the same (for shared libraries).

-- 
John Skaller <skaller at users dot sourceforge dot net>

Attachment: signature.asc
Description: This is a digitally signed message part

---
Neko : One VM to run them all

Reply via email to