Discussion on operating system memory allocation call

zhaozj2021-02-16  57

In note: This is a reply I posted on the school BBS. It is discussed here. The original text is: sender: True (more cruel to yourself), the letter area: c_and_cpp Title: NEW Space System Will it be automatically recycled? Sending station: Harbin Institute of Technology (Fri May 14 12:07:43 2004) Are all operating systems? Where is the theoretical basis? My first reply is: sender: IAMXIAOHAN, the letter area: c_and_cpp Title: Re: The space system from NEW will be automatically recycled? Sending station: BBS Harbin Institute of Technology (Fri May 14 12:16:51 2004) This is mainly how the operating system works. For the current general operating system, the process is forced to recover all allocated resources in destruction. So you can use Delete, but use Delete to recycle resources in time, this is a good programming habit [in True (for yourself more cruel) masterpiece:]: When the program is running, I think I think The system should be automatically recovered, is it? : Are all operating systems? Where is the theoretical basis? The following is Sun's reply sender: Sun (big light bulb), the letter area: c_and_cpp Title: Re: The space system will be automatically recycled? Sending station: Harbin Institute of Technology (Fri May 14 13:08:12 2004), transfer [in the masterpiece of Iamxiaohan ":]: This is mainly how the operating system works,: For the current general operating system, the process is forced to recover all allocated resources backwards in the destruction. Does this by reference? :): So you can use Delete, but use Delete to recycle resources in time, this is a good: ................. The following post is right. Sun's reply, you are welcome to discuss or correct.

Sender: IAMXIAOHAN, the letter area: c_and_cpp Title: Re: The space system coming out is automatically recycled? Sending station: BBS Harbin Institute of Technology (Sat May 15 09:25:05 2004) Seeing N this book saying that for modern operating systems, it is not delete, in fact, new operations will be compiled by the compiler. Link to Malloc, is called Malloc, and Malloc is a system call to allocate memory. Therefore, the distribution of memory is ultimately completed by the operating system instead of the compiler, the compiler is only a call system call to allocate actions, and the truly assigned is determined by the operating system. General operating system has a piece Very large free memory area, this free memory area is also known as a heap, and when you assign this memory with new, it is also called allocation on the heap, when you use New to generate an operating system call, request allocation of memory At the time, the operating system checks this pile, retrieves a size of a size and marked as "unallocated", and returns its pointer, then mark this memory as "assigns", when you call When DELETE, the operating system clears the "Assignable" tag of this memory so that this memory can be allocated again. This is the simplest memory allocation action, a metaphor, the operating system is like a bank, New is like borrowing money to the bank, DELETE is like your money, the bank cancels the loan. Q, why can you operate this memory after Delete instead of the paragraph error? As can be seen from the above description, the memory after DELETE is still in the memory space, but it is only re-being marked as "assigns", so you can also operate it, but you don't know it. When you are allocated, if it is allocated, your operation of this block is very dangerous. Of course, a robust operating system can completely when you visit the memory that is not your memory (or access to memory that is "assignable", generate an error interrupt or error trap, which mainly looks at how the operating system is managed. . A very robust operating system may detect all the access to the stack memory. If you find that you have access to a memory, it will be wrong. And this test is time-time, therefore, some operating systems can choose to detect only for "allocated" memory, so that the memory is re-contained as "assignable" to be re-allocated. You can continue to operate this memory, and once this memory is redistributed, the memory mark is "allocated, then you will continue to use this memory, the operating system will report an error.

