The Linux Programming Interface
Michael Kerrisk
Copyright 2011
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.
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
UNIX is a registered trademark of The Open Group.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
This logo applies only to the text stock.
No Starch Press
Dedication
For Cecilia, who lights up my world .
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
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 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
Michaels 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
... a very thoroughyet easy to readexplanation of UNIX system and network programming, with an emphasis on Linux systems. Its certainly a book Id recommend to anybody wanting to get into UNIX programming (in general) or to experienced UNIX programmers wanting to know whats new in the popular GNU/Linux system.
Fernando Gont, Network Security Researcher, IETF participant, and RFC author
... encyclopedic in the breadth and depth of its coverage, and textbook-like in its wealth of worked examples and exercises. Each topic is clearly and comprehensively covered, from theory to hands-on working code. Professionals, students, educators, this is the Linux/UNIX reference that you have been waiting for.
Anthony Robins, Associate Professor of Computer Science, The University of Otago
Ive 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 systme en C sous Linux
... 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 Programmers Guide: Programming for the Real World
... the most complete and up-to-date book about Linux and UNIX system programming. If youre new to Linux system programming, if youre a UNIX veteran focused on portability while interested in learning the Linux way, or if youre simply looking for an excellent reference about the Linux programming interface, then Michael Kerrisks book is definitely the companion you want on your bookshelf.
Loc Domaign, Chief Software Architect (Embedded), corpuls.com
Preface
Subject
In this book, I describe the Linux programming interfacethe system calls, library functions, and other low-level interfaces provided by Linux, a free implementation of the UNIX operating system. These interfaces are used, directly or indirectly, by every program that runs on Linux. They allow applications to perform tasks such as file I/O, creating and deleting files and directories, creating new processes, executing programs, setting timers, communicating between processes and threads on the same computer, and communicating between processes residing on different computers connected via a network. This set of low-level interfaces is sometimes also known as the system programming interface.
Although I focus on Linux, I give careful attention to standards and portability issues, and clearly distinguish the discussion of Linux-specific details from the discussion of features that are common to most UNIX implementations and standardized by POSIX and the Single UNIX Specification. Thus, this book also provides a comprehensive description of the UNIX/POSIX programming interface and can be used by programmers writing applications targeted at other UNIX systems or intended to be portable across multiple systems.
Intended audience
This book is aimed primarily at the following audience:
programmers and software designers building applications for Linux, other UNIX systems, or other POSIX-conformant systems;
programmers porting applications between Linux and other UNIX implementations or between Linux and other operating systems;
instructors and advanced students teaching or learning Linux or UNIX system programming; and
system managers and power users wishing to gain a greater understanding of the Linux/UNIX programming interface and of how various pieces of system software are implemented.
I assume you have some prior programming experience, but no previous system programming experience is required. I also assume you have a reading knowledge of the C programming language, and know how to use the shell and common Linux or UNIX commands. If you are new to Linux or UNIX, you will find it helpful to read the programmer-oriented review of fundamental concepts of Linux and UNIX systems in .