Dynamic Link and dynamic link library

Source: Internet
Author: User

Dynamic Link is relative to static link. In program design, in order to achieve code and module reuse, the program designer often makes common function functions into a library. When the program needs to implement a function, it directly calls the function in the library file, this achieves code reuse. In early programming, reusable function modules were stored in static library files in the form of compiled binary code, and files suffixed with Lib in the MS operating system. During programming, if a user program calls a function in the static library file, the compiler automatically copies the binary code of the function from the static library file to the user's target program during program compilation, compile with the target program into an executable file. This indeed achieves code reuse in the coding phase, which reduces the burden on the program designer, but does not achieve reuse during the execution period. For example, if a program named a.exe uses the F () mechanism in the static library, when a.exe runs on multiple instances, there are actually multiple copies of F () in the memory, resulting in a waste of memory.

With the advancement of technology, there has been a new connection method, that is, dynamic links, which fundamentally solves the problems caused by static links. Dynamic Links are handled in a similar way as static links. reusable code is also stored in a separate library file (in the MS operating system, files are suffixed with DLL, there is also a dynamic link library in Linux, called the shared object so file ), the difference is that the compiler does not copy the function execution body in the library file to the executable file when compiling a program that calls the dynamic link library, instead, only one function call Tag is retained in the executable file. When the program is running, the operating system loads the Dynamic Linked Library files into the memory and maps them to the address space of the program, this ensures that the program can normally call the functions in the library file. At the same time, the operating system ensures that when the program runs on multiple instances, only one dynamic link library is copied to the memory, that is, the dynamic link library is shared at runtime. The use of dynamic links brings a few benefits: first, the dynamic link library and user programs can be written separately. The separation here can be used to separate time and space, it can also be the separation of development languages, which reduces the Coupling Degree of the program. Secondly, because of the unique compilation and running Modes of Dynamic Links, the size of the target program itself is smaller than that of static links, at the same time, the runtime is a shared dynamic link library, which saves disk storage space and running memory space. The last one is to increase Program Flexibility and implement functions such as plug-in mechanism. Anyone who has used Winamp knows that many of its functions are provided in the form of plug-ins. These plug-ins are some dynamic link libraries, and the main program has specified the call interface in advance, any plug-in written according to the specified call interface can be called by Winamp. Windows 95, 98, NT series and other systems all provide the dynamic link library function, and most of the system calls of these operating systems are implemented through the dynamic link library, kenel32.dll, user32.dll, gdi32.dll, and other dynamic link library files in the most common NT operating systems contain a large number of system calls. In the Windows family, the operating system of the NT kernel is more secure than the previous 95 and 98 systems in the dynamic link library mechanism. 95. When a program calls a dynamic link library, the 98 system loads the dynamic link library to a virtual address space called process shared space between 2-3G, in addition, the page tables of all processes in the virtual address space of 1 GB are the same. That is to say, for all processes, the page tables in this shared area all point to the same group of physical pages, the dynamic link library loaded into the memory is visible to all running processes. If a dynamic link library is changed by one of the processes, or its own crashes, it will affect all processes that call it. If the dynamic link library is a dynamic link library of the system, the system will crash. In Windows NT, the dynamic link library is mapped to the user address space of the process, and the copy on write mechanism is used to ensure the sharing security of the dynamic link library, copy on write can be interpreted as copy at write time. Generally, multiple running processes share the same dynamic link library in the original mode until a process needs to write data to a page of the dynamic link library, the system copies the page, sets the attributes of the new copy page to readable and writable, and modifies the page table of the process to point to the new physical page. In this way, no matter how the process modifies the data on this page, it will not affect other processes that call this dynamic link library.

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.