Drink Beer and Geek Out

What is kernel_task? Why is it running?

If like me, you keep an eye on the Mac OSX activity monitor, then you’ve likely seen kernel_task and wonder what the heck it is. Well, we’re going to answer that today.

kernel_task01

A bit of background

Mach is one of the more successful stories in microkernel development, Mac OS X is based around the Mach microkernel, as well as the NeXTStep of old, and even the DEC’s OSF/1, and IBM’s frequently deceased OS/2 (at least for RS6000 machines). In accordance with the basic concept of a microkernel, Mach manages memory, and handles inter-process communication but not much else.

Understanding kernel_task

Kernel_task is not a “task” per se, but more….a representation of the microkernel itself. When Dave Zarzycki created launchd(8) for Mac OS X there was a culture of “reducing the daemon count”, so in order to get his launchd(8) project into the base operating system, he incorporating the functionality of init(8) which on a classical Unix (think FreeBSD) operating system, is more or less responsible for boot strapping the userland processes, and starting all the background daemons, as well as the ttys(5) that are needed to console access, etc. Being the first userland process, launchd(8) starts with PID 1, which means it’s the userland process to end all userland processes ;) . If you’ll notice in Activity Monitor, kernel_task is PID 0, which most likely means that it’s not userland related at all.

A little more undertandng of kernel_task…

Documentation on kernel_task is sparse at best, but it is the basic (virtual/)memory manager for Mac OS X on top of Mach. This is similar to the sigma0 concept with the L4 microkernel. There needs to be some underlying task on top of any microkernel to dole out resources, like pages of memory, to the userland processes. Unfortunately. kernel_task is responsible for handling the allocation of pages of memory in the Xnu kernel (Mach+IOKit+whatever else Apple threw in), which would explain why it has an amazing amount of threads. There is however another process that shows up, the dynamic_pager which handles the swap file for Mac OS X. Of course, kernel_task and dynamic_pager work together of a Mach port, and when necessary, kernel_task runs out of available resources, it serves up memory from a swap file managed by dynamic_pager.

There are lots of cool underlying daemons and processes that handle a lot of the lower level things in Mac OS X, because in a conventional microkernel design, all that nonsense should be handled in userland. One of my personal favourites is blued(1) the bluetooth daemon, kextd(8) which manages the loading and unloading of kernel extentions (kexts they call them, man those Apple engineers are clever :-P ). Other fun processes include coreaudiod(8), netinfod(8), and configd(8).

Leave a Reply