Registers, memory, hard drive, Ready, and ran. . .

What course of a computer running a program needs to experience it?

 

Master recent study C language, he wrote a simple program follow the tutorial on the book:

 

Compiled after the owner, in order Jianpanshangqiao the execution of the program "./hello":

cup not neglect the master's orders handed down word for word register hands from the keyboard, told said: "Quick to the memory handling, do not allow the owner to waiting," speed quickly register the nickname "Rocket" to no trace to a trace, the moment in front of memory, the owner handed over the data from the memory Jianpanshangqiao, said: "this is the master to the data as soon as possible," Carter, the register has disappeared, memory Although large capacity can store a lot of things, but the speed is much slower compared to the register, memory nicknamed "big truck."

 

A look at the data memory is a command execution program, looking in a warehouse under, found that the program did not run over, no warehouse, he stood in the hard drive home. Secretly crying memory, hard disk is a super large warehouse can put vast amounts of things, filling them masters of everything, and now reportedly hard disk capacity of 1T giant, 100 times larger than themselves, but to find something in there a very slow , slow you suspect life, everyone calls him "turtle" memory a few minutes after finishing things, hard to do, better than a few days, yes, for several days. . .

 

Memory to the hard disk drive door, Qiaokai Men, said: "hello quickly to find out the program code and data owner to run." HDD said slowly: "Yes I know, you have to wait a few days, here too messy, too, I'm looking for a long time." Hard disk memory to know that the goods on this efficiency, with a sigh: "I know I slowly and so on at home. "

 

Three days later, the hard disk finally dragged out of a box, shipped to the home of memory, the memory can not waste a minute, put the box loaded on the train, foot throttle, went straight to the register of the house. Register home on the old cpu everyone's on the edge, they are relatives, the house is also said to register cpu home. Register at the door waiting to no avail, and finally see the memory with a data box, straight from the door, took the register memory box, immediately next door to the cpu, cpu took the box into the studio, the room crazy machine operation up, not recovered memory, registers have been holding the operation result data, to monitor house, and soon, the display on the big screen at home showed a "Hello, World!" results.

 

Run the program displays the results:

 

Owners see the results, smile and laugh.

cpu松了一口气,拉着寄存器说:“这样不行,内存和硬盘那两个家伙太慢了,特别是硬盘,时间全耗在他那里了,我们得自己搞几个仓库在家附近,重要的东西我们自己复制一份在仓库里,不用每次都去他们那里取了。”

 

后来cpu在家附件建了三个仓库:分别叫L1,L2,L3。L1仓库离家最近就100米,但是面积也最小只有32平米;L2仓库离家300米,面积大了点有256平米;L3仓库离家1.2公里,面积也是最大的,有8192平米。寄存器不解的问:“为什么不把L1仓库建的大一点啊,我们拿东西多方便啊”,cpu说:“我们家在市中心,你知道这里的房价多贵吗?建大了主人根本负担不起,主人才刚毕业呢,哪里有钱建这么大的仓库?大的仓库只能往郊区建,我们家旁边只能建两个小的,有的cpu家还只有L1,L2两个仓库呢,你就知足吧。”

 

“下次我们取数据的时候,先去离家最近的L1仓库看看有没有,如果没有的话再去L2仓库看,如果也没有就去L3仓库查一下,都没有找到的话,再去内存家取,取到数据后,我们就在我们自己家的仓库里备份一份,下次用到就直接拿来用,就不用去找内存和硬盘要了,这样我们的速度就快很多了。”cpu得意的说。

 

越往金字塔上面的设备速度越快,容量越小;越往金字塔下面的设备速度越慢,容量越大:

 

 

cup芯片的结构如下图,主要有寄存器,程序计数器(PC),算术/逻辑单元(ALU)和高速缓存存储器组成

 

 

以下是寄存器,高速缓存,主存,硬盘的速度和容量的大小:

 

  • 寄存器,速度0.3纳秒,大小约1K
  • L1一级高速缓存,速度0.9纳秒,大小约32K
  • L2二级高速缓存,速度2.8纳秒,大小约256K
  • L3三级高速缓存,速度12.9纳秒,大小约8M
  • 主存(内存),速度120纳秒,大小约8G
  • 硬盘,速度10 000 000纳秒,大小约1T

1s(秒) = 1000 ms(毫秒) = 1000 000 μs(微秒) = 1000 000 000 ns(纳秒)

 

如果把上图的0.3纳秒同步放大到1秒,cpu去寄存器取一个数据只需要1秒,但是到机械硬盘取要大约1年的时间。。。。。I/O是常见的速度瓶颈,我们写程序的时候尽量在内存,高速缓存上取数据,内存可比硬盘快100倍呢。

 

硬盘笑而不语,心想:“你们速度快有什么了不起的,一断电所有的数据都会清空,每次还不是都得重新从我这拿数据。”

 

第二天主人一开机,cpu:“我是谁?我在哪里?你们是谁?”,寄存器,内存,一脸懵逼:“我们什么都不记得了,我们是谁?”硬盘在一边乐了:“这帮逗比又开始了。。。”

 

欢迎关注我的微信公众号

 

 

 

发布了13 篇原创文章 · 获赞 169 · 访问量 6656

Guess you like

Origin blog.csdn.net/zhanyd/article/details/100182317