CPU 如何从 RAM 访问应用程序和数据 [英] How are applications and data accessed by the CPU from RAM

查看:30
本文介绍了CPU 如何从 RAM 访问应用程序和数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将应用程序加载到 RAM 并打开文件(因此文件的数据也存储在 RAM 中)之后,我无法理解 CPU 如何从 RAM 访问应用程序和数据.

据我了解,CPU 只是在程序计数器滴答作响或在中断后执行任务时从 RAM 获取指令.那么它如何访问应用程序和数据.是不是它没有并且仍然只是获取指令(例如,加载要在应用程序中打开的硬盘驱动器上的文件)并处理应用程序发出的任何请求,这些请求随后作为指令存储在 RAM 中(比如保存一个文件).或者与打开的文件(例如)相关的应用程序和数据是否只保留在 RAM 中而根本不被 CPU 访问.

同样,看了一篇文章,说操作系统的副本存储在 RAM 中.然后 CPU 可以访问操作系统.(我认为 CPU 只是使用来自 RAM 的指令).然后它如何与操作系统通信,以及如何从 RAM 中的操作系统副本或硬盘驱动器中的操作系统向 CPU 发送中断.

对不起,如果这真的很混乱,我没有明白.

解决方案

问题的根源: 计算机硬件和计算机软件之间缺乏明确的区别.


计算机系统的组件

为了让我们清楚地了解它们并了解它们的性质,让我声明如下:

  • 硬件:它包括 CPU、RAM、磁盘、寄存器、显卡、网卡、内存总线以及所有您可以触摸并称为计算机"的东西.是身体.
  • 软件:它包括操作系统、程序、CPU 指令、编译器、编程语言以及几乎所有关于计算机的无形的东西.它是灵魂.
  • 固件:这是对硬件工作绝对必要的基本代码.这存储在安装在硬件本身中的只读存储器上.该软件对硬件至关重要,因此被认为处于硬件和软件中间,因此称为固件.

当我们说计算机启动并运行并正确执行我们的指令时,我们将从理解开始.但是到时候你会说——我是怎么到这里的?那我就提几点关于电脑启动的问题.


按下电源键时...

...最原始的基本输入输出系统(因此称为BIOS),硬写在计算机硬件上开始执行.这是写在只读存储器上的,这将启动使机器独立运行的过程.它将软件(操作系统)从一个硬件(磁盘)加载到另一个硬件(RAM 和 CPU 寄存器)中,使软件能够与硬件正常工作.

现在身体和灵魂在一起了,个人(机器)可以工作了.


到目前为止,操作系统已经在 RAM 和 CPU 中.(如果您怀疑,请阅读按下电源按钮时.)现在让我们逐段处理您的问题-


第一段

<块引用>

我在理解应用程序和数据加载应用程序后由 CPU 从 RAM 访问进入 RAM 并打开一个文件(因此文件的数据也存储在内存).



解释如下:

  1. 这里的确切问题是您认为访问数据的是 CPU 和 RAM.CPU 和 RAM 只是执行单元.
  2. 操作系统(软件)通过 CPU 和 RAM(硬件)访问数据.它位于执行应用程序的操作系统领域.
  3. 这就是为什么您可以在同一硬件上安装 Linux 和 Windows,但不能在 Linux 中执行 .exe 文件,因为执行的是操作系统而不是 RAM/CPU.
  4. 此外,CPU 和 RAM 以及磁盘如何物理交互以引入数据、执行数据、保存数据等属于硬件领域.这需要解释,其中涉及逻辑门(AND、OR、NOT...)、二极管、电路和其他很多电子产品人员可以解释的东西.


第二段

<块引用>

据我了解,CPU 只是从 RAM 中获取指令程序计数器在中断后计时或执行任务.如何然后它是否访问应用程序和数据.难道它不并且仍然只是得到指令(例如在要在应用程序中打开的硬盘驱动器)并处理任何应用程序发出的请求存储在 RAM 中之后的说明(例如保存文件).



