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

📄 lib0022.html

📁 Memory Management—Algorithms and implementation in C/C++ Introduction Chapter 1 - Memory Manag
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Case Study: Linux</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.books24x7.css">
</head>
<body >
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle"  alt="Team LiB"></a></div></td>
<td valign="top" class="v2" align="right"><div STYLE="MARGIN-RIGHT: 0.15in"><a href="LiB0021.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0023.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr>
</table>

<div class="chapter">
<a name="ch02"></a>
<div class="section">
<h2 class="first-section-title"><a name="176"></a><a name="ch02lev1sec3"></a>Case Study: Linux</h2><blockquote class="blockquote">
<p class="first-para">"The memory management on the PowerPC can be used to frighten small children."</p>
<p class="last-para">&mdash; Linus Torvalds</p>
</blockquote>
<p class="para">Now we arrive at the production operating systems. Linux has a sophisticated and fairly mature kernel, one that IBM is willing to sell on its mainframes. This additional, but necessary, complexity will make our discussion a little more interesting.</p>
<div class="section">
<h3 class="sect3-title">
<a name="177"></a><a name="ch02lev2sec12"></a>History and MINIX</h3>
<p class="first-para">In the beginning, there was a professor in Amsterdam named Andrew S. Tanenbaum. He wanted his students to be able to get their hands dirty with operating system internals without having to spend months dissecting a large production system. In 1987 Tanenbaum wrote MINIX, a small UNIX clone that originally ran on <a name="178"></a><a name="IDX-68"></a>the 8086/88. He wrote it from scratch and did an impressive job of making its source code easy to read and follow. I can attest to this fact, as I have read portions of the source myself. The code that programs the 8259 PIC is particularly well done. MINIX was also designed as a microkernel-based system so that the file system and memory manager could be switched with new ones at run time.</p>
<p class="para">Tanenbaum received megabytes of e-mail from people who wanted to add features to MINIX. Tanenbaum's goal, however, was to present his students with something that they could explore as a learning tool. Adding extra functionality, like demand paging, was out of the question.</p>
<p class="para">Into the picture came Linus Torvalds, a computer science student in Finland who decided that he was going to hack MINIX into a production-quality operating system. In 1991, Linus made an announcement of his intentions on the MINIX users newsgroup. By December of 1991, version 0.1 was released and Linux began its ascendance to worldwide popularity.</p>
<table border="0" cellspacing="0" cellpadding="0" class="note">
<tr>
<td valign="top" class="admon-check"></td><td valign="top" class="admon-title">Note&nbsp;</td><td valign="top" class="admon-body">
<p class="first-para">It may seem a bit odd, but one of the original detractors of Linux was Tanenbaum! In January of 1992, Tanenbaum left a posting on comp.os.minix titled "Linux is obsolete." Needless to say, he was wrong.</p>
</td>
</tr>
</table>
<a></a>
</div>
<div class="section">
<h3 class="sect3-title">
<a name="179"></a><a name="ch02lev2sec13"></a>Design Goals and Features</h3>
<p class="first-para">According to Linus' first posting on comp.os.minix, Linux started off as "just a hobby." Over the years, it has morphed into a powerful system that easily holds its own against proprietary flavors of UNIX. According to the README file in the kernel's base source code directory, "Linux is a Unix clone written from scratch by Linus Torvalds with assistance from a loosely-knit team of hackers across the Net. It aims towards POSIX compliance."</p>
<p class="para">POSIX, the Portable Operating System Interface, is a specification that establishes a standard set of operating system calls. It was introduced as an effort to help unify the UNIX operating system. Different vendors, like IBM, HP, Sun, and SGI, all sell their own version of UNIX. I should not have to tell you that these vendors have a vested interest in locking customers in to their own special blend. The POSIX standard is an effort to at least give the appearance that there is a least common denominator, even if the different vendors would be more than happy to stab each other in the back.</p>
<p class="para">The POSIX standard is maintained by the IEEE (Institute of Electrical and Electronics Engineers) and is also known as IEEE 1003.1-2001. The full title is:</p>
<a name="180"></a><a name="IDX-69"></a>
<div class="informalexample">
<pre class="literallayout">
      1003.1-2001 IEEE Std 1003.1-2001
      (Open Group Technical Standard, Issue 6),
      Portable Operating System Interface (POSIX&reg;) 2001
