written 6.9 years ago by | modified 2.9 years ago by |
Subject: Operating System
Topic: MEMORY MANAGEMENT
Difficulty: Hard
written 6.9 years ago by | modified 2.9 years ago by |
Subject: Operating System
Topic: MEMORY MANAGEMENT
Difficulty: Hard
written 6.7 years ago by |
By examining the nature of the execution of programs, the following requirements are to be satisfied.
Address mapping and relocation
After a process is created and loaded into main memory, it is ready to run. For the operating system to dispatch it, the following information must be known for access, as Figure below: the locations of process control information, the execution stack, and code entry. However these are not enough.
Within a program, there may be also memory references in various instructions, e.g. jmp somewhere, where somewhere is a reference to a memory location which contains the next instruction to be executed, or move AX, [somewhere_else], where somewhere_else is the memory location in which the value is supposed to be moved into AX.
Before the program is loaded, these references are usually the relative addresses to the entry point of the program, which is 0. These addresses are also called logical addresses, which make up a logical address space.
After the loading of the program to a location in the main memory, some facility must be available to support transforming the logical addresses into physical addresses, which are the real addresses of main memory pointing to destination data or instructions and makes up the physical address space.
Thus somehow the processor and the operating system must be able to translate logical addresses into physical ones, reflecting the current location of the program, as Figure 46.The translation may be performed in different ways at different moments, as follows:
Compiler time:
If it is known in advance that a program will reside at a specific location of main memory, then the compiler may be told to build the object code with absolute addresses right away. For example, the boot sect in a bootable disk may be compiled with the starting point of code set to 007C:0000.
Load time:
It is pretty rare that we know the location a program will be assigned ahead of its execution. In most cases, the compiler must generate relocatable code with logical addresses. Thus the address translation may be performed on the code during load time. Figure below shows that a program is loaded at location x. If the whole program resides on a monolithic block, then every memory reference may be translated to be physical by added to x.
Execution time:
The load-time translation can solve the problem in some sense, however in some cases, a process has to be moved from one location to another during execution.
For example, a process may be swapped out to virtual memory area in the disk so that another process may be loaded.
When it is swapped back into the main memory, it is painful to require that it resides at the previous location, since the location may still be occupied by another process.
Thus the process needs to be reloadable. In this case, the translation is done dynamically during the execution time: Easy memory reference remains to be logical, and when it is to be involved in the execution, the translation facility works on it and generate the physical address.
Note that the generated address does not replace the original logical address one. Most general-purpose operating systems use this method.
Protection and sharing
Memory references are valid if pointing to locations that belong to the current process itself, however usually prohibited if referring to locations in other processes, whether accidental or intentional.
Note that the memory protection requirement needs help from the processor rather than the operating system, since when a process occupies the processor, the operating system can hardly control it, say checking the validity of memory references.
Any protection mechanism must allow several processes to access the same portion of main memory.
For example, multiple processes may use a same system library and it is natural to load one copy of the library in main memory and let it shared by those processes.
Application organization
As we know, main memory is organized as a linear, or one-dimensional, address space, while most programs are organized into modules.
Whether regarding sharing or protection, it is natural for the users to express control in terms of modules, thus some supporting mechanism is needed. The segmentation method we will cover later on responds to this requirement.
Two-level Memory organization
We have discussed memory hierarchy which includes both fast expensive memories and slow but cheap storage devices.
Typically two levels of storage are present, main memory and disks. The former is fast, but usually cannot provide enough space for concurrent processes.
In this case, hard disks are used for the operating system to swap out inactive processes, which may later be swapped back into the main memory.