Re: memory question

From: Doug White <dwhite_at_gumbysoft.com>
Date: Thu, 19 Aug 2004 19:25:48 -0700 (PDT)
On Thu, 19 Aug 2004, Gustavo A. Baratto wrote:

> This is a bit off-topic, but I haven't found this information in the freebsd
> website nor google... and this seems to be the most programmer oriented
> mailinglist
>
> If I have 2 or more different programs using the same shared library... The
> first program loads the .so at startup and keeps running. My question is
> that when the second program starts, does it have to load the .so again, or
> the memory used by the shared library will be shared between all the two
> programs?

I'm probably off base here, and expect to get clubbed by a developer any
second.

Technically yes, but from a fairly basic level.  ld.so, the dynamic
linker, mmap()s the library to "load" it.  As pages in the library are
read, they are faulted into memory.  When another process mmap()s the same
file, the Vm system notices the pages for the file are already in core and
doesn't fault them back in again, assuming the programs access the same
pages.  For the pruposes of resource utilization, though, both processes
are charged for the size of the library.

Note that ld.so maps in the pages read-only(*) so there is little danger
from one process corrupting another by scribbling on the library pages.
Files mmap()ed read-write are a whole different bag.

(*) There's probably exceptions to this for stuff like static variables.
Changing the file on disk will cause the changes to propagate to all the
processes holding mmap()s over the file. This is usually fatal for shared
libraries.

> I know that different processes started by the same parent share the .so,
> but I wanna know if completely different programs started by different
> parents can share the same shared object.
>
>  For example, if perl/DBI started from the shell, and php/mysql started from
> apache can share the same libmysqlclient.so in memory.

It would be rather lame if libraries were exclusive access. :)  There may
be cases where they do get their own copies.  You shouldn't rely on this
behavior for any sort of memory-use calculations...

I suspect the next question is going to be about sharing data between
these processes :)

-- 
Doug White                    |  FreeBSD: The Power to Serve
dwhite_at_gumbysoft.com          |  www.FreeBSD.org
Received on Fri Aug 20 2004 - 00:25:48 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:07 UTC