# 存储系统原理

# 存储系统分类

存储系统包括:

  • cache 存储系统
    • 包括 cache 和主存
    • 目的是提高存储器速度
  • 虚拟存储系统
    • 包括主存和硬盘 (辅存)
    • 目的是扩大存储器容量

# 存储系统层次结构

  • 通用寄存器
  • 高速缓存
  • 主存储器
  • 辅存储器
  • 脱机大容量存储器

# 存储系统的速度

H=N1/(N1+N2)H=N_1/(N_1+N_2) 是命中率,则访问周期为 T=HT1+(1H)T2T = HT_1 + (1-H)T_2. H1H \to 1, 则 TT1T \to T_1.

存储系统的访问效率定义为

e=T1T=1H+(1H)T2/T1=f(H,T2T1)e = \frac{T_1}{T} = \frac{1}{H + (1-H){T_2}/{T_1}} = f(H, \frac{T_2}{T_1})

# 虚拟存储器

虚拟存储器的管理方式有三种:

  • 段式管理
  • 页式管理
  • 段页式管理

# 页面替换算法

# LRU 算法

LRU (least recently used) 算法伪代码如下:

# LRU code
# defining the table size
table_size = 3
reference_string = [0, 1, 2, 3, 0, 1, 4, 0, 1, 2, 3, 4]
				
# the list which stores the current pages in memory
# and where page replacement is executed.
pages = []
# page faults
faults = 0
# iterating through the ref string
for page in reference_string:
	# check if page already exists in the list, 
	# if yes, we just remove the page and append it
	# to the end of the list (last index).
	if page in pages:
		# removing
		pages.remove(page)
		# appending
		pages.append(page)
	# if page is not there
	else:
		# we first check length of page list. 
		# if still spots left in the list, we first fill it
		if(len(pages) < table_size):
			pages.append(page)
		else:
			# if the page list is filled. We remove the 
			# first page. 
			pages.remove(pages[0])
			# and then we append page to the end of list. 
			pages.append(page)
		# Increment 1 in Page faults
		faults +=1
	
print("total page faults = ", faults)