Everything is Unix

Programming in a higher-level language, it's often easy to forget about using lower-level Unix facilities in tricky situations. Here are a few examples to give you an idea of what you might be missing.

Recently there has been some chatter on various programming blogs about how we should be using classic Unix features to build more scalable infrastructure. This all started when Ryan Tomayko wrote I like Unicorn because it’s Unix. The gist of that post was that Eric Wong’s Unicorn, an HTTP server written in Ruby, performed extremely well despite being written in Ruby because Eric wasn’t afraid to drop down to the lower-level Unix system calls instead of using the language’s traditional higher level abstractions.

Ryan does an excellent job of explaining exactly how following age-old Unix design patterns and using those system calls is the right way to go. He provided the code for a simple TCP “echo server” that can handle clients very efficiently. Not to be outdone, other popular scripting languages saw their advocates step forward with examples of doing the same thing. In Python is Unix, Jacob Kaplan-Moss provides a Python implementation of the same echo server. In Perl is Unix, Aristotle Pagaltzis presented a Perl implementation of a pre-forking echo server as well.

What you notice in each example is that the code is surprisingly readable and simple, letting the operating system to the really heavy lifting. In fact, they’re all very similar. Most of the real differences are syntactic sugar from the particular language. And that’s the whole point of these examples. Linux and Unix have some amazing built-in facilities for solving common problems and they’ve been around for a long time. But the reality is that many of the people coding in higher level languages like Ruby, Python, or Perl may not even be aware of them. Making matters worse, as Ryan points out, is a lack of documentation. Some of the high-level languages (Ruby in particular) do a poor job and really describing the low-level calls they expose or why you might use them. So if you don’t already have more than a basic understanding of Unix systems programming to fall back on, the odds are really not in your favor.

I won’t reiterate the benefits of the networking system calls that Ryan, Jacob, and Aristotle showcase in their examples. But I would like to consider a few other Unixisms that are ofter overlooked and can make some classes of problems easier to solve.

Multi-process with fork()

Ryan touches on this a bit in his post, but I’d like to draw a bit more attention to the power of the fork() system call. When you have a lot of work to do, using fork() to make one or more “clones” of your process so you can divide and conquer works quite well–especially on multi-core CPUs. Using waidpid(), the parent process has a reliable mechanism for waiting for all the workers to exit().

One of the main benefits of fork() is that child processes will inherit almost everything from their parent. That can be especially helpful if there’s a large amount of identical information that each worker needs to have fast access to. The parent can read that data in before forking and each child will also inherit a copy. But thanks to modern copy-on-write techniques, the child process ends up sharing the exact same copy that the parent had. So if the parent reads in 512MB of data and then forks 10 children, you don’t need 5GB of RAM to support the children. Unless they start modifying the data, memory bloat will not be an issue.

The best part of all is that this all completely automatic, on by default, and something you get for free. There’s not low-level programming required on your part to get these benefits. The operating system knows how to do these things and does them quite well.

Atomic File Operations and Locking

Often times when you have multiple process all trying to fetch data from the same pool or perform the same service, you can end up end up with a hard to recreate and debug race condition. You may end up with multiple process believe they have exclusive access to a given resource, duplicating each other’s work and potentially causing a myriad of problems that could be challenging to undo.

A classic Unix solution to this problem is to use an atomic file operation. The common choice is to use one of the atomic filesystem metadata operations, such as rename(). The basic operation is like this. If you have multiple processes all trying to get exclusive access to a resource, you can use a file to mediate that. Each process will try to create /tmp/data.lock and write its process id (PID) into it. The process that wins is then allowed to use the resource, removing the file when done. For added safety, processes should check to see if the current lock holder is alive. If not, they may treat the lock as stale and remove it.

But simply trying to create the file and write a PID into it is not atomic. You could check to see if the file exists, create it, and write to it. That’s the simple approach that sounds good on paper, but if multiple processes are trying to do that at once, you end up with a number of possible races. The traditional solution is for each process to create a uniquely named file such as /tmp/data.lock.$PID (though that can be improved too) and then use an atomic file operation that results in creating /tmp/data.lock. The two common choices for an atomic operation are link and rename() or link().

