⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1143.html

📁 著名的linux英雄站点的文档打包
💻 HTML
📖 第 1 页 / 共 5 页
字号:
个人认为,LINUX的存储管理。在不同的机器上是完全不同的,至于linux是否为他们提供了统一的接口。我还不太清楚。可以参见其他机型的源码<br>
<br>
读核日记(七) --linux的内存管理机制(1)  <br>
本文出自:http://os.silversand.net 作者: sunmoon (2001-09-01 13:05:00)      <br>
内存管理是一个操作系统必不可少.并且.非常重要的一环.linux的成功.和它优秀的内存管理联系非常密切.因为一个系统的高效性欲稳定性往往决定于它的内存管理机制.我项很多人吃过dos下640k 的苦吧.<br>
<br>
前面我们介绍了386保护模式.从今天起我们将在此基础上,分析linux的虚拟存储管理,对每个程序员来说.他们都希望有无穷大的快速的内存,然而,现阶段是不可能的,况且,无穷大与快速本身就可能矛盾<br>
<br>
为了解决无穷大.linux 引入了虚拟存储系统,为了解决快速,linux 引入了cache ,交换机制等等,以使的存储系统,在容量上接近硬盘,速度上接近cache.(当然,我认为这是存储系统的实际目的).<br>
<br>
Linux 的内存管理采取的是分页机制.它的设计目的是分时多任务.linux 可同时处理256个任务(这应该与某个变量来定义,一时想不起来).同时它采用了两级饱和机制来分别内核进程与用户进程.<br>
<br>
在386 保护模式的0-4G 的线性虚拟地址中,3-4G 是留给内核进程的.而0-3G分给用户进程.内核在内核空间的寻址不同于用户进程在用户空间的寻址.因为内核是在启动时装入内存的.说以它可以直接吧地址映射到3G 以上.用户若想访问内核就不许通过swapper_pg_div 中的指针来得到页表.<br>
<br>
相反,用户进程,在用户空间的寻址是通过所用户页目录中的指针得到用户的页表.并通过页表的指针直接指向相应的物理内存.<br>
<br>
Linux虚拟内存的实现,需要几种不同的机制来实现:<br>
<br>
地址映射机制<br>
<br>
内存的分配与回收<br>
<br>
请页机制<br>
<br>
交换机制<br>
<br>
内存共享机制<br>
<br>
在具体的读源码之前.我们先根据我们以前学过的操作系统知识.和C语言等知识.来考虑一下,这几个机制如何实现.现自己设计一下.在看别人是怎样实现的.找到自己想不到.或者对效率空间有损的地方.这样才有进步.我不止一次的说.操作系统的某一部分,就起实现来说,非常简单.它的难点是如何将大量的功能集成出一个kernerl.<br>
<br>
地址映射机制,说白了,就是在虚拟内存与物理内存上的一个桥梁.它要做的事情可能就是通过几个不同的表.把虚拟地址转换成物理地址,把物理地址转化虚拟地址.<br>
<br>
我们以前说过.因为有系统与用户之分,它必须也要有不同的数据结构.为了解决速度等问题.它会有一个硬件的缓冲区<br>
<br>
对于它的数据结构.我们可以先想一下.如虚拟地址的信息,虚拟地址在那个区域等等<br>
<br>
至于请页机制,更好理解.因为linux是页式存储的.因此必然会存在空白页和使用页.既然是页.就必然会存在页溢出.页无效(是不是在win98 下经常出现类似错误,当然linux的内存管理不可能和windows一样,可基本道理相同).因此.在每一个页出错.或者该页存不下多余的数据时.就要要求内核分配新的页面<br>
<br>
同时.当时用fork() 产生一个新的进程时.也需要分配新的叶面.这一部分大概讲的就是进程如何向内和描述自己需要怎么样的和多少页<br>
<br>
在我们学习&lt;&lt;数据结构&gt;&gt;是我们学了,很多内存分配方式,如首次拟和.最佳拟和,最差拟和等等.但是我们可以想象.linux 大概不会用他们.那就一定是伙伴系统了.因此我们可以对于伙伴系统的分配,回收的基本算法.回想一下.这样在读者一部分源码时,回有意象不到的收获.<br>
<br>
至于交换机制.我们也可以现想一想.内存中总与很多使用者的页.如果这些也已经把所有的页都用完了.再分配时必须把其中的某些页释放.释放那些页,需要考虑.如最近不用页.近期少用页,等等都可以在考虑之中.<br>
<br>
这个算法,大概就是计算内存中使用的页,什么时候可以换处.说白了就是为所有的使用页计算一个”权”,而这个”权”就决定了他什么时候被释放以换如它的内容.需要想的是对于经常使用的页.可以把它放入cahe.(尽管这一部分对程序员是透明的,但我们应该理解他的原理).<br>
<br>
最后的一部分共享内存,我想和我门初学linux编程时,进程通讯里面的共享内存没有区别.大概也就是在它的数据结构中加入可以允许不同进程访问的tag 就行了.<br>
<br>
以上,只是我们对linux的内存管理机制的猜测,需要我们做的工作就是具体的读源码.更正不正确的猜想.同时学习别人的实际思路.<br>
<br>
从下篇日记开始.我们将分别讲解这几部分的实现<br>
<br>
读核日记(八) --linux的内存管理机制(2)  <br>
本文出自:http://os.silversand.net 作者: sunmoon (2001-09-02 07:05:00)      <br>
地址的映射机制<br>
<br>
地址的映射机制,主要完成主存.辅存.和虚存之间的关联.包括磁盘文件到虚存的映射和虚存与内存的映射关系.为了虚拟存储和进程调度相一致.linux 采用可一系列的数据结构,和一个硬件缓存(TLB)来实现地址映射机制.<br>
<br>
mm_strut 用来描述进程的缓存.<br>
<br>
struct mm_struct<br>
<br>
{<br>
<br>
struct vm_area_struct * mmap; /* list of VMAs */<br>
<br>
struct vm_area_struct * mmap_avl; /* tree of VMAs */<br>
<br>
struct vm_area_struct * mmap_cache; /* last find_vma result */<br>
<br>
pgd_t * pgd;<br>
<br>
atomic_t count;<br>
<br>
int map_count; /* number of VMAs */<br>
<br>
struct semaphore mmap_sem;<br>
<br>
spinlock_t page_table_lock;<br>
<br>
unsigned long context;<br>
<br>
unsigned long start_code, end_code, start_data, end_data;<br>
<br>
unsigned long start_brk, brk, start_stack;<br>
<br>
unsigned long arg_start, arg_end, env_start, env_end;<br>
<br>
unsigned long rss, total_vm, locked_vm;<br>
<br>
unsigned long def_flags;<br>
<br>
unsigned long cpu_vm_mask;<br>
<br>
unsigned long swap_cnt; /* number of pages to swap on next pass */<br>
<br>
unsigned long swap_address;<br>
<br>
/*<br>
<br>
* This is an architecture-specific pointer: the portable<br>
<br>
* part of Linux does not know about any segments.<br>
<br>
*/<br>
<br>
void * segments;<br>
<br>
};<br>
<br>
他描述了一个进程的页目录,有关进程的上下文信息.以及数据.代码.堆栈的启示结束地址.还有虚拟存储取得数目.以及调度存储用的链表指针.他的参差比较高<br>
<br>
较高层次的vm_area-struct 是描述进程的虚拟地址区域.他形成一个算相链表.按虚地址下降排列.这样当内核需要在一个给定进程页上执行给定操作时.客从双向列表中找到该项.在世想有关页的处理.如.页错误.页换出等等<br>
<br>
他的具体结构如下:<br>
<br>
struct vm_area_struct {<br>
<br>
struct mm_struct * vm_mm; /* VM area parameters */<br>
<br>
unsigned long vm_start;<br>
<br>
unsigned long vm_end;<br>
<br>
 <br>
<br>
/* linked list of VM areas per task, sorted by address */<br>
<br>
struct vm_area_struct *vm_next;<br>
<br>
 <br>
<br>
pgprot_t vm_page_prot;<br>
<br>
unsigned short vm_flags;<br>
<br>
 <br>
<br>
/* AVL tree of VM areas per task, sorted by address */<br>
<br>
short vm_avl_height;<br>
<br>
struct vm_area_struct * vm_avl_left;<br>
<br>
struct vm_area_struct * vm_avl_right;<br>
<br>
 <br>
<br>
/* For areas with inode, the list inode-&gt;i_mmap, for shm areas,<br>
<br>
* the list of attaches, otherwise unused.<br>
<br>
*/<br>
<br>
struct vm_area_struct *vm_next_share;<br>
<br>
struct vm_area_struct **vm_pprev_share;<br>
<br>
 <br>
<br>
struct vm_operations_struct * vm_ops;<br>
<br>
unsigned long vm_offset;<br>
<br>
struct file * vm_file;<br>
<br>
unsigned long vm_pte; /* shared mem */<br>
<br>
};<br>
<br>
而page 结构 则是对物理页进行描述的一个数据结构,他不是一个真正的物理页.而只不过是描述了一个物理页的内容和框架.作了逻辑页的一个标志;.他的标志域定义了这个页在进行的操作.链域则定义了一个双项链表.时的页框.可以很容易的查找到.为实际物理内存的使用直到方便<br>
<br>
他的具体结构如下<br>
<br>
typedef struct page {<br>
<br>
/* these must be first (free area handling) */<br>
<br>
struct page *next;<br>
<br>
struct page *prev;<br>
<br>
struct inode *inode;<br>
<br>
unsigned long offset;<br>
<br>
struct page *next_hash;<br>
<br>
atomic_t count;<br>
<br>
unsigned long flags; /* atomic flags, some possibly updated asynchronously */<br>
<br>
wait_queue_head_t wait;<br>
<br>
struct page **pprev_hash;<br>
<br>
struct buffer_head * buffers;<br>
<br>
int owner; /* temporary debugging check */<br>
<br>
} mem_map_t;<br>
<br>
所有的page 结构将都被转入一个叫做mem_map 的数组中.<br>
<br>
当一个进程运行时,他的代码段和数据段将都会被调入内存.如果它使用了共享库.共享客的内容也将贝雕如内存.进程运行时.系统首先分配一个vm_area_struct 给进程.并将这各进程连结到虚拟内存的连标中去.这是根据进程的可执行影像中的信息.吧数据段和客执行代码非配内存.新分配的内存必须和进程已有的内存连结起来才能应用.这样聚会出现页故障.系统利用了请页机制来避免对物理内存的过分使用.但进程访问的虚存不在当前的物理内存时,这时系统会将需要的页调入内存.同时修改进程的页表.用来标志虚拟页是否在物理内存中.<br>
<br>
因此,系统用了较复杂的数据结构来跟踪进程的虚拟地址.在task_struct 中包含一个指向mm_struct 结构的指针.进程的mm_struct 中则包含了进程可执行影像的页目录指针pgd.还包含了指向vm_area_struct 的几个指针,每个vm_area_struct 包含一个进程的虚拟地址区域.<br>
<br>
一个进程有多个vm_area_stuct 结构.linux 要经常对进程分配..或调整vm_area_struct .这样对vm_area_stuct 的查找效率.对系统很有影像.所以在这里将所有的vm_area_struct 形成了一个查找效率较高的平衡二叉树结构.<br>
<br>
我个人认为,在整个linux内核中这个地方.数据结构是最复杂的.如果把这一部分肯下来以后,整个内核便开始清晰了<br>
<br>
</FONT><br>
                                      </TD>
                                    </TR>
                                <TR>
                                <TD colSpan=2><FONT 
                                class=middlefont></FONT><BR>
                                        <FONT 
                                class=normalfont>全文结束</FONT> </TD>
                                    </TR>
                                <TR>
                                <TD background="images/dot.gif" tppabs="http://www.linuxhero.com/docs/images/dot.gif" colSpan=2 
                                height=10></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV></TD>
                        <TD vAlign=top width="20%" 
                      background="images/line.gif" tppabs="http://www.linuxhero.com/docs/images/line.gif" rowSpan=2> 
                          <DIV align=center> 
                            <table class=tableoutline cellspacing=1 cellpadding=4 
                        width="100%" align=center border=0>
                              <tr class=firstalt> 
                                <td noWrap background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif" colspan=2 height=21>
                                <font class=normalfont><b>所有分类</b></font></td>
                              </tr>
