MacDevCenter    
 Published on MacDevCenter (http://www.macdevcenter.com/)
 See this if you're having trouble printing code examples


Mac Dev News Roundup

Unix for the Rest of Us

by Peter Fraterdeus
06/08/2001

By now, I'm sure you know that Mac OS X is built on top of BSD. But did you know that BSD is actually much older than its rival Linux and actually predates the personal computer? It's true.

How is it that this OS, billed by Steve Jobs as Apple's code base for the next decade, is built on a core that's decades old? To some, it might seem strange that such an important strategic building block depends on seemingly ancient technology.

The Unix foundation has been growing and adapting all through the last few decades, sprouting Sun's Solaris, IBM's AIX, and, of course, Linux, among other variations. In addition, Chairman Jobs used a Unix derivative on his NeXT boxen, back when a sleek, black magnesium cube was sitting on Tim Berners-Lee's desk in Geneva. It was on Berners-Lee's NeXT computer that the World Wide Web was born, and the next generation of Macintosh users will be raised on the same platform.

BSD or Berkeley Standard Distribution is a Unix workalike OS designed at University of California's Berkeley campus -- and was one of the first "open source" POSIX-compliant operating systems (before the Open Source movement had a name). The Computer Science department at Berkeley designed BSD to produce the same output, given the same input, as any other POSIX-compliant OS.

At the time, Unix was splintering into many dialects, each of which was tied to a particular hardware vendor. POSIX was an attempt by the federal government to assure that there would be a common set of features and commands across vendor hardware platforms -- that a program written for one hardware platform could be ported to any other hardware with some degree of confidence. The history of BSD is available on the web for those who are interested.

My concern, of course, is more immediate. My Titanium G4 PowerBook was designed to run this particular POSIX-compliant operating system. But it seems perfectly happy running command scripts that could have been written, in theory, 20 years ago for an VAX -- as well as displaying the gorgeous 21st-century Aqua interface which would have taken a supercomputer to render even five years ago!

The fundamental motivation behind Apple's Mach/BSD/Darwin strategy is to take advantage of the decades of development behind the Unix kernel architecture. Its stability and native network support of the core BSD services, and the millions of users/developers of POSIX software who have helped to develop and debug this OS, allows Apple to roll out a mature OS without having to spend years refining the product.

While Microsoft's Windows operating system may be more widely familiar due to its origins in cheap, numerous PCs and clones, Unix is ubiquitous in academic and scientific venues, and is widely used for its scalability in architecture, video, cinema, animation, and other areas where processing horsepower is traditionally more important than the user interface.

Comment on this articleWe're already seeing Java developers take interest in Mac OS X. Do you think the Open Source community will be interested as well?
Post your comments

Also in Mac Dev News Roundup:

Flying High with AirPort

Total OS X Preview

iPhoto Scripts and Plug-ins

Server farms offering highly parallel processing of images, mined data, web commerce, and so forth are far easier to implement and extend under Unix. Just buy more hardware, clone the hard drive, and plug into the network. Of course, the Open Source movement has grown enormously because of this. All you need to buy is the hardware -- no license fees!

Linux is the best-known open-source OS today, but BSD -- whether FreeBSD, OpenBSD, or Apple's Darwin -- is gaining visibility as well. In fact, thanks to Linus Torvald's recent outburst (he now disclaims it) about the Mach kernel in OS X, Darwin's visibility has risen substantially. (There's a reason they say there's no such thing as bad PR!) Darwin is Apple's name for its BSD OS layer. It's a perfectly useful OS on its own, but we know it mostly as the base upon which the Aqua interface runs.

Loyal Apple supporters are now faced with learning to love and understand an OS which is really as different from the original Mac OS as an 18-year-old is from a 7-year-old. It may be argued that only the name is the same. Certainly, the cursor flashing from the command line on the Terminal application has no analog under the Classic Mac OS. The cursor and the various daemons are great servants, as they say, but lousy masters. In future articles, I'll try to give you a sense of how to direct these servants, and to explore the services that they can provide.

The foundation of the foundation

Underneath the BSD layer (and its flashing Terminal cursor) is the Mach kernel. We won't have much to do with the kernel, although everything that happens on the computer has to deal with it. The kernel is the traffic cop -- it keeps all the various processes running smoothly. The kernel task is the first task started at startup and the last ended at shutdown.


[localhost:~] peterf% sudo kmodstat
Password:
Id Refs Address  Size  Wired  Name (Version) <Linked Against>
 1    1 0x0      0x0   0x0    com.apple.kernel (1.3.3)
 2    1 0x0      0x0   0x0    com.apple.kernel.bsd (1.0.3)
 3    9 0x0      0x0   0x0    com.apple.kernel.iokit (1.0.3)
 4    9 0x0      0x0   0x0    com.apple.kernel.libkern (1.0.3)
 5    3 0x0      0x0   0x0    com.apple.kernel.mach (1.0.3)

The first five modules dynamically link to the kernel. Some of the others include those shown here.

You get the idea that these kernel modules are providing very fundamental services, without which we wouldn't have access to CD/DVDs, AirPort, FireWire drives, or USB. (I'm impatiently awaiting a module for my Wacom tablet!)

