When multiple ProgramWhen using the same shared library, we all know that multiple programs have multiple processes, but the shared library has only one copy in the memory. So the question is: If I define a global variable in the Shared Library, is there only one global variable? If so, when multiple processes are using this shared library, the problem arises. For example, we define a global variable server_inited in the shared library to indicate whether a socket server has been started successfully. If this variable is true, the next time you call the function to start the server, check this variable and find that it is already true, so the server will not be started. In this way, after a process calls a function that starts the server once, other processes call this function again. Obviously, this is not acceptable.
As mentioned above, global variables cannot be used when writing shared libraries. This will be very troublesome.
For this reason, I wrote two programs for testing. One testlib is used to generate a shared library, and the other is used to test.
The conclusion is: No. Shared LibraryCodeSegment, that is, each function, may have only one copy in the memory, but global variables, including static variables, and Global static variables have independent copies in each process. So it will not cause the problems mentioned above.