Feasibility Analysis of sharing global variables between Linux shared libraries (dynamic libraries) and processes

Source: Internet
Author: User

In the masterProgramThe global variables in the dynamic link library are correct and undeniable, but this requires the dynamic library to be added when the program compiles the link, and use extern to declare the variable in the program to be used, which does not actually achieve the essential purpose of the dynamic library (to call it again when used); this is actually not a big problem, if you only use it in one process, this article mainly discusses the use of dynamic library global variables in multiple processes.

If you want to share global variables among processes by using dynamic libraries, it is unlikely that different processes will copy the global variables when accessing the dynamic library, that is, if another process tries to modify the global variable in the dynamic library, the system will copy the global variable to the process separately, this modification operation does not affect the values of variables in other processes.

Test procedure: (http://www.liuzhigong.com/download/test_linux_so.tar.gz)

Test results:

As you can see, the main process first modifies this value, and then reads the modified value from the sub-process, however, modifications in the child process do not affect the global variables in the dynamic library used in the master process. So It is not feasible to try to share global variables between processes using dynamic libraries.. Appendix: Compilation Method

Gcc-FPIC-shared testshare. C-o libtestshare. so.2.0
Gcc-O main-L/main. c libtestshare. so.2.0
./Main

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.