正如您所猜到的那样 - CPU 没有得到指令,操作系统通过 CPU 来完成.此外,就像大脑不直接指示手和腿移动而是使用神经进行交互一样,CPU 不会告诉磁盘提供/获取数据.CPU 仅与 RAM 和寄存器一起工作. 多个硬件单元协同工作,为数据和指令的传输提供路径.涉及的重要硬件有:

  1. 处理器(CPU 和 CPU 中内置的寄存器)
  2. 缓存
  3. 内存 (RAM)
  4. 磁盘
  5. 胶带

我喜欢 这个答案.这张图片不仅列出了硬件部分,还说明了这些部分在执行速度上的巨大差异.

让我们继续...


第三段

<块引用>

同样的,看了一篇文章后,说是抄袭操作系统存储在 RAM 中.然后 CPU 可以访问操作系统.(我以为 CPU 只是根据指令工作从内存).然后它如何与操作系统通信以及中断是如何从 RAM 中的操作系统副本发送到 CPU 或来自硬盘中的操作系统.



现在您已经知道操作系统确实存在于 RAM 和 CPU 寄存器中.那就是它生活的地方.那是它告诉CPU如何工作的地方.如果操作系统足够小(或者如果寄存器和缓存足够大),操作系统将更接近 CPU.

  • CPU 不与操作系统通信.它不能.受老板控制的是工人.操作系统就是那个老大.
  • CPU 无法访问操作系统. CPU 是身体,操作系统是灵魂.灵魂告诉身体该做什么,而不是相反.
  • CPU 无法处理来自 RAM 的指令.它仅执行操作系统(可能位于 RAM 中)给出的指令.因此,即使有指令将某些操作系统模块加载到 RAM 中,发出该指令的也不是 RAM/CPU,而是操作系统本身.
  • 中断有两种类型 - 硬件和软件 -你的问题是关于软件中断的.由于 OS 的执行部分在 RAM 中,简单来说我们可以说中断是从位于 RAM 中的 OS 发送到 CPU 的.

结论

硬件和软件之间缺乏区分是你困惑的根本原因.在 Coursera学术地球 加深理解.

I am having a bit of trouble understanding how applications and data are accessed by the CPU from RAM after the application has been loaded into RAM and a file opened (thus data for the file also stored in RAM).

By my understanding, a CPU just gets instructions from RAM as the program counter ticks or carries out tasks after an interrupt. How then does it access the application and data. Is it that it doesn't and still just gets instructions (for example to load a file on the hard drive to be opened in the application) and processes any requests made by the application which are stored in RAM as instructions thereafter (like saving a file). Or does the application and data relating to an opened file (for example) just stay in RAM and not get accessed by the CPU at all.

Similarly, after reading an article, it said that a copy of the operating system is stored in RAM. The CPU can then access the operating system. (I thought the CPU just worked with instructions from RAM). How does it then communicate with the operating system and how are interrupts sent to the CPU, from the copy of the OS in RAM or from the OS in the hard drive.

Sorry if this is really confusing, alot i didn't understand.

解决方案

Root of your question: Lack of clear differentiation between Computer's Hardware and Computer's Software.


Components of a Computer System

Just so that we are clear about both of them and that we understand their nature, let me state as follows:

  • Hardware: It includes CPU, RAM, Disk, Register, Graphics Card, Network Card, Memory BUS and everything that you can touch and call to be the 'Computer'. It is the body.
  • Software: It includes Operating System, Program, CPU instruction, Compiler, Programming Language and almost everything intangible about the computer. It is the soul.
  • Firmware: It is that basic code which is absolutely essential for hardware's working. This is stored on a Read Only Memory installed in the hardware itself. This piece of software is vital for hardware therefore is considered in the mid of hardware and software and hence called Firmware.

