Avinash
Avinash

Reputation: 1

how to load shared library by one application and unload by another application in linux

I am using linux system and having a shared library.

I want to load and run the shared library when first application wants to use the library. and want to unolad and stop the library when last application got closed.

suppose first application load the library and now second and third application also started using the library. But now application one is closing , so application one should not unload the library it should keepon running until last application is not closing.

how i can achieve this scenario ?? I am trying to search but not getting the answer??

Thanks in advance.

Upvotes: 0

Views: 339

Answers (1)

Employed Russian
Employed Russian

Reputation: 213636

I am trying to search but not getting the answer

This is most likely because your question is (appears to be) based on a lot of assumptions (about what it means to "be running" and "be loaded") that may have been true in MS-DOS era, but are almost certainly not true on any recent operating system.

When the first application loads a shared library (let's call it libfoo.so), libfoo.sos code and data is mapped into memory of the process that is running the first application. The code is usually mapped read-only, and the data with read-write permissions.

When the second application (process) starts, and also uses libfoo.so, the same mapping is performed, and the same physical memory that was used in the first mapping now also appears in the second process's virtual address space (but possibly at a different address). That sharing of physical memory between processes is why the libraries are called shared to begin with.

It's slightly more complicated for data, which is shared with copy-on-write (COW) semantics.

When the last process using libfoo.so exits, the physical pages occupied by libfoo.so code and data can finally be freed up, and the library is unloaded.

So, one answer to your "application one should not unload the library it should keep on running until last application is not closing" question is that this will happen automatically.

Another answer is that "the library it should keep on running" makes no sense, because only the process can be running; a library can not.

Upvotes: 2

Related Questions