BRK/sbrk maintains a location. BRK/sbrk changes this position. BRK changes absolute position sbrk changes relative position yesterday's supplement: always remember: There are several basic types of C. All new types are redefined using typedef. Benefits of Type redefinition: 1. convenient maintenance 2. ease of transplantation (the same name is used in each system and does not need to be modified) 3. easy to understand I. ing virtual memory no additional maintenance data memory allocation MMAP/munmap1. function description:
void * MMAP ( void * Start, /// specifies the ing virtual address. If it is 0, the system specifies the start position size_t length, /// specify the size of the ing space. Multiple of pagesize int Prot, /// ing permission: prot_none prot_read prot_write prot_exec int flags, // ing method int FD, /// file description symbol offset_t off /// Start position of the ing in the file (it must be a multiple of 0 or pagesezi) );
The ing method flags: Memory ing: Also called anonymous ing. The last two parameters are invalid file ing: only file ing is mapped to a file. The last two parameters are valid for map_anonymous: Memory ing map_sharedmap_private: either. File ing 2. case:
# Include <unistd. h> # Include <Sys/Mman. h> # Include <Stdlib. h> # Include <Stdio. h> Int Main (){ Int * P = MMAP (null, getpagesize (), prot_read | Prot_write, map_anonymous | Map_shared, 0 , 0 ); * P = 20 ; * (P + 1 ) = 30 ; * (P + 2 ) = 40 ; Printf ( " % D \ n " , P [ 2 ]);// Print 40 Munmap (p, 4096 );} 3. Summary: What kind of memory management method is selected? STL New Malloc small and large data Large data blocks of the same type as BRK and sbrk dynamically move the pointer MMAP/munmap Control Memory Access/use file ing/Control Memory Sharing II. programming tools and dynamic libraries 1. gcc2. make3. gdb4. other tools 5. dynamic library (shared library) 1. gcc-O output file name-O-O1-O2-O3 // compilation Optimization-g-g1-g2-g3 // generate debugging information-wallerror //-wall displays all warnings-werror treats warnings as error prompts-W // close all warning-C // compile only without connection, generate. o file (target file)-E // pre-compiled-S // assembly. Generate the. s file (Assembly file). The compilation process is-e-c-S. automatically call the connector LD-D // define the macro in the command line (the macro can be Code -X // specifies the compiled language type C, C ++ ,. S (assembly), none (automatic determination)-STD = c89-std = c99 III. Static library compilation 1. compilation process (*. a) A is the abbreviation of achieve 1.1 compiled into the target file-static optional gcc-C-static code file. c1.2 archive to static library ar tool (commonly used-r-t option) Ar-r static file name Archived File Name ar-r add. aadd. onm tool (view function symbol table) nm static library or dynamic library or target file or execution file 1.3 use static library GCC code file static library small example: use static library to complete the following Program Input a diamond radius, print the diamond input integer, encapsulate it into iotool, and then print it into a graphic plan: 1. Implement input 2. Implement diamond 3. Compile static library 4. Call static library
//Iotool. c# Include <stdio. h>IntInputint (Const Char*Info ){IntR;//Return ValuePrintf ("% S:", Info); scanf ("% D",&R );ReturnR ;}
// Graphic. c # Include <stdio. h> Void Diamond ( Int R ){ Int X, Y; For (Y = 0 ; Y <= 2 * R; y ++ ){ For (X = 0 ; X <= 2 * R; X ++ ){ If (Y = x + R | Y = x-r | Y =-x + R | Y =-x + 3 * R) {printf ( " * " );} Else {Printf ( " " ) ;}} Printf ( " \ N " );}}
Compile: Gcc-C-static iotool. c
Gcc-C-Static Graphic. Car-r demo1.a iotool. O graphic. oar-T demo1.anm demo1.a
//Main. cMain (){IntR = inputint ("Enter the diamond radius:"); Diamond (r );}
Compile: GCC main. c demo1.a-O main
Execute:./main to compile and summarize the static library as part of the Code: 1. What is the library? Compiled binary archive files encapsulated by functions and other Code 2. ar archiving tool 3. advantages of using libraries to manage code: easy to organize code reuse to protect code copyrights 4. the meaning of "static" in the static library: The compiled program runs without relying on the library as a part of the program to compile and connect 5. the essence of the static library is the set of target files (archive) 6. -Static: Optional. library specifications and conventions library naming rules: Lib library name. a. main version number. minor version number. generally, the "lib library name" is used for the batch number. a. Ar-r libdemo2.a iotool. O graphic. O Library usage rules-l library name-l library directory GCC main. c-o main-l demo2-L. iv. Dynamic library compilation 1. what is a dynamic library (shared library) can be executed, static library cannot be executed, but dynamic library does not have main, cannot be executed independently, dynamic library will not be connected to a part of the Program for execution, dynamic library files are required. tool LDD view the dynamic library that the program needs to call LDD can only view executable files (shared library files or ELF files) readelf-H (-h indicates viewing the execution file header) nm (view function symbols in the library) 3. dynamic library compilation 3.1 compiling-C-f pic (optional) (-F specifies file format PIC location irrelevant code) 3.2 connection-shared Compilation: gcc-C-FPIC iotool. CGCC-C-FPIC graphic. C (non-standard) GCC-shared-odemo3.so iotool. O G Raphic. O (standard) GCC-shared-olibdemo4.so iotool. O graphic. o4. use dynamic library GCC code file name dynamic library file name GCC code file name-l library name-l path of dynamic library GCC main. c-ldemo4-L. -O main standard naming rule: Lib library name. solib library name. a: How does one load the dynamic library of the 4.1 execution program? 4.2 Why do dynamic libraries and directories need to be established when the dynamic library is not part of the execution program? Because the connector needs to confirm the position of the function in the dynamic library, the loading of the dynamic library: 1. find the dynamic library 2. load the dynamic library to the memory (system implementation) 3. ing to the user's memory space (system implementation) dynamic library search rules:/lib/user/libld_library_path find the path specified by the environment variable to set the current path to the environment variable: Export LD_LIBRARY_PATH =. :~ :..:~ Walle buffer mechanism: the system loads the files in Lib:/user/lib: LD_LIBRARY_PATH to the cache/sbin/ldconfig-V to refresh the path of the Search Library in the cache so. Example: enter two numbers to calculate the sum of the two numbers. Requirement: Enter and compute two numbers and encapsulate them into dynamic library calls. 5. Use libdl. so library dynamic library loading principle function search in the dynamic library has been encapsulated into cry libdl. solibdl. so contains four functions: dlopen // open a dynamic library dlsym // find a function dlclose in the opened dynamic library // close the dynamic library dlerror // return an error