In my comprehension, "dll/so" can be shared between programs(processes). for example when "libprint.so" is loaded in "main"(called main1) at first time, "libprint.so" is loaded from disk into memory, if we start another "main"(called main2), "libprint.so" will not loaded from disk but mapped from memory because "libprint.so" has been already loaded in memory once.
So i design an experiment:
main.cc --> main
#include <iostream>
#include <chrono>
#include <thread>
void printinfo();
int main() {
printinfo();
while(true) {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
return 0;
}
print1.cc --> libprint1.so
#include <iostream>
void printinfo() {
std::cout << "Print One" << std::endl;
}
print2.cc --> libprint2.so
#include <iostream>
void printinfo() {
std::cout << "Print Two" << std::endl;
}
mv libprint1.so libprint.so
./main
// output is: Print One
keep the main.exe running, and replace the libprint.dll with libprint2.dll, like
mv libprint2.so libprint.so
./main
// output is: Print Two
why the output is "Print Two"? I expect it to be "Print One" the "libprint.so" is already loaded in memory, although i changed the content of "libprint.so", but the so's absolute path is the same as before, how does the operating system know the "new libprint.so" is different with before?
Thanks for @Michael Chourdakis, in windows environment, the libprint.dll could not be replaced when main.exe is running.
But the problem is still there in linux(libprint.so could be replaced in linux), @user253751 says there must be some tricks that linux figure different "so", i want to know exactly what the tricks are, do i have to read the linux os source code ?