Is there only one static variable in the shared library in Linux?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.