writing

Mac OS X, It’s Not Linux, but It’s Close

Posted on August 11, 2010 at 9:43 am

First published in Linux Journal

In the past, the Mac OS was a fairly unique entity, not having much in common with other OSes, such as Windows or UNIX, which made cross-platform work a bit convoluted. However, the advent of the latest incarnation of the Mac OS, called OS X or Darwin, provides a very comfortable alternative for Linux geeks. Because Darwin is based on BSD UNIX, it is possible to use POSIX-compliant applications on the Mac.

Apple provides a package called Xcode on its developer site. Xcode has the necessary tools for compiling programs on the Mac, and it includes a nice graphical IDE and lots of examples for developing applications for OS X. Xcode is based on the GNU toolset, providing tools like gcc, libtool, make and so on. That means, with Xcode, most command-line applications can be compiled and run on the Mac. So, a simple little hello world program:


#include <stdio.h>
#include <stdlib.h>
int main (int argc, char **argv) {
printf("Hello World\n");
}

compiles fine with gcc, giving you an executable that prints out “Hello World” on the command line. Basically, anything that is POSIX-compliant should compile and run with no issues.

Getting graphical programs to run can be a bit more involved. Mac OS X does provide an X server and all the standard development libraries you would need for a pure X11 application, like Xlib. However, none of the other standard libraries, like GTK or Qt, are available by default. You have to download, compile and install them yourself, which works fairly well, but you have to choose the correct configuration options and collect all the required dependencies. But, you shouldn’t need to go through so much pain. Two projects in active development provide some form of package management for GNU software: Fink and MacPorts. Using these, getting and installing GNU software is as easy to do as it is with most Linux distros.

The Fink Project started in 2001 and is based on the Debian package management system, so you can use the Debian package tools like dpkg, dselect and apt-get, making it familiar for Debian-based distro users. Once the base installation is done, you can start to install packages. If you like a text-based manager, use dselect (Figure 1). If you prefer a graphical manager instead, use the following command to get synaptic (Figure 2):

sudo apt-get install synaptic

Using these applications, you can install many of the packages you are familiar with in Linux. The package count, at the time of this writing, is 10,872.


Figure 1


Figure 2. synaptic Package Manager

However, not all packages are available as a binary install using these tools. For that class of packages, Fink installs them directly from source, compiling and installing on your Mac. So, for example, if you want to install gramps and do some genealogy work, execute the following:

sudo fink install gramps

Even installing from source, Fink deals well with dependency issues, because it still is based on the Debian package management system.

The MacPorts Project started in 2002 and models itself after the BSD port packaging system. Thus, you use the command to manage the packages on your system. Once you have done the base install, you can install other software packages simply by running the command:

sudo port install stellarium

Several graphical interfaces are available as well, such as Porticus. However, those typically are independent projects, as opposed to the Debian tools available in Fink. As such, their development cycle and behavior tend to be a bit more erratic and unstable than the older and more mature Debian tools. But still, they may be exactly what you’re looking for if you prefer a graphical interface. Like the Fink Project, both binary packages and source packages are available. There are 5,829 packages available in the MacPorts Project.

Both projects provide access to the full wealth of open-source applications that has been available to Linux users, and the number of packages provided by both projects continues to grow.

Once you have one, or both, of these projects installed (they will coexist on your system), you will have all the tools necessary to do your own code development. I have used anjuta (Figure 3) on my MacBook to develop some small GNOME applications. These compile and run equally well on my MacBook and my Netbook running Ubuntu. Although there isn’t binary compatibility between OS X and Linux, with source compatibility, it is (at least in theory) simply a matter of recompiling for the other system.


Figure 3. Anjuta IDE

Running Mac OS X code on Linux is not as easy as running Linux code on Mac OS X. The real stumbling block is the graphical interface called Quartz on the Mac OS. Although the kernel and most of the command-line tools have been released as open-source software, Quartz still is closed. At the time of this writing, I could not find any references to a reverse-engineered, open-source replacement for Quartz. So the only option available is running OS X inside a virtual machine. Although this is not technically running Mac applications on Linux, it does provide the ability to run OS X on a Linux box.

*Apple Developer Connection: developer.apple.com
*Open-Source Apple: www.opensource.apple.com
*Fink Project: www.finkproject.org
*MacPorts Project: www.macports.org

at, batch and cron

Posted on June 17, 2010 at 10:00 pm

This was first published in Linux Journal, issue 184.

at,batch,cron – The ABC’s of doing work when nobody’s home

People have always been interested in doing more work with less effort. This drive kind of reaches its peak when work is being done, even though you aren’t actually doing anything. With Linux, you can effectively do this with the trio of programs at, batch and cron. So now your computer can be busy getting productive work done, even long after you’ve gone home. Most people have heard of cron, fewer people have heard of at, and even fewer have heard of batch. Here you’ll find out what they can do for you, and the most common options to get the most out of them.

at is actually a collection of utilities. The basic idea is that you can create queues of jobs to run on your machine at specified times. The time at runs your job is specified on the command line, and almost every time format known to man is accepted. The usual formats, like HH:MM or MM/DD/YY, are supported. The standard POSIX time format of [[CC]YY]MMDDhhmm[.SS] is also supported. You can even use words for special times, like now, noon, midnight, teatime, today or tomorrow, among others. You can also do relative dates and times. For example, you could tell at to run your job at 7PM in 3 days time by using “7PM + 3 days”.

