Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Unit_3.docx
Скачиваний:
1
Добавлен:
01.05.2025
Размер:
84.93 Кб
Скачать

3.3 Resource Sharing

In this module, we are going to look at the operating system as a mechanism for resource sharing. We will see that many aspects of operating system function can be explained in those terms. In the course of explaining how sharing works, this module covers most of the components of the computer system. The computer system not only shares many of its internal resources, such as the processor, but also its external resources, such as the hard disk drive.

3.3.1 Virtual Memory

  • Managing Memory

  • Relocation

  • Virtual Memory

Managing Memory

Managing the system's memory is an important job of the kernel. Some of the computer's main memory (DRAM) is reserved for the operating system, but most of it is available for user programs. Let us say a user is running a Web browser, an editor, and a computer game. Each of these programs needs a certain amount of memory, but none of them needs access to all the memory. The kernel allocates some memory to each program and keeps track of what program is using what.

Modern operating systems such as Linux and Windows provide virtual memory, to increase program flexibility. (We will say more about exactly what virtual memory is, later.) To understand why virtual memory is useful, let us start by looking at how older operating systems like MS-DOS worked. In those systems, all programs ran in the same real address space, since there was no virtual address space.

Executable programs, written in binary machine code, contain instructions and data. Both instructions and data contain memory addresses. So when you write a program in machine code (or a compiler translates your high-level language into machine code for you), you (or the compiler) must calculate the address for every instruction and every piece of data. Let us say you start at zero and lay out all your instructions and data in sequence, so your entire program occupies memory addresses 0 through 8,462. Now, suppose everyone else writes their programs the same way, starting at address 0. Obviously, you cannot run two programs that occupy the same memory addresses at the same time with this scheme—since as soon as you load the second one, it will overwrite the first.

Relocation

A solution that was invented before virtual memory was to write programs in a special way. Any memory location that contained an address was specially marked in the binary file. When the user tried to run the program, the operating system would allocate memory for it somewhere and load the program into that memory area. As it did so, it would "fix up" all the specially marked address references so that they pointed to the correct locations. For example, let us say the original program contained a table of information starting at memory location 700. Another part of the program—say, at location 210—contained the address of the table (i.e., the value 700) and was specially marked as containing an address. The operating system loads this program into memory, beginning at location 30,000. As it does so, it changes the value in location 30,210 from 700 to 30,700, and so on. This process is called relocation.

This scheme allows the computer to load multiple programs in memory at once, in whatever portion of memory is available at the time. Its advantages are that it is simple to implement and does not require any changes to the hardware. But, it also has some serious shortcomings. First of all, because of the way relocation works, the memory allocated to the program must be contiguous. Suppose the user is running a half a dozen small programs at the same time. After some of these programs have exited, programs 1, 3, and 6 are left running. Now the user wants to run a big application, but unfortunately, there may now be nowhere to put it. If the application cannot squeeze into the space that was used by program 2 or by programs 4 and 5 together, then it cannot fit into memory, even though the total number of noncontiguous free blocks of memory may be more than adequate.

Another drawback of this approach is that the size of a running program is limited to the amount of physical memory installed on the machine, minus whatever the operating system has reserved for itself. But, large programs do not normally use all their memory at once. A program with a large address space may only need to access a few thousand instructions and a few thousand bytes of data at a time. It would be more efficient to allocate only a little bit of RAM at a time to such a program and to keep the rest of its address space somewhere else, such as on disk. This is what virtual memory allows us to do.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]