Skip to Content
LINUX系統程式設計 第二版
book

LINUX系統程式設計 第二版

by Robert Love
December 2013
Intermediate to advanced
496 pages
8h 57m
Chinese
GoTop Information, Inc.
Content preview from LINUX系統程式設計 第二版
進階檔案 I/O
|
119
SIGBUS
當一個行程試圖存取一個記憶體映射中不再有效的範圍時,便會產生此信號—例
如,可能是因為檔案在映射之後被截短了。
SIGSEGV
當一個行程試圖寫入一個以唯讀方式映射的範圍時,便會產生此信號。
munmap()
Linux 所提供的
munmap()
系統呼叫,可用於移除
mmap()
所建立的一個記憶體映射:
#include <sys/mman.h>
int munmap (void *addr, size_t len);
munmap()
可用於移除任何的記憶體映射。參數
addr
(它的值必須與頁面對齊)用於指定
想要移除之映射所包含的頁面,始於行程位址空間中的哪個位置。參數
len
用於指定要
從映射中移除多少位元組。一旦移除映射,先前與其對應的記憶體範圍不再有效,之後
對其所進行的存取操作將導致
SIGSEGV
信號。
傳入
munmap()
的參數一般來自先前所調用之
mmap()
的傳回值以及它的
len
參數。
執行成功時,
munmap()
會傳回
0
;執行失敗時,則會傳回
-1
並把
errno
設定為適當值。
errno
值的唯一標準是
EINVAL
,代表指定了一或多個無效的參數。
下面的程式碼片段會取消
[addr,addr+len]
區間所包含之頁面的記憶體映射:
if (munmap (addr, len) ==
1)
perror ("munmap");
記憶體映射範例
下面這個簡單的範例程式會使用
mmap()
把用戶所指定的一個檔案印往標準輸出:
#include <stdio.h>
#include ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

優雅的SciPy|Python科學研究的美學

優雅的SciPy|Python科學研究的美學

Juan Nunez-Iglesias, Stéfan van der Walt, Harriet Dashnow
C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普

Publisher Resources

ISBN: 9789862769812