In this way, sometimes you have access to the delete memory, and sometimes it will be wrong, this is because the memory has not been separated in the previous memory, the operating system does not detect whether the interviews of this memory are legal. So there is no error, but when you visit next time, this memory has been assigned to other processes. Therefore, the operating system will detect this memory, so it is wrong. For some operating systems that use page-managed operating systems, The bottom-level memory allocation is assigned a page (4KB), then implement a layer of advanced memory management so that you can assign a specified size memory instead of a whole page, when all spaces on a page are all delete However, when all marked as "assignable", the operating system will be placed in "not existed" and then remove it from the memory, which is removed in the page table, so that when re-accessed, The CPU will generate a loop interrupt, and the operating system will handle such an interrupt. Processing may be reloaded, perhaps an output of an error, because the operating system found that you have delete, no need to access this page. To access, you need to re-NEW, so a very interesting phenomenon: you delete a memory, then keep access, the first 99 times can, and 100 times, this may be Because of the first 99 times, because there is no DELETE space because this page, while the other process, other processes have a good delete, so the operating system has a waste of this page. When you visit, the operating system is I will find that you are illegal access, it will be wrong. Therefore, 99 running the correct procedure, not necessarily the 100th operation, it will be correct! The advantage of this is that the operating system only detects whether the memory access is legal when it generates a misinterpage, and because memory access is very annoying, reduce the number of tests, of course, can rise system efficiency. When the operating system is allocated, a data structure is created, and a lot of information that is allocated, such as the first address, size of this memory. Therefore, there will be such an image. When Malloc or New Memory, you need to specify the memory size to be assigned, but when you free or delete, you don't need to specify the size, but just give the first memory. The address pointer is OK, this is mainly because the operating system only needs to reintegrate this memory as "assignable", and its memory size, the operating system automatically records it when the operating system allocates memory. Modern operating system, when allocating memory, which process is recorded to which process is assigned, so that when the process is exiting, it is fully mandatory to recover all assignments to this process. Of course, if an operating system does not record this memory and assign it to anyone, then it can't force a memory, then you need to use delete. Such a situation is mainly on the operating system of the antique stage, because the memory is very limited, the operating system is not allowed to use it to store the information it needs to be stored, but for now, save the memory is assigned to That process is really small case, so now you can not pay attention to whether you use Delete, but use Delete will release memory space in time, but do not use delete, it will only be released during the process, this difference Naturally, you should not say more.

However, I want to remind the operating system forced recovery, it is not equal to the "garbage collection mechanism!", Look at Java, its garbage collection mechanism, it is constantly allocated, as long as it can be assigned, if it is not possible It's time to find which memory is already used, it reclaims it, but the operating system has no such capacity, it does not know which memory has been used, for it, as long as it is Is there a memory without delete, it thinks the program is still using it. To be simple, for two programs for Java or C, if you only use new without delete, Java can recover memory during the program run, but C's programs are not, only after the program runs, The operating system is once a one-time recycling, Java is closed while receiving, and the program can run forever, and C is only used without receiving. When it is not used, the program has to be killed, and the operating system is reclaimed again. So, if you use for for (;;) new ..., the Java program can run, while C is always using the memory. So do you have all New, can you not delete? This is also inconvenient, some operating systems are allocated in memory, in addition to keeping memory allocation, and except for a type parameter, to mark this memory is used alone, or multi-process shared. If it is used alone, of course, it can be automatically recovered by the operating system at the end of the process; if it is a multi-process sharing, this is certain, because this process is over, but there is still a process, so it cannot be recycled. On the distribution of multi-process shared memory, this is generally possible to be implemented by the reference count, each memory has a reference count value, the initial reference count value is 0, New once 1, delete is -1, Only when it is 0, it will be really recycled by the operating system. At this time, if you have, but there is no delete, the reference count cannot be 0, this memory will be retained, but cannot be operated System recycling, in this case, you must be delete after New. The distribution of memory is done by the operating system, not by the compiler, Malloc, New, etc., just a system call, which calls the operating system's memory allocation function to complete the function of memory allocation, and how to assign it is operational The system is determined, the compiler cannot be decided, and it is impossible to know, so this is the platform-related, and the compiler is independent. Each operating system has different strategies of memory allocation, anyway, the CPU provides those functions, how to implement the requirements to meet the demand is the eight fairy pass the sea, each shows the gods, there is no reference information, no standard answer, Linux Meeting its needs, using an implementation, and Windows adopts another implementation in order to meet its needs. Like Pyos, my idea is to provide a more powerful memory management, for example, a process can apply to assign a shared memory area, and only the memory of the application is written, while other processes only have read rights, this is Another memory allocation strategy.

I think we can make a need, then discuss what method can do so, you can discuss why Linux or Windows is implemented like this, not what Linux or Windows is implemented, not what should be implemented, or to discuss the book How to achieve it. Original: http://purec.binghua.com/Article/showArticle.asp? ArticleId = 130

转载请注明原文地址:https://www.9cbs.com/read-18961.html

New Post(0)