Advanced Mac OS X Programming: The Big Nerd Ranch Guide
Mark Dalrymple
Copyright 2011 Big Nerd Ranch, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recoring, or likewise. For information regarding permissions, contact
Big Nerd Ranch, Inc.
154 Krog Street
Suite 100
Atlanta, GA 30307
(404) 478-9005
http://www.bignerdranch.com/
book-comments@bignerdranch.com
The 10-gallon hat with propeller logo is a trademark of Big Nerd Ranch, Inc.
Exclusive worldwide distribution of the English edition of this book by
Pearson Technology Group
800 East 96th Street
Indianapolis, IN 46240 USA
http://www.informit.com
The authors and publisher have taken care in writing and printing this book but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
Aperture, Apple, AppleShare, Aqua, Bonjour, Carbon, Cocoa, Cocoa Touch, Final Cut Pro, Finder, iChat, Instruments, Interface Builder, iOS, iTunes, Keychain, Leopard, Mac, Mac OS, MacBook, Objective-C, Quartz, QuickTime, Rosetta, Snow Leopard, Spotlight, Tiger, Time Machine, and Xcode are trademarks of Apple, Inc., registered in the U.S. and other countries.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
ISBN-13 9780321706546
ISBN-10 0321706544
Dedication
For Zoe. May she grow up to be as geeky as her Weird Uncle Bork.
Acknowledgments
This book is based upon the experiences teaching a five-day class at The Big Nerd Ranch called Advanced Mac OS X Bootcamp. The patience and curiousity of my students has made this a more complete and comprehensible introduction to the plumbing that makes Mac OS X a reliable, flexible, and high-performance system.
Special thanks go to Jeremy Sherman. Jeremy stepped up and taught one of the Advanced Bootcamps when I was unable to so. Along the way he made numerous improvements to this book and associated course materials, helping to modernize and robusticize the code. Jeremy is also responsible for the excellent under-the-hoods look at Blocks.
Incredible thanks go to Aaron Hillegass, my co-author on the first two editions of this book. Many moons ago Aaron took a chance on this random guy sitting in the back of a Cocoa class in Asheville. Over the years I've learned from Aaron more than I imagined possible about writing, teaching, and treating others well.
Susan Loper, the tireless editor, performed acts of magic on the purple prose she was given.
In preparing this book, many people reviewed the drafts and brought errors to our attention. The most astonishing quantity of corrections over the years came from Bill Monk. It would be difficult to overstate Bill's contributions. Other technical reviewers who submitted errors or offered suggestions: John Vink, Juan Pablo Claude, Carl-Johan Kihlborn, Mike Morton, Ajeya Vempati, Eric Peyton, Chris Ridd, Michael Simmons, David Southwick, Jeremy Wyld, Richard Wolf, Tom Van Lenten, Dave Zarzycki, James Spencer, Greg Miller, Anne Halsall, Roy Lovejoy, Jonathan Saggau, Jim Magee, and Rob Rix. They made this book better with their useful corrections and suggestions. Any errors that remain in this book are completely my fault.
I would also like to thank my wife, my Spousal Overunit Sharlotte DeVere, for her support, patience and understanding.
Foreword
In 1989, the band Living Colour released the song Glamour Boy which explained that the world was filled with glamour boys who valued appearance over other, deeper properties. And the song proclaims I ain't no glamour boy.
Many programmers are glamour boys. They lack a deep understanding of what is happening under the surface, so they are pleased simply to ship an application that the client doesn't hate. If they had a better comprehension of how the operating system does its work, their code would be faster, more reliable, and more secure.
If you are a Mac OS X or iOS programmer, the knowledge that separates really good coders from glamour boys is in this book. It demystifies the plumbing of Mac OS X. It helps you interpret mysterious messages from the compiler and debugger. When you finish reading this book, you will be a better programmer than when you started.
I met Mark Dalrymple shortly after Mac OS X shipped. I was astonished by his deep knowledge of Mac OS X's Unix core and how Apple had extended and enhanced it. I begged him to write the first edition of this book. The result was even better than I had hoped for.
If you read this third edition, you will master:
Don't be a glamour boy.
Aaron Hillegass |
Big Nerd Ranch |
July 1, 2011 |
Introduction
Mac OS X: Built to Evolve
Complex systems come into existence in only two ways: throughcareful planning or through evolution. An airport is an example ofsomething that is planned carefully beforehand, built, and thenundergoes only minor changes for the rest of its existence. Complexorganisms (like humans) are an example of something that has evolvedcontinually from something simple.In the end, organisms that are well suited to evolution will alwayswin out over organisms that are less suited to evolve.
An operating system evolves. Of course, the programmer who creates anew operating system designs it carefully, but in the end, anoperating system that is well suited to evolution will replace anoperating system that is not. It is, then, an interesting exercise tothink about what traits make an operating system capable of evolution.
The first version of Unix was developed by Ken Thompson at BellLaboratories in 1969. It was written in assembly language to run on aPDP-7. Dennis Ritchie, also at Bell Labs, invented the C programminglanguage. Among computer languages, C is pretty low level, but it isstill much more portable than assembly language. Together, Thompsonand Ritchie completely rewrote Unix in C. By 1978, Unix was runningon several different architectures. Portability, then, was the firstindication that Unix is well suited to evolution.