</pre>
</div>
<p class="para">The last time I looked, the price of this standard was around $176. This is why a search for "POSIX" on Google or Yahoo will not prove very fruitful. Trust me on this. Fortunately, someone in the Linux development effort bought a copy. It was a good investment. Any operating system worth its salt will be POSIX compliant.</p>
<p class="para">The following sample list of features supported by Linux reads like a Christmas list to Santa:</p>
<ul class="itemizedlist">
<li class="first-listitem">
<p class="first-para">Multiuser</p>
</li>
<li class="listitem">
<p class="first-para">Symmetric multiprocessing (SMP)</p>
</li>
<li class="listitem">
<p class="first-para">Protected mode memory protection</p>
</li>
<li class="listitem">
<p class="first-para">Demand-paged virtual memory</p>
</li>
<li class="listitem">
<p class="first-para">Dynamically and statically linked libraries</p>
</li>
<li class="listitem">
<p class="first-para">Multiple file system support</p>
</li>
<li class="listitem">
<p class="first-para">Loadable kernel modules (LKMs)</p>
</li>
<li class="listitem">
<p class="first-para">High-performance TCP/IP stack</p>
</li>
</ul>
<p class="para">If you compare this feature list to MMURTL's, you will see why I discussed MMURTL first.</p>
<table border="0" cellspacing="0" cellpadding="0" class="note">
<tr>
<td valign="top" class="admon-check"></td><td valign="top" class="admon-title">Note&nbsp;</td><td valign="top" class="admon-body">
<p class="first-para">It would be very easy to fill an entire book with an explanation of the inner workings of Linux. In fact, I reference a couple of such books at the end of this chapter. I am going to stick to somewhat of a tourist's view in the following discussion; this will allow me to highlight the principal architectural features, which illustrate various memory management policies, without getting too weighed down.</p>
</td>
</tr>
</table>
<a></a>
</div>
<div class="section">
<h3 class="sect3-title">
<a name="181"></a><a name="ch02lev2sec14"></a>Linux and Segmentation</h3>
<p class="first-para">Like MMURTL, Linux makes only minimal use of the Pentium's segmentation facilities. I must say, I was a little surprised by this. I was expecting Linux to implement a full-blown, four-ring segmentation scheme. The Linux kernel does not make use of an LDT. The GDT, which the kernel does use, is fairly small:</p>
<div class="informalexample">
<pre class="literallayout">
ENTRY(gdt_table)
    .quad 0x0000000000000000    /* NULL descriptor */
    .quad 0x0000000000000000    /* not used */
    .quad 0x00cf9a000000ffff    /* 0x10 kernel 4GB code at
                                   0x00000000 */
    .quad 0x00cf92000000ffff    /* 0x18 kernel 4GB data at
                                   0x00000000 */
    .quad 0x00cffa000000ffff    /* 0x23 user   4GB code at
                                   0x00000000 */<a name="182"></a><a name="IDX-70"></a>
    .quad 0x00cff2000000ffff    /* 0x2b user   4GB data at
                                   0x00000000 */
    .quad 0x0000000000000000    /* not used */
    .quad 0x0000000000000000    /* not used */
     /*
     * The APM segments have byte granularity and their bases
     * and limits are set at run time.
     */
    .quad 0x0040920000000000    /* 0x40 APM set up for bad
                                   BIOSs */
    .quad 0x00409a0000000000    /* 0x48 APM CS    code */
    .quad 0x00009a0000000000    /* 0x50 APM CS 16 code (16 bit) */
    .quad 0x0040920000000000    /* 0x58 APM DS    data */
    .fill NR_CPUS*4,8,0         /* space for TSSs and LDTs */
</pre>
</div>
<p class="para">These structures are defined in <span class="fixed">/usr/src/linux/arch/i386/kernel/head.S</span>. The previous assembly code does not use MASM-compatible directives. Instead, it follows AT&amp;T syntax. Linux only uses one more kernel segment than MMURTL, and it is used to accommodate kernel data. Let's dissect the GDT and place the results in a table for easy reading (see <a class="internaljump" href="#ch02table02">Table 2.2</a>).</p>
<a name="183"></a><a name="ch02table02"></a>
<table class="table" border="1">
<caption class="table-title">
<span class="table-title"><span class="table-titlelabel">Table 2.2</span></span>
</caption>
<thead>
<tr valign="top">
<th class="th" scope="col" align="left">&nbsp;</th><th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">Kernel Code</b>
</p>
</th><th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">Kernel Data</b>
</p>
</th><th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">User Code</b>
</p>
</th><th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">User Data</b>
</p>
</th>
</tr>
</thead>
<tbody>
<tr valign="top">
<td class="td" align="left">
<p class="table-para">
<b class="bold">Base address</b>
</p>
</td><td class="td" align="left">
<p class="table-para">0x0</p>
</td><td class="td" align="left">
<p class="table-para">0x0</p>
</td><td class="td" align="left">
<p class="table-para">0x0</p>
</td><td class="td" align="left">
<p class="table-para">0x0</p>
</td>
</tr>
<tr valign="top">
<td class="td" align="left">
<p class="table-para">
<b class="bold">Size limit</b>
</p>
</td><td class="td" align="left">
<p class="table-para">0xFFFFF</p>
</td><td class="td" align="left">
<p class="table-para">0xFFFFF</p>
</td><td class="td" align="left">
<p class="table-para">0xFFFFF</p>
</td><td class="td" align="left">
<p class="table-para">0xFFFFF</p>
</td>
</tr>
<tr valign="top">
<td class="td" align="left">
<p class="table-para">
<b class="bold">Limit units</b>
</p>
</td><td class="td" align="left">
<p class="table-para">4KB units</p>

⌨️ 快捷键说明

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