I hope to provide you with more information about how the BSD layer in Mac OS works in future articles. For those with Unix or OpenStep backgrounds, it will be familiar territory. In fact, there are plenty of you out there with more in-depth knowledge about BSD than I have. I hope you'll feel free to correct me and enhance my knowledge as I try to bring an appreciation of this brave (new) world to Mac users of all levels.

Location, location, location

Our first recipe is simple but very useful.

Open your Terminal app (it's in /Applications/Utilities/) and type

locate '/Fonts/'

If locate returns nothing, it's because the "locate DB" script hasn't been initialized yet. If this is the case -- you've recently installed OS X, and the regular cron scripts haven't yet run -- type the following and let the machine churn for a few minutes. The ampersand at the end of the line puts the process in the background.

sudo /usr/libexec/locate.updatedb &

In the meantime, you can be reading the man page for the locate command by typing

man locate

When the updatedb script has run, try it again. Type

locate '/Fonts/'

You'll probably see a long list of file names flash past. Because the Terminal remembers quite a few lines, you can scroll back and see what's been located to match your request. If you'd like to see this one screen at a time, try

locate '/Fonts/' | less

which "pipes" the output of locate through the "text pager" called less (which, of course, is a program whose ancestor was called more as in "hit space for more").

The first page of the list will appear on the screen. Hit the spacebar for more. You can also hit Command-F for the next page (Forward), hit the down arrow to see the next line, hit Command-B for the previous page (Back), and so forth. The less command is good for a quick, read-only look at any text file on your system. Of course, there's more to the less command than this, but that's for another day.

For now, I'll leave you with a bit more on the locate function. Try this:

locate '/Sys*Fonts'

and then this

locate '/Sys*Fonts/'

and then this

locate '/Sys*Fonts*' | less

or maybe

locate '/Lib*Fonts*' | less

When you read the man page for locate, the synopsis looks like

SYNOPSIS
       locate pattern

A pattern is any sequence of characters, with certain special behaviors accorded to the "globbing" characters, as the man page says:

"Shell globbing and quoting characters ("*", "?", "\", "[" and "]") may be used in pattern, although they will have to be escaped from the shell. Preceding any character with a backslash ("\") eliminates any special meaning which it may have. The matching differs in that no characters must be matched explicitly, including slashes ("/")."

As a special case, a pattern containing no globbing characters ("foo") is matched as though it were "*foo*".

Briefly, these are patterns as they are used in a regular expression to create a "regex" that can match a set of different strings. For instance, in our example, /Sys*Fonts* means match any file path name which begins with the characters "/Sys" then has any number of intervening characters followed by Fonts followed by any number of characters out to the end of the line. Books are available (from O'Reilly, of course) on regular expressions. For an very formal presentation, try man re_format and man grep. While shell file name globbing doesn't use exactly the same syntax (in that it's much more limited than the full grep parser), there is plenty of power in the use of a well-placed asterisk or a set of brackets.

I hope this little dip of the toes into the BSD shell gives you a feeling for the depth that awaits you in the next few columns.

Peter Fraterdeus has been webmastering with Linux since 1993 and became hooked on the Mac after purchasing a 128k model in October 1984.


Read more Mac Dev News Roundup columns.

Return to the Mac DevCenter.

Copyright © 2009 O'Reilly Media, Inc.