macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Unix for the Rest of Us
Pages: 1, 2

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.