Seventh. Data Management 7.1 Memory management This is a memory management for Linux, the code is downloaded in the memory management code. In all computer systems, memory is a scarce resource. Linux provides a concise view of the application that reflects a large, directly addressable memory space, and Linux also provides a memory protection mechanism that avoids interference between different applications. If the machine is properly configured and has enough swap space, Linux also allows the application to access a larger memory space than the actual physical memory.
7.1.1 Simple memory allocations use the malloc call in the standard C-language library to allocate memory:
#include <stdlib.h>
void *malloc (size_t size);
Writing a program memory1.c
This program requires the malloc function to return a pointer to the 1MB memory space. First check to make sure that the malloc function is called successfully, and then use some of the memory to indicate that the allocated memory does already exist.
Because
the malloc function returns a void* pointer, so it needs to be converted to the desired char* type pointer by type conversion.。 The malloc function guarantees that the memory it returns is address-aligned, so it can be converted to any type of pointer.
7.1.2 allocates a large number of memory-writing programs memory2.c
The memory allocated by the application is managed by the Linux kernel. Each time the program requests memory or attempts to read and write to its allocated memory, the Linux kernel takes over the disease and decides what to do with the request.
Initially, the kernel simply satisfies the application's memory request by using free physical memory, but when the physical memory runs out, it starts using the so-called swap space. In a Linux system, swap space is a separate area of the disk that is allocated when the system is installed. If you are familiar with the Windows operating system, the Linux swap space is a bit like a hidden Windows interchange file. But unlike windows, there is no local heap, global heap, or discarded memory segment for Linux swap space, and so the--linux kernel will do all the work for you.
The kernel moves data and program code between physical and swap space, so that every time the memory is read and written, the data looks as if it were already in physical memory, regardless of where they were before you accessed them.
Linux implements a "page-on-demand virtual memory system" where all the memory the user program sees is virtual and does not really exist on the physical address used by the program. Linux says that all of the memory is divided in pages, typically 4096 bytes per page size. Whenever a programmer attempts to access memory, virtual memory is transformed into physical memory, and the specific implementation and time spent on the transformation depend on the specific hardware situation used. When there is no physical presence in the access, a page fault is generated that gives control to the kernel.
The Linux kernel checks the memory address of the access, and if the address is legally available to the program, the kernel determines which physical memory page to provide to the program. Then, if the page has never been written before, the kernel allocates it directly, and if it is already stored on the hard disk's swap space, the kernel reads the page containing the data into the physical memory (it may be necessary to remove an existing page from memory). Then, after the virtual memory address is mapped to the physical address, the kernel allows the user program to continue running. Linux applications do not need to worry about this process, as all the specific internships are hidden in the kernel.
Eventually, when the application exhausts all physical memory and swap space, or when the maximum stack length is exceeded, the kernel rejects subsequent memory requests and may terminate the program's operation.
7.1.3 Abusing memory authoring memory4.c
The memory4.c allocates some memory first, then attempts to write some data after the allocated memory, running the result: Segment error (spit core), see paragraph error (spit Core) detailed. The Linux memory management system protects other parts of the system from this memory misuse. To ensure that a poorly behaved program cannot break any other programs, Linux terminates its operation.
7.1.4 NULL pointer Linux provides strong protection for reading and writing null pointers to addresses.
Write a MEMORY5A.C program to demonstrate what happens when a null pointer is accessed
7.1.5 free memory The Linux memory management system guarantees that the memory allocated to it is returned to the system at the end of the program. However, most programs require not just allocating some memory, using a short period of time, and then exiting. A more common usage is to use memory dynamically, as needed.
A program that uses memory dynamically should always use the free call to release unused memory to the malloc memory manager. By doing so, you can merge the scattered blocks of memory together and manage it by the malloc library, not by the application. If a running program uses and frees memory for itself, these free memory is actually still in the state assigned to the process. Behind the scenes, Linux manages the memory blocks used by programmers as a set of physical pages, typically 4K bytes per page in memory. However, if a memory page is not in use, the Linux memory manager can displace it from physical memory into swap space (paging), thereby mitigating its impact on resource usage. If the program apprentice accesses data that is located in a memory page that has been swapped into swap space, Linxu briefly pauses the program, displaces the memory page from the swap space to the physical memory, and then allows the program to continue running as if the data persisted in memory.
#include <stdlib.h
void free (void *ptr_to_memory);
The pointer argument used when calling free must be to point to the memory allocated by the malloc, calloc, or ReAlloc call.
Writing a program memory6.c
This program calls free to release the memory, with a pointer parameter that points to the previously allocated memory.
Once the call to free frees up a piece of memory, it is no longer part of the process. It will be managed by the MALLOC function library. After you call free on a piece of memory, you must never read or write to it again.
7.1.6 Other memory allocation functions the other two memory allocation functions are not as frequent as malloc and free, and the calloc and ReAlloc prototypes are:
#include <stdlib.h>
void *calloc (size_t number_of_elements, size_t element_size);
void *realloc (void *existing_memory, size_t new_size);
The Calloc function allocates memory for an array of structures, so the number of elements and the size of each element are required as their arguments. The memory it allocates will all be initialized to 0. If the calloc call succeeds, a pointer to the first element in the array is returned. Similar to the malloc call, subsequent CALLOC calls cannot guarantee that a contiguous memory space can be returned, so it is not possible to repeat the call Calloc to expect the memory returned to be contiguous with the previous memory.
The ReAlloc function is used to change the length of the allocated memory block. It needs to pass a pointer to the memory that was previously allocated through the CALLOC call, and then increase or decrease its length based on the value of the new_size parameter. To accomplish this task, the ReAlloc function may have to move the data. Therefore, to make sure that once the memory is reassigned, you must use the new pointer instead of using the pointer that precedes the REALLOC call to access the memory.
If ReAlloc cannot resize the memory block, it returns a null pointer, which means you must avoid using code similar to the following:
My_ptr = malloc (block_size);
...
My_ptr = ReAlloc (my_ptr, block_size * 10);
If the realloc call fails, it will return a null pointer, MY_PTR will point to null, and previously allocated memory with malloc will no longer be accessible through my_ptr. Therefore, before releasing the old block of memory, the best way is to use malloc to request a new piece of memory, and then copy the data from the old memory block to the new block of memory through the memcpy call. This allows the application to continue to access the data stored in the original block of memory, even if an error occurs, enabling a clean program to terminate.
Linux Programming-Data Management (seventh)