We will start with understanding from the time when we say that the computer is up and running and is properly executing our instructions. But at that time you will say - How did I reach here? So I will mention a few points about the startup of the computer.


When the power button is pressed...

...the most primitive and basic input output system (therefore called BIOS), which is hard written on the computer hardware begins execution. This is written on Read Only Memory and this starts the process to get the machine to stand on its own. And it loads the software (Operating System) from one piece of hardware (disks) into another piece of hardware (RAM and CPU registers) enabling the software to work properly with hardware.

Now the body and soul are together and the individual (machine) can work.


Until now, OS is already in RAM and CPU. (Read When the power button is pressed if you doubt it.) Let's handle your question paragraph by paragraph now -


First Paragraph

I am having a bit of trouble understanding how applications and data are accessed by the CPU from RAM after the application has been loaded into RAM and a file opened (thus data for the file also stored in RAM).



The explanation is as follows:

  1. The exact issue here is your thinking that it is CPU and RAM that access the data. CPU and RAM are only executing units.
  2. It is OS (software) that accesses the data by means of CPU and RAM (hardware). It is in the realm of OS where applications are executed.
  3. This is why you can install Linux and Windows on same hardware but cannot execute .exe files in Linux because OS does the execution and not RAM/CPU.
  4. Further, how do CPU and RAM and disk physically interact to bring in the data, execute it, save it back etc. is in the domain of hardware. That would require explanation which involves logic gates (AND, OR, NOT...), diodes, circuitry and a hell lot of other things which an Electronics guy can explain.


Second Paragraph

By my understanding, a CPU just gets instructions from RAM as the program counter ticks or carries out tasks after an interrupt. How then does it access the application and data. Is it that it doesn't and still just gets instructions (for example to load a file on the hard drive to be opened in the application) and processes any requests made by the application which are stored in RAM as instructions thereafter (like saving a file).



As you have guessed it - CPU doesn't get instructions, Operating System does it through CPU. Also, just the way brain doesn't directly instruct the hands and legs to move and instead uses nerves for interaction, the CPU doesn't tell the disks to give/take the data. CPU works with RAM and registers only. Multiple units of hardware work in conjunction to provide a path for data and instruction to travel. The important pieces of involved hardware are:

  1. Processor (CPU and registers built in the CPU)
  2. Cache
  3. Memory (RAM)
  4. Disk
  5. Tape

I like the image provided in this answer. This image not only lists the hardware pieces but also illustrates the mammoth difference in the execution speed of these pieces.

Let's move on to the...


Third Paragraph

Similarly, after reading an article, it said that a copy of the operating system is stored in RAM. The CPU can then access the operating system. (I thought the CPU just worked with instructions from RAM). How does it then communicate with the operating system and how are interrupts sent to the CPU, from the copy of the OS in RAM or from the OS in the hard drive.



By now you already know that indeed OS is present in RAM and CPU registers. That is where it lives. That is from where it tells the CPU how to work. If OS would be small enough (or if Registers and Caches would be big enough), the OS would live even closer to CPU.

  • The CPU does not communicate with the OS. It can't. It is the worker that is controlled by a boss. OS is that boss.
  • CPU cannot access Operating System. CPU is the body, OS is the soul. Soul tells the body what to do, not vice-versa.
  • CPU doesn't work with instructions from RAM. It merely executes the instructions given by the Operating System (which may be living in RAM). So even when there is an instruction to load some module of OS into the RAM, it is not RAM/CPU but OS itself that issues that instruction.
  • Interrupts are of two types - Hardware and Software - and your query is about the software interrupts. Since the executive part of OS is in the RAM, in simple words we can say that interrupts are sent to CPU from OS living in RAM.

Conclusions

The lack of distinction between hardware and software is the basic cause of your confusions. Take some course about Operating Systems on Coursera or Academic Earth for deeper understanding.

这篇关于CPU 如何从 RAM 访问应用程序和数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