From owner-freebsd-current@FreeBSD.ORG Fri Aug 20 02:25:48 2004 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 55D5516A4CE for ; Fri, 20 Aug 2004 02:25:48 +0000 (GMT) Received: from carver.gumbysoft.com (carver.gumbysoft.com [66.220.23.50]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3D9D943D45 for ; Fri, 20 Aug 2004 02:25:48 +0000 (GMT) (envelope-from dwhite@gumbysoft.com) Received: by carver.gumbysoft.com (Postfix, from userid 1000) id 2EDE472DD4; Thu, 19 Aug 2004 19:25:48 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by carver.gumbysoft.com (Postfix) with ESMTP id 2A1BA72DCB; Thu, 19 Aug 2004 19:25:48 -0700 (PDT) Date: Thu, 19 Aug 2004 19:25:48 -0700 (PDT) From: Doug White To: "Gustavo A. Baratto" In-Reply-To: <087b01c48635$d3ebc850$6400a8c0@chivas> Message-ID: <20040819191709.Q66276@carver.gumbysoft.com> References: <087b01c48635$d3ebc850$6400a8c0@chivas> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII cc: freebsd-current@freebsd.org Subject: Re: memory question X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 20 Aug 2004 02:25:48 -0000 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@gumbysoft.com | www.FreeBSD.org