Table Of Content(cid:74)(cid:62)(cid:59)(cid:22)(cid:66)(cid:63)(cid:68)(cid:75)(cid:78)(cid:22)
(cid:70)(cid:72)(cid:69)(cid:61)(cid:72)(cid:55)(cid:67)(cid:67)(cid:63)(cid:68)(cid:61)(cid:22)
(cid:63)(cid:68)(cid:74)(cid:59)(cid:72)(cid:60)(cid:55)(cid:57)(cid:59)
(cid:34)(cid:1)(cid:45)(cid:74)(cid:79)(cid:86)(cid:89)(cid:1)(cid:66)(cid:79)(cid:69)(cid:1)(cid:54)(cid:47)(cid:42)(cid:57)(cid:157)(cid:1)(cid:52)(cid:90)(cid:84)(cid:85)(cid:70)(cid:78)(cid:1)(cid:49)(cid:83)(cid:80)(cid:72)(cid:83)(cid:66)(cid:78)(cid:78)(cid:74)(cid:79)(cid:72)(cid:1)(cid:41)(cid:66)(cid:79)(cid:69)(cid:67)(cid:80)(cid:80)(cid:76)
(cid:46)(cid:42)(cid:36)(cid:41)(cid:34)(cid:38)(cid:45)(cid:1)(cid:44)(cid:38)(cid:51)(cid:51)(cid:42)(cid:52)(cid:44)
PRAISE FOR THE LINUX PROGRAMMING INTERFACE
“If I had to choose a single book to sit next to my machine when writing
software for Linux, this would be it.”
—MARTIN LANDERS, SOFTWARE ENGINEER, GOOGLE
“Michael Kerrisk’s new book [is] a thing of beauty. A book you’re proud to
just have on the shelf, but too useful to stay there.”
—SERGE HALLYN, LINUX KERNEL DEVELOPER
“I found The Linux Programming Interface to be extremely useful and expect
toreturn to it frequently. Anyone who has an interest in programming for
Linux will likely feel the same way.”
—JAKE EDGE, LWN.NET
“This book, with its detailed descriptions and examples, contains everything
you need to understand the details and nuances of the low-level programming
APIs in Linux . . . no matter what the level of reader, there will be something
to be learnt from this book.”
—MEL GORMAN, AUTHOR OF Understanding the Linux Virtual Memory Manager
“Michael’s inexhaustible determination to get his information right, and to
express it clearly and concisely, has resulted in a strong reference source for
programmers. While this work is targeted at Linux programmers, it will be of
value to any programmer working in the UNIX/POSIX ecosystem.”
—DAVID BUTENHOF, AUTHOR OF Programming with POSIX Threads AND
CONTRIBUTOR TO THE POSIX AND UNIX STANDARDS
“Simply the best Linux programming book since Steven’s Unix program-
mingbooks.”
—CHRIS BARBER, CB1, INC.
“. . . encyclopedic in the breadth and depth of its coverage, and textbook-
likein its wealth of worked examples and exercises. Each topic is clearly
andcomprehensively covered, from theory to hands-on working code.
Professionals, students, educators, this is the Linux/UNIX reference that
youhave been waiting for.”
—ANTHONY ROBINS, ASSOCIATE PROFESSOR OF COMPUTER SCIENCE, THE
UNIVERSITY OF OTAGO
“. . . a very thorough—yet easy to read—explanation of UNIX system and
network programming, with an emphasis on Linux systems. It’s certainly a
book I’d recommend to anybody wanting to get into UNIX programming (in
general) or to experienced UNIX programmers wanting to know what’s new
in the popular GNU/Linux system.”
—FERNANDO GONT, NETWORK SECURITY RESEARCHER, IETF PARTICIPANT, AND
RFC AUTHOR
“If you think you don’t need this book since you know everything already,
that’s what I thought too, and I was wrong.”
—BERT HUBERT, NETHERLABS COMPUTER CONSULTING
“Michael’s book is a different kind of beast . . . everything relating to the
subject that I could reasonably think of is in the book, in a very thorough
andmaniacally complete yet enjoyably readable way.”
—FEDERICO LUCIFREDI, SLASHDOT.ORG
“. . . an essential resource for the serious or professional Linux and UNIX
systems programmer. Michael Kerrisk covers the use of all the key APIs
across both the Linux and UNIX system interfaces with clear descriptions
and tutorial examples and stresses the importance and benefits of following
standards such as the Single UNIX Specification and POSIX 1003.1.”
—ANDREW JOSEY, DIRECTOR, STANDARDS, THE OPEN GROUP, AND CHAIR OF
THE POSIX 1003.1 WORKING GROUP
“What could be better than an encyclopedic reference to the Linux system,
from the standpoint of the system programmer, written by none other than
the maintainer of the man pages himself? The Linux Programming Interface is
comprehensive and detailed. I firmly expect it to become an indispensable
addition to my programming bookshelf.”
—BILL GALLMEISTER, AUTHOR OF POSIX.4 Programmer’s Guide: Programming for
the Real World
“Kerrisk’s book is more thorough, more comprehensive and just as well
written as the Stevens and Rochkind books. . . . It is now easily THE standard
book on Linux/UNIX system programming.”
—VLADIMIR G. IVANOVIC, SOFTWARE PRODUCT MANAGEMENT
“. . . the most complete and up-to-date book about Linux and UNIX system
programming. If you’re new to Linux system programming, if you’re a UNIX
veteran focused on portability while interested in learning the Linux way, or
if you’re simply looking for an excellent reference about the Linux program-
ming interface, then Michael Kerrisk’s book is definitely the companion you
want on your bookshelf.”
—LOÏC DOMAIGNÉ, CHIEF SOFTWARE ARCHITECT (EMBEDDED), CORPULS.COM
“Michael Kerrisk has not only written a great book about Linux programming
and how it relates to various standards, but has also taken care that bugs he
noticed got fixed and the man pages were (greatly) improved. In all three
ways, he has made Linux programming easier. The in-depth treatment of
topics in The Linux Programming Interface . . . makes it a must-have reference
for both new and experienced Linux programmers.”
—ANDREAS JAEGER, PROGRAM MANAGER, OPENSUSE, NOVELL
“An outstanding book in the style of [the] beloved classic Advanced Program-
ming in the UNIX Environment. . . . The treatment of each system call is very
thorough, indicating deviations from the Single UNIX Specification and
comparisons with various Unix flavors.”
—JOHN WIERSBA, LINUX/UNIX PROGRAMMER, THOMSON REUTERS
“It’s a wonderful book . . . well written and never boring . . . the author has
achieved the right balance in his descriptions and is able to explain clearly
ahuge variety of topics.”
—DR. JOHN GRAHAM-CUMMING, PROGRAMMER AND AUTHOR OF The Geek Atlas
“I’ve been very impressed by the precision, the quality and the level of detail
Michael Kerrisk put in his book. He is a great expert of Linux system calls and
lets us share his knowledge and understanding of the Linux APIs.”
—CHRISTOPHE BLAESS, AUTHOR OF Programmation système en C sous Linux
“[The Linux Programming Interface] will no doubt become the standard guide
for Linux system programming.”
—PEDRO PINTO, LEAD SOFTWARE INFRASTRUCTURE ARCHITECT, BLUE
CAPITALGROUP
San Francisco
THE LINUX PROGRAMMING INTERFACE. Copyright © 2010 by Michael Kerrisk.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or
mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior
written permission of the copyright owner and the publisher.
ISBN-10:1-59327-220-0
ISBN-13:978-1-59327-220-3
Publisher:WilliamPollock
ProductionEditor:RileyHoffman
CoverDesign:OctopodStudios
FrontCoverPhoto:RobSuisted
BackCoverPhoto:LynleyCook
Copyeditor:MarilynSmith
Compositor:SusanGlinertStevens
Proofreader:LindaSeifert
Fortechnicalreviewers,pleaserefertotheauthor’sacknowledgements
Forinformationondistribution,translations,orbulksales,pleasecontactNoStarchPress,Inc.directly:
NoStarchPress,Inc.
2458thStreet,SanFrancisco,CA94103
phone:1.415.863.9900;info@nostarch.com;www.nostarch.com
LibraryofCongressCataloging-in-PublicationData
Kerrisk, Michael, 1961-
The Linux programming interface : a Linux and UNIX system programming handbook / by Michael Kerrisk.
p. cm.
Includes bibliographical references and index.
ISBN-13: 978-1-59327-220-3 (hardcover)
ISBN-10: 1-59327-220-0 (hardcover)
1. Linux. 2. UNIX (Computer file) 3. Operating systems (Computers) I. Title.
QA76.76.O63K496 2010
005.4'32--dc22
2010001947
NoStarchPressandtheNoStarchPresslogoareregisteredtrademarksofNoStarchPress,Inc.Otherproductand
companynamesmentionedhereinmaybethetrademarksoftheirrespectiveowners.Ratherthanuseatrademark
symbolwitheveryoccurrenceofatrademarkedname,weareusingthenamesonlyinaneditorialfashionandtothe
benefitofthetrademarkowner,withnointentionofinfringementofthetrademark.
UNIX®isaregisteredtrademarkofTheOpenGroup.
Theinformationinthisbookisdistributedonan“AsIs”basis,withoutwarranty.Whileeveryprecautionhasbeen
takeninthepreparationofthiswork,neithertheauthornorNoStarchPress,Inc.shallhaveanyliabilitytoany
personorentitywithrespecttoanylossordamagecausedorallegedtobecauseddirectlyorindirectlybythe
informationcontainedinit.
For Cecilia, who lights up my world.
B R I E F C O N T E N T S
Preface ...................................................................................................................xxxiii
Chapter 1: History and Standards.................................................................................... 1
Chapter 2: Fundamental Concepts................................................................................. 21
Chapter 3: System Programming Concepts...................................................................... 43
Chapter 4: File I/O: The Universal I/O Model................................................................. 69
Chapter 5: File I/O: Further Details................................................................................ 89
Chapter 6: Processes.................................................................................................. 113
Chapter 7: Memory Allocation..................................................................................... 139
Chapter 8: Users and Groups...................................................................................... 153
Chapter 9: Process Credentials.................................................................................... 167
Chapter 10: Time....................................................................................................... 185
Chapter 11: System Limits and Options......................................................................... 211
Chapter 12: System and Process Information................................................................. 223
Chapter 13: File I/O Buffering..................................................................................... 233
Chapter 14: File Systems............................................................................................ 251
Chapter 15: File Attributes.......................................................................................... 279
Chapter 16: Extended Attributes.................................................................................. 311
Chapter 17: Access Control Lists.................................................................................. 319
Chapter 18: Directories and Links................................................................................ 339
Chapter 19: Monitoring File Events.............................................................................. 375