While I was studying about shared library I read a statement
Although the code of a shared library is shared among multiple processes, its variables are not. Each process that uses the library has its own copies of the global and static variables that are defined within the library.
I just have few doubts.
I am just a starter so please help me understand.
Thanks!
In contrast, when archive libraries are linked into an executable program, they have a fixed location in the process's private virtual address space. Shared libraries reside in the /usr/shlib directory. Archive libraries reside in the /usr/lib directory.
Unless specified otherwise, a shared library will share memory with the process hosting it. Each process instance will then have its own copy. However, on Windows it is possible to create shared variables which allow inter-process communication. You do so by putting them in the right kind of segment.
A shared library or shared object is a file that is intended to be shared by multiple programs. Symbols used by a program are loaded from shared libraries into memory at load time or runtime.
Simply put, A shared library/ Dynamic Library is a library that is loaded dynamically at runtime for each application that requires it. Dynamic Linking doesn't require the code to be copied, it is done by just placing name of the library in the binary file.
Shared libraries are loaded into a process by memory-mapping the file into some portion of the process's address-space. When multiple processes load the same library, the OS simply lets them share the same physical RAM.
Portions of the library that can be modified, such as static globals, are generally loaded in copy-on-write mode, so that when a write is attempted, a page fault occurs, the kernel responds by copying the affected page to another physical page of RAM (for that process only), the mapping redirected to the new page, and then finally the write operation completes.
To answer your specific points:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With