at will listen to the standard input for the commands to run, which you finish off with a ctrl-D. You can also place all of the commands to run in a text file and tell at where to find it by using the command line option “-f filename”. at will use the current directory at the point of invocation as the working directory.

By default, at will dump all of your jobs into one queue named “a”. But you don’t need to stay in that one little bucket. You can group your jobs into a number of queues quite easily. All you need to do is add the option “-q x” to the at command, where “x” is a letter. This means that you can group your jobs into 52 queues (a-z and A-Z). This lets you use some organization in managing all of this after hours work. Queues with higher letters will run with a higher niceness. The special queue “=” is reserved for jobs currently running.

So once you’ve submitted a bunch of jobs, how do you manage them? The command atq will print out the list of your upcoming jobs. The output of the list is: job ID, date, hour, queue and username. If you’ve broken up your jobs into multiple queues, you can get the list of each queue individually by using the option “-q x” again. If you change your mind, you can delete a job from the queue by using the command “atrm x”, where x is the job ID.

Now, what happens if you don’t want to overload your box? Using at, your scheduled job will run at the assigned time regardless of what else may be happening. Ideally, you would want your scheduled jobs to run only when they won’t interfere with other work. This is where the command batch comes in. batch behaves the same way at does, but will only run the job once the system load drops below a certain value (usually 1.5). You can change this value when atd starts up. By using the command line option “-l xx”, you can tell batch not to run unless the load is below the value “xx”. Also, batch defaults to putting your jobs into the queue “b”.

These tools are great for single runs of jobs, but what happens if you have a recurring job which needs to run on some sort of schedule? This is where our last command, cron, comes in. As a user, you actually don’t run cron. You instead run the command crontab, which lets you edit the list of jobs that cron will run for you. Your crontab entries are lines containing a time specification, and a command to execute. For example, you might have a backup program running at 1AM each evening:

0 1 * * * backup_prog

cron will accept a wide variety of time specifications. The fields available for your crontab entries include

field allowed values
minute 0-59
hour 0-23
day of month 1-31
month 1-12
day of week 0-7

Using these fields and values, we can use them directly, use groups of values separated by commas, use ranges of values, or use an asterix to represent any value. You can also use special values

string meaning
@reboot run once, at startup
@yearly run once a year (0 0 1 1 *)
@annually same as @yearly
@monthly run once a month (0 0 1 * *)
@weekly run once a week (0 0 * * 0)
@daily run once a day (0 0 * * *)
@midnight same as @daily
@hourly run once an hour (0 * * * *)

Now that you have these three utilities under your belt, you can schedule those backups to run automatically, or get a long compile to start after you’ve gone home, or get your machine to keep using up any idle cycles. So go out and get lots of work done, even when nobody is home.

No book deal

Posted on June 16, 2010 at 9:46 pm

I just got a call from an editor with O’Reilly. I suggested a book idea. Unfortunately, they don’t think that there is a big enough market to make the cost of a production run worth it. And I have to agree with them. But I still think that this is an idea that is needed. So this is what I’m proposing. I’m going to start writing up the chapters and posting them here. After that, I think I’ll just go ahead and publish it myself through Lulu.com. We’ll see if people actually find it and get any use out of these postings.

Two new columns

Posted on March 8, 2010 at 5:06 pm

Well, I’ve had several ideas mulling around in my head, and I think they’ve reached the point where I’m ready to start actually working on them. I’ve had two ideas for regular articles: a “What if?” column, and a column of “Maple for Mythbusters”. The first column will be articles where I look at different ideas and say “What if this idea were real? What would it mean?” The first one is going to look at homeopathy, and make the assumption that it is real. Starting there, what else do we have to accept, by logic, due to accepting that homeopathy is real. I’m going to look at a different idea for each article. The second column is going to look at different experiments that the Mythbusters have done and see how we can use Maple to do some scientific computations around those experiments. I really like Maple, and I think more people should be supporting a Canadian made product.

These two columns are going to run on alternate weeks, starting with “What if?” this Friday. If you have any ideas for either column, things you’d like to see covered, please let me know.

Losing my chops

Posted on August 28, 2009 at 11:33 am

I’ve just realized that I’m starting to lose my chops for both programming and physics. Just like anything else, you need to practice to keep yourself up to snuff. So, I’m going to start working on my chops. There are going to be a series of pages that I’m going to start up this weekend. I’m thinking of a series of pages on Mythbusters for the science, and a series of pages on scientific computing. We’ll see if I can keep this up and actually get my edge back.

Writing…

Posted on August 24, 2009 at 11:08 am

Well, I seem to be finding a really satisfying hobby, writing. I’ve worked out a mini-column deal with Linux Journal where I write a ~1000 word column for their UpFront section each month. It is really fun, since I get to explore really obtuse areas of the Linux command line. I’m posting it on the “Published Articles” page.

I’ve got ideas for columns for other magazines that I will be pitching. Right now I have more ideas pouring out of me then I can write down. I also have an idea for a book for atheist Druidry. I’m finding more and more people who are interested in it, but there are no real resources out there. I will probably post the introduction up here when it’s done, and I’m going to be pitching the idea to some publishers out there. Who knows, maybe I’ll actually be a formally published author.

Top