The rename() call is what the Unix command mv uses under the hood. It will change the file from its temporary name to the final name as long as that file doesn’t already exist. The link() call will try to create a new directory entry (a hard link) that references the same file. In either case, the underlying file (the inode) does not change, only the meta data in the file system does. And those changes are atomic.

This works well as long as you’re not using NFS. That’s a whole can of worms all its own.


Those are just a few examples of letting the OS doing some of your heavy lifting in sticky situations. If you’re been working in scripting languages for a while but haven’t spent much time looking at lower-level facilities like this, it might be worth making the time do do you. You might be surprised by how much you can learn and how much better your code could be. In addition to those described so far, I suggest learning more about signals and your your language of choice implements them. And if you have the time and inclination, I highly recommend a copy of Advanced Programming in the Unix Environment (second edition) by Richard Stevens. It’s truly a classic that provides rich examples of so many useful things that Unix can handle for you.

Have you found yourself simplifying code and making it more reliable by stepping back and letting the lower-level system calls do the hard work? Tell us about it in the comments.

Comments on "Everything is Unix"

Please pay a visit to the web sites we comply with, including this 1, because it represents our picks through the web.

Check beneath, are some totally unrelated web sites to ours, having said that, they may be most trustworthy sources that we use.

Here is an excellent Blog You may Discover Exciting that we encourage you to visit.

If you are intending for most excellent contents like I truly do, simply visit see
this website every single day because it gives feature contents, thanks

Also visit my blog … HaroldOShuga

Although web sites we backlink to beneath are considerably not related to ours, we feel they’re truly really worth a go by, so possess a look.

One of our visitors a short while ago suggested the following website.

Always a big fan of linking to bloggers that I love but really don’t get a lot of link really like from.

The details talked about in the article are some of the ideal readily available.

Here is a good Weblog You might Uncover Intriguing that we encourage you to visit.

Below you will obtain the link to some web sites that we think you must visit.

Here are some links to web-sites that we link to because we believe they’re really worth visiting.

Although sites we backlink to below are considerably not associated to ours, we really feel they are essentially worth a go by means of, so have a look.

Más Tarde, Siemens abre una filial en España con el nombre de Siemens Electromédica, S.A. y en el 1927 adquiere la firma Industria Latina de Electricidad Aplicada, S.A. (ILDEA) dedicada a la Electromedicina. En 1970, la Compañía cambia su nombre por el de Siemens, S. A., y cuatro años después 1974, bajo la misma denominación, se constituye una empresa única, como resultado de la unión de las sociedades que hasta entonces realizaban sus actividades como empresas Siemens. Siemens, S.A Madrid con las fábricas de Getafe y Cornellá , Siemens Electromédica Española, S.A. (Madrid). Este nuevo despliegue servirá para satisfacer a la oferta comercial de productos y servicios de Tercera Generación de telefonía móvil.

Llamenos al 902 929 916 y un técnico se desplazará el mismo día de su llamada a su domicilio a realizar su reparación, siempre con repuestos originales de la marca Daewoo. No lo dude, sea cual sea su averia nuestro Servicio tecnico Daewoo se encargara de solucionarla en un tiempo record, el mismo dia de su llamada al 902 929 916. Nuestra personal cualificado en la marca Daewoo, conseguirá arreglar su lavavajilla el mismo día de su llamada al 902 929 916 y siempre con repuestos originales de la marca Daewoo. Reparar su lavavajilla Daewoo en Granada es fácil siempre que su reparación sea realizada por técnicos certificados.

Check below, are some completely unrelated web sites to ours, nevertheless, they may be most trustworthy sources that we use.

We like to honor many other online sites on the net, even when they aren?t linked to us, by linking to them. Underneath are some webpages worth checking out.

Leave a Reply