<tr class=secondalt> <td noWrap width=27%> <font class=normalfont>1:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type1.html" tppabs="http://www.linuxhero.com/docs/type1.html">非技术类</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>2:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type2.html" tppabs="http://www.linuxhero.com/docs/type2.html">基础知识</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>3:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type3.html" tppabs="http://www.linuxhero.com/docs/type3.html">指令大全</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>4:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type4.html" tppabs="http://www.linuxhero.com/docs/type4.html">shell</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>5:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type5.html" tppabs="http://www.linuxhero.com/docs/type5.html">安装启动</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>6:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type6.html" tppabs="http://www.linuxhero.com/docs/type6.html">xwindow</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>7:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type7.html" tppabs="http://www.linuxhero.com/docs/type7.html">kde</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>8:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type8.html" tppabs="http://www.linuxhero.com/docs/type8.html">gnome</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>9:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type9.html" tppabs="http://www.linuxhero.com/docs/type9.html">输入法类</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>10:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type10.html" tppabs="http://www.linuxhero.com/docs/type10.html">美化汉化</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>11:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type11.html" tppabs="http://www.linuxhero.com/docs/type11.html">网络配置</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>12:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type12.html" tppabs="http://www.linuxhero.com/docs/type12.html">存储备份</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>13:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type13.html" tppabs="http://www.linuxhero.com/docs/type13.html">杂项工具</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>14:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type14.html" tppabs="http://www.linuxhero.com/docs/type14.html">编程技术</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>15:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type15.html" tppabs="http://www.linuxhero.com/docs/type15.html">网络安全</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>16:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type16.html" tppabs="http://www.linuxhero.com/docs/type16.html">内核技术</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>17:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type17.html" tppabs="http://www.linuxhero.com/docs/type17.html">速度优化</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>18:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type18.html" tppabs="http://www.linuxhero.com/docs/type18.html">apache</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>19:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type19.html" tppabs="http://www.linuxhero.com/docs/type19.html">email</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>20:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type20.html" tppabs="http://www.linuxhero.com/docs/type20.html">ftp服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>21:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type21.html" tppabs="http://www.linuxhero.com/docs/type21.html">cvs服务</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>22:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type22.html" tppabs="http://www.linuxhero.com/docs/type22.html">代理服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>23:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type23.html" tppabs="http://www.linuxhero.com/docs/type23.html">samba</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>24:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type24.html" tppabs="http://www.linuxhero.com/docs/type24.html">域名服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>25:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type25.html" tppabs="http://www.linuxhero.com/docs/type25.html">网络过滤</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>26:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type26.html" tppabs="http://www.linuxhero.com/docs/type26.html">其他服务</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>27:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type27.html" tppabs="http://www.linuxhero.com/docs/type27.html">nfs</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>28:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type28.html" tppabs="http://www.linuxhero.com/docs/type28.html">oracle</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>29:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type29.html" tppabs="http://www.linuxhero.com/docs/type29.html">dhcp</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>30:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type30.html" tppabs="http://www.linuxhero.com/docs/type30.html">mysql</a></font></td>    </tr>  </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>31:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type31.html" tppabs="http://www.linuxhero.com/docs/type31.html">php</a></font></td>    </tr>  </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>32:</font> </td><td noWrap width=73%>   <table width=100% border=0>    <tr>       <td><font class=normalfont><a href="type32.html" tppabs="http://www.linuxhero.com/docs/type32.html">ldap</a></font></td>    </tr>  </table></td></tr>                            </table>
                          </DIV></TD></TR>
                    <TR vAlign=top>
                        <TD width="80%"> 
                          <DIV align=center><BR>
                          </DIV>
                        </TD></TR></TBODY></TABLE></T

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -