]dll-the principle of sharing dynamic link libraries among multiple processes

Source: Internet
Author: User

The same problem is when I interview the director, the director asked me a question, these days have been busy, have not read a book, summed up this question today? Why does a process use a dynamic link library and another process can continue using it? At that time, the answer is very general, only half of the answer, and now here to summarize it!
As we have said before, there are several ways to communicate between processes, but the dynamic link library We are talking about is also one of the ways of communication between processes.
The knowledge of all operating systems is the same regardless of the Windows or Linux operating system.
The dynamic-link library is the foundation of the Windows operating system, where the Windows API is basically provided in the form of a dynamic-link library, which is usually not able to run directly or receive messages directly, they are separate files (the suffix is typically. dll, Of course there are some other suffixes that are also available, including functions that can be performed by executable programs or other DLL calls, which means that the dynamic link library is composed of functions. And only when other modules invoke the functions in the dynamic-link library, the dynamic Link Coucay works, in the actual programming, a function can usually be completed in a dynamic link library, and then provided to other function calls. When the process of accessing the dynamic link library is loaded, the system allocates a 4GB private address space for the process (if 32-bit), then the system parses the executable module and finds the DLL that will be called in the executable module. Then the system is responsible for searching these DLLs to find these DLLs and then load these DLLs into memory, and assign them virtual memory space, finally map the DLL's page to the calling process's address space summary, the DLL's virtual inside the code page and data page, They are mapped to process A's code page and data page, and if process B is started at this point, and process B may want to access the DLL, simply map the DLL's code pages and data pages in virtual memory to the address space of the second process. This also shows that in memory, only one copy of the DLL's code and data is needed.

Multiple processes share the same code for a DLL , and it is clear that this saves memory space.

But under Windows (which is the same in Linux), because each process is assigned a 4GB private address space,

and the code and data in the DLL are only mapped to this private address space, so these applications can still not affect each other,

This means that multiple applications can share the same code in the same DLL ,

But the data that the DLL holds for each process is not the same,

And each process allocates its own address space for all the data used by the DLL ,

For the simplest example, my DLL has a function of int Add (int num1, int num2),

The function is to implement the addition of Num1 and num2 and return the result after the addition.

Then I have a process A that uses this DLL , and it calls the function Add (TEN),

And then I have a process B which also uses this DLL , and it calls the function Add (() ,

So the data in process a is actually stored in the private address space of process a ,

The data in process b is stored in the private address space of process B ,

The above simple example shows that if you simply use the dynamic link library in a way that does not enable communication between processes.

If you want to use the dynamic link library to achieve interprocess communication, then there is a solution to try,

That is, the memory allocated from the system for the dynamic link library (the system needs to load the dynamic link insecure library into memory),

Because in memory, the dynamic link library actually only has one copy,

It is shared by all modules that need to invoke the functions in the dynamic link library, or simply as executable programs.

Since it is shared, if I save the data in this memory allocated by the system to the dynamic link library,

Wouldn't that be possible to get or set by all the executable programs that access the dynamic link library.

In this case, I can use process A to set up the data in this shared memory,

Then process B can read the data in this shared memory, which is not the process of communicating between processes,

In this case, the idea is exactly the same as using the Clipboard.

It also uses a two-process shared memory to serve as a mediator for data storage.

Http://blog.chinaunix.net/uid-26983585-id-3364514.html

]dll-the principle of sharing dynamic link libraries among multiple processes

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.