The Abstraction: Address Spaces(地址空间)

一个打印虚拟存储器地址分配的小程序:

  1. main()函数的地址

  2. 存在heap上的地址

  3. 存在stack上的地址

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    printf("location of code : %p\n", (void *) main);
    printf("location of heap : %p\n", (void *) malloc(1));
    int x = 3;
    printf("location of stack : %p\n", (void *) &x);
    return 0;
}

在Mac上的结果:

location of code : 0x100000ea0
location of heap : 0x1003000f0
location of stack : 0x7fff5fbff7ac

From this, you can see that code comes first in the address space, then the heap, and the stack is all the way at the other end of this large virtual space. All of these addresses are virtual, and will be translated by the OS and hardware in order to fetch values from their true physical locations.

关于printf

%[flags][width][.precision][length]specifier

这里写图片描述

This is VM(Virtual Management) intro.

Early Systems

The OS was a set of routines (a library, really) that sat in memory (start- ing at physical address 0 in this example), and there would be one running program (a process) that currently sat in physical memory (starting at physical address 64k in this example) and used the rest of memory. There were few illusions here, and the user didn’t expect much from the OS.

这里写图片描述

Multiprogramming and Time Sharing

After a time, because machines were expensive, people began to share machines more effectively. Thus the era of multiprogramming was born, in which multiple processes were ready to run at a given time, and the OS would switch between them, for example when one decided to perform an I/O.

multiprogramming was born.

Soon enough, however, people began demanding more of machines, and the era of time sharing was born.

Time sharing was born.

The notion of interactivity became important, as many users might be concurrently using a machine, each waiting for (or hoping for) a timely response from their currently-executing tasks.

One way to implement time sharing would be to run one process for a short while, giving it full access to all memory, stop it, save all of its state to some kind of disk (including all of physical memory), load some other process’s state, run it for a while, and thus implement some kind of crude sharing of the machine.

One way to implement time sharing.

Unfortunately, this approach has a big problem: it is way too slow, particularly as memory grows.

While saving and restoring register-level state (the PC, general-purpose registers, etc.) is relatively fast, saving the entire contents of memory to disk is brutally non-performant.

The problem of the approach.

Thus, what we’d rather do is leave processes in memory while switching between them, allowing the OS to implement time sharing efficiently.

这里写图片描述

In the diagram, there are three processes (A, B, and C) and each of them have a small part of the 512KB physical memory carved out for them. Assuming a single CPU, the OS chooses to run one of the processes (say A), while the others (B and C) sit in the ready queue waiting to run.

As time sharing became more popular, you can probably guess that new demands were placed on the operating system. In particular, allowing multiple programs to reside concurrently in memory makes protection an important issue; you don’t want a process to be able to read, or worse, write some other process’s memory.

The new demands: protection for concurrent processes.

The Address Space

the running program’s view of memory in the system.

The address space of a process contains all of the memory state of the running program. For example, the code of the program (the instruc- tions) have to live in memory somewhere, and thus they are in the ad- dress space. The program, while it is running, uses a stack to keep track of where it is in the function call chain as well as to allocate local variables and pass parameters and return values to and from routines. Finally, the heap is used for dynamically-allocated, user-managed memory, such as that you might receive from a call to malloc() in C or new in an object- oriented language such as C++ or Java. Of course, there are other things in there too (e.g., statically-initialized variables), but for now let us just assume those three components: code, stack, and heap.

code, stack, and heap

这里写图片描述

But,

The program really isn’t in memory at physical addresses 0 through 16KB; rather it is loaded at some arbitrary physical address(es).

virtual address 0 != physical address 0

When the OS does this, we say the OS is virtualizing memory, because the running program thinks it is loaded into memory at a particular ad- dress (say 0) and has a potentially very large address space (say 32-bits or 64-bits); the reality is quite different.

Isolation

Isolation is a key principle in building reliable systems. If two entities are properly isolated from one another, this implies that one can fail with- out affecting the other. Operating systems strive to isolate processes from each other and in this way prevent one from harming the other. By using memory isolation, the OS further ensures that running programs cannot affect the operation of the underlying OS. Some modern OS’s take isolation even further, by walling off pieces of the OS from other pieces of the OS.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值