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系統程式設計 第二版
332
|
第九章
memset()
,直接使用
calloc()
會比較好:
glibc
知道匿名映射已經被填滿零值,而且
calloc()
會對「一個映射不需要明確填入零值」感到滿意。
系統呼叫
munmap()
會釋出一個匿名映射,並把已分配的記憶體釋回核心:
int ret;
/* 釋出 'p' 之後,會釋回 512 KB 的映射 */
ret = munmap (p, 512 * 1024);
if (ret)
perror ("munmap");
mmap()
munmap()
以及映射的相關細節可參考第 4 章。
映射 /dev/zero
其他的 Unix 系統,例如 BSD,並不具備
MAP_ANONYMOUS
旗標。事實上,它們是透過映射
一個特殊的裝置檔,
/dev/zero
,來實作類似的解決方案。此裝置檔為匿名記憶體提供了
一致的語意。一個映射包含了填滿零值的「寫入時才複製」(copy-on-write)頁面;因
此映射
/dev/zero
的行為如同使用匿名記憶體。
Linux 之上總是可以找到一個
/dev/zero
裝置,而且提供映射此檔案與取得填滿零值之記
憶體的能力。的確,引進
MAP_ANONYMOUS
之前,Linux 程式設計者就是採用此做法。為
了向下相容於較舊版的 Linux,或是移植到其他 Unix 系統,開發者仍然可以映射
/dev/
zero
,以便取代建立匿名映射的做法。這與「映射任何其他檔案」並無不同:
void *p;
int fd;
/* 開啟 /dev/zero 以備讀取與寫入 */
fd = open ...
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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

深入理解運算原理|從簡單的機器到無所不能的程式

深入理解運算原理|從簡單的機器到無所不能的程式

Tom Stuart
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
算法技术手册(原书第2 版)

算法技术手册(原书第2 版)

George T.Heineman, Gary Pollice, Stanley Selkow

Publisher Resources

ISBN: 9789862769812