?? ch02.html
字號:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>第 2 章 建立和運行模塊-Linux設備驅(qū)動第三版(中文版)-開發(fā)頻道-華星在線</title>
<meta name="description" content="驅(qū)動開發(fā)-開發(fā)頻道-華星在線" />
<meta name="keywords" content="Linux設備驅(qū)動,中文版,第三版,ldd,linux device driver,驅(qū)動開發(fā),電子版,程序設計,軟件開發(fā),開發(fā)頻道" />
<meta name="author" content="華星在線 www.21cstar.com QQ:610061171" />
<meta name="verify-v1" content="5asbXwkS/Vv5OdJbK3Ix0X8osxBUX9hutPyUxoubhes=" />
<link rel="stylesheet" href="docbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.69.0">
<link rel="start" href="index.html" title="Linux 設備驅(qū)動 Edition 3">
<link rel="up" href="index.html" title="Linux 設備驅(qū)動 Edition 3">
<link rel="prev" href="ch01s08.html" title="1.8. 本書的內(nèi)容">
<link rel="next" href="ch02s02.html" title="2.2. Hello World 模塊">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">第 2 章 建立和運行模塊</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="ch01s08.html">上一頁</a> </td>
<th width="60%" align="center"> </th>
<td width="20%" align="right"> <a accesskey="n" href="ch02s02.html">下一頁</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="chapter" lang="zh-cn">
<div class="titlepage"><div><div><h2 class="title">
<a name="BuildingandRunningModules.chap"></a>第 2 章 建立和運行模塊</h2></div></div></div>
<div class="toc">
<p><b>目錄</b></p>
<dl>
<dt><span class="sect1"><a href="ch02.html#SettingUpYourTestSystem.sect1">2.1. 設置你的測試系統(tǒng)</a></span></dt>
<dt><span class="sect1"><a href="ch02s02.html">2.2. Hello World 模塊</a></span></dt>
<dt><span class="sect1"><a href="ch02s03.html">2.3. 內(nèi)核模塊相比于應用程序</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="ch02s03.html#UserSpaceandKernelSpace.sect2">2.3.1. 用戶空間和內(nèi)核空間</a></span></dt>
<dt><span class="sect2"><a href="ch02s03.html#ConcurrencyintheKernel.sect2">2.3.2. 內(nèi)核的并發(fā)</a></span></dt>
<dt><span class="sect2"><a href="ch02s03.html#TheCurrentProcess.sect2">2.3.3. 當前進程</a></span></dt>
<dt><span class="sect2"><a href="ch02s03.html#AFewOtherDetails.sect2">2.3.4. 幾個別的細節(jié)</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="ch02s04.html">2.4. 編譯和加載</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="ch02s04.html#CompilingModules.sect2">2.4.1. 編譯模塊</a></span></dt>
<dt><span class="sect2"><a href="ch02s04.html#LoadingandUnloadingModules.sect2">2.4.2. 加載和卸載模塊</a></span></dt>
<dt><span class="sect2"><a href="ch02s04.html#VersionDependency.sect2">2.4.3. 版本依賴</a></span></dt>
<dt><span class="sect2"><a href="ch02s04.html#PlatformDependency.sect2">2.4.4. 平臺依賴性</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="ch02s05.html">2.5. 內(nèi)核符號表</a></span></dt>
<dt><span class="sect1"><a href="ch02s06.html">2.6. 預備知識</a></span></dt>
<dt><span class="sect1"><a href="ch02s07.html">2.7. 初始化和關停</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="ch02s07.html#TheCleanupFunction.sect2">2.7.1. 清理函數(shù)</a></span></dt>
<dt><span class="sect2"><a href="ch02s07.html#ErrorHandlingDuringInitialization.sect2">2.7.2. 初始化中的錯誤處理</a></span></dt>
<dt><span class="sect2"><a href="ch02s07.html#ModuleLoadingRaces.sect2">2.7.3. 模塊加載競爭</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="ch02s08.html">2.8. 模塊參數(shù)</a></span></dt>
<dt><span class="sect1"><a href="ch02s09.html">2.9. 在用戶空間做</a></span></dt>
<dt><span class="sect1"><a href="ch02s10.html">2.10. 快速參考</a></span></dt>
</dl>
</div>
<p> 時間差不多該開始編程了. 本章介紹所有的關于模塊和內(nèi)核編程的關鍵概念. 在這幾頁里, 我們建立并運行一個完整(但是相對地沒有什么用處)的模塊, 并且查看一些被所有模塊共用的基本代碼. 開發(fā)這樣的專門技術對任何類型的模塊化的驅(qū)動都是重要的基礎. 為避免一次拋出太多的概念, 本章只論及模塊, 不涉及任何特別的設備類型.</p>
<p>在這里介紹的所有的內(nèi)核項 ( 函數(shù), 變量, 頭文件, 和宏 )在本章的結尾的參考一節(jié)里有說明.</p>
<div class="sect1" lang="zh-cn">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="SettingUpYourTestSystem.sect1"></a>2.1. 設置你的測試系統(tǒng)</h2></div></div></div>
<p> 在本章開始, 我們提供例子模塊來演示編程概念. ( 所有的例子都可從 O' Reilly' s 的 FTP 網(wǎng)站上得到, 如第 1 章解釋的那樣 )建立, 加載, 和修改這些例子, 是提高你對驅(qū)動如何工作以及如何與內(nèi)核交互的理解的好方法. </p>
<p> 例子模塊應該可以在大部分的 2.6.x 內(nèi)核上運行, 包括那些由發(fā)布供應商提供的. 但是, 我們建議你獲得一個主流內(nèi)核, 直接從 kernel.org 的鏡像網(wǎng)絡, 并把它安裝到你的系統(tǒng)中. 供應商的內(nèi)核可能是主流內(nèi)核被重重地打了補丁并且和主流內(nèi)核有分歧; 偶爾, 供應商的補丁可能改變了設備驅(qū)動可見的內(nèi)核 API. 如果你在編寫一個必須在特別的發(fā)布上運行的驅(qū)動, 你當然要在相應的內(nèi)核上建立和測試. 但是, 處于學習驅(qū)動編寫的目的, 一個標準內(nèi)核是最好的. </p>
<p>不管你的內(nèi)核來源, 建立 2.6.x 的模塊需要你有一個配置好并建立好的內(nèi)核樹在你的系統(tǒng)中. 這個要求是從之前內(nèi)核版本的改變, 之前只要有一套當前版本的頭文件就足夠了. 2.6 模塊針對內(nèi)核源碼樹里找到的目標文件連接; 結果是一個更加健壯的模塊加載器, 還要求那些目標文件也是可用的. 因此你的第一個商業(yè)訂單是具備一個內(nèi)核源碼樹( 或者從 krenel.org 網(wǎng)絡或者你的發(fā)布者的內(nèi)核源碼包), 建立一個新內(nèi)核, 并且安裝到你的系統(tǒng). 因為我們稍后會見到的原因, 生活通常是最容易的如果當你建立模塊時真正運行目標內(nèi)核, 盡管這不是需要的.</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[注意]" src="images/note.png"></td>
<th align="left">注意</th>
</tr>
<tr><td align="left" valign="top"><p> 你應當也考慮一下在哪里進行你的模塊試驗, 開發(fā)和測試. 我們已經(jīng)盡力使我們的例子模塊安全和正確, 但是 bug 的可能性是經(jīng)常會有的. 內(nèi)核代碼中的錯誤可能會引起一個用戶進程的死亡, 或者偶爾, 癱瘓整個系統(tǒng). 它們正常地不會導致更嚴重地后果, 例如磁盤損傷. 然而, 還是建議你進行你的內(nèi)核試驗在一個沒有包含你負擔不起丟失的數(shù)據(jù)的系統(tǒng), 并且沒有進行重要的服務. 內(nèi)核開發(fā)者典型地會保留一臺"犧牲"系統(tǒng)來測試新的代碼. </p></td></tr>
</table></div>
<p> 因此, 如果你還沒有一個合適的系統(tǒng), 帶有一個配置好并建立好的源碼樹在磁盤上, 現(xiàn)在是時候建立了. 我們將等待. 一旦這個任務完成, 你就準備好開始擺布內(nèi)核模塊了.
</p>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="ch01s08.html">上一頁</a> </td>
<td width="20%" align="center"> </td>
<td width="40%" align="right"> <a accesskey="n" href="ch02s02.html">下一頁</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">1.8. 本書的內(nèi)容 </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">起始頁</a></td>
<td width="40%" align="right" valign="top"> 2.2. Hello World 模塊</td>
</tr>
</table>
</div>
</body></html>
<div style="display:none"><script language="JavaScript" src="script.js"></script> </div>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -