From: Alexei A. Frounze on
On Apr 17, 8:57 am, peter <cmk...(a)gmail.com> wrote:
> On 4月17日, 下午3時08分, "Alexei A. Frounze" <alexfrun...(a)gmail.com> wrote:
>
>
>
> > On Apr 17, 12:01 am, "Alexei A. Frounze" <alexfrun...(a)gmail.com>
> > wrote:
>
> > > On Apr 16, 11:25 pm, peter <cmk...(a)gmail.com> wrote:
>
> > > > Hi
> > > >   Shared library is used to save disk space, not memory space. If you
> > > > load two different programs and these two programs are linked with the
> > > > same shared library. That shared library exist in memory twice. Right?
>
> > > Under certain conditions, there will be just one copy of it
> > > everywhere.
> > > Typical conditions for that:
> > > - the library's code either appears at the same virtual address in
> > > both programs in memory or it's position-independent
> > > - the library contains no modifiable data, only constants; all
> > > modifiable stuff belongs to programs, their stacks and thread-local-
> > > storage (if any)
> > > - just one version of the library exists :)
>
> > > Alex
>
> > Wait, what I've just described was essentially DLLs...
>
> > Theoretically, you can still save memory even with linked libraries,
> > but for them to look like DLLs you'd need to still satisfy all of the
> > above and the library image should be identical inside of the apps,
> > that is, the linker shouldn't shuffle the library's code when linking.
> > I'd say it should be a loose linking, more or less like appending the
> > library to the rest of the program image and doing nothing else with
> > it.
>
> > Also, some relaxations w.r.t. modifiable data can be made if you use
> > copy-on-write.
>
> > Alex
>
> what is Linux do when it loads two programs with the same shared
> library?
> I can't find any document that mention this thing.

I don't know for sure. I'm using Linux about once a year and haven't
learned much about it yet.
Alex
From: Robert Redelmeier on
peter <cmk128(a)gmail.com> wrote in part:
> Shared library is used to save disk space, not memory space. If you
> load two different programs and these two programs are linked with the
> same shared library. That shared library exist in memory twice. Right?

Fopr MS-DOS, this is correct. For more modern OSes (OS/2,
*BSD, Linux, MS-WindowsNT), it is not. Not only are libraries
almost always shared (only one copy (of used parts) in RAM),
but so are multiple copies of programs. This is part of the
OS demand paged Copy-on-Write memory management strategy.
There is considerable CPU hardware support for this paging.

As a simple illustration, check your free RAM (with `free` under
*BSD & Linux) TaskMan.exe(?) under MS-WindowsNT+) on a "clean"
system. Then open your favorite bloatware like a browser (Firefox
or MS-InternetExplorer for the foolhardy). Check free RAM again.
Maximize bloat by load some video (like youtube.com), check again.

Now, open a second copy, check again, second video in that copy
check again. You will notice free RAM decreases considerably
on first browser & flasplayer load, but much less on second
(and higher). They also open quicker. Because lots is shared.


-- Robert






From: peter on
On 4月18日, 上午5時01分, Robert Redelmeier <red...(a)ev1.net.invalid> wrote:
> peter <cmk...(a)gmail.com> wrote in part:
>
> > Shared library is used to save disk space, not memory space. If you
> > load two different programs and these two programs are linked with the
> > same shared library. That shared library exist in memory twice. Right?
>
> Fopr MS-DOS, this is correct.  For more modern OSes (OS/2,
> *BSD, Linux, MS-WindowsNT), it is not.  Not only are libraries
> almost always shared (only one copy (of used parts) in RAM),
> but so are multiple copies of programs.  This is part of the
> OS demand paged Copy-on-Write memory management strategy.
> There is considerable CPU hardware support for this paging.
>
> As a simple illustration, check your free RAM (with `free` under
> *BSD & Linux)  TaskMan.exe(?) under MS-WindowsNT+) on a "clean"
> system.  Then open your favorite bloatware like a browser (Firefox
> or MS-InternetExplorer for the foolhardy).  Check free RAM again.
> Maximize bloat by load some video (like youtube.com), check again.
>
> Now, open a second copy, check again, second video in that copy
> check again.  You will notice free RAM decreases considerably
> on first browser & flasplayer load, but much less on second
> (and higher).  They also open quicker.  Because lots is shared.
>
> -- Robert

Thanks Robert, completely understand now.
Just want to clarify one thing : Shared library with Position-
indepedent-Code can be share in RAM. But non-PIC shared library
cannot. The only one advantage of non-PIC shared library is to save
disk space. Right?
thanks
From: H. Peter Anvin on
On 04/17/2010 08:57 AM, peter wrote:
>
> what is Linux do when it loads two programs with the same shared
> library?
> I can't find any document that mention this thing.

Linux shares all contents except modified data pages.

-hpa
From: Robert Redelmeier on
peter <cmk128(a)gmail.com> wrote in part:
> Thanks Robert, completely understand now.
> Just want to clarify one thing : Shared library with
> Position- indepedent-Code can be share in RAM. But non-PIC
> shared library cannot. The only one advantage of non-PIC
> shared library is to save disk space. Right? thanks


That may be too restrictive. While AFAIK libraries are
normally written with PIC, they do not need to be: Each process
has its own memory map set by the OS and invisible to the app.
So long as the OS can load or remap the posn' dependant code
at the required addr, everything is fine.

What is more difficult is self-modifying code (SMC). This
cannot be shared, but will trigger a CoW fault and a copy
will be made. Fortunately, SMC is rarely justified.


-- Robert