daper's blog

Using dd as a Swiss Army knife

Here are some useful examples of how a programmer (but not only) can use the dd command as a Swiss Army knife. Many of us are used to use a command similar to this one:

  1. dd if=/dev/sda of=sda.img bs=1k

This is a faster version of the cat command because of the bs= option. But dd can do much more, even for a programmer. Here are some examples of dd usage that I find useful as a debugging aid:

Threads and fork(): think twice before mixing them.

When debugging a program I came across a bug that was caused by using fork(2) in a multi-threaded program. I thought it's worth to write some words about mixing POSIX threads with fork(2) because there are non-obvious problems when doing that.

Using Valgrind to debug memory leaks

Valgrind is a wonderful tool useful mainly to debug memory related problems in C/C++ programs. I don't know a better tool to find memory leaks. Although output of this program is often clear and intuitive it's worth to spend some time to get deeper knowledge of how Valgrind works, what exactly its messages mean and what are the problematic cases when tracing a memory leak is harder even with Valgrind.

All about Linux signals

In most cases if you want to handle a signal in your application you write a simple signal handler like:

void handler (int sig)

and use the signal(2) system function to run it when a signal is delivered to the process. This is the simplest case, but signals are more interesting than that! Information contained in this article is useful for example when you are writing a daemon and must handle interrupting your program properly without interrupting the current operation or the whole program.

C++ exception specifications are evil

When I began to program in C++ I was already experienced in C and had written some code in Java. After writing few thousands lines of code in this language I felt comfortable with all of it's goodies like templates, inheritance, operator overloading and (I thought so) exceptions. But I made at least one mistake: I used exception specifications. My intention was to use them as in Java: tell the compiler that this method throws specific exceptions so it can use this information to optimize code and tell me when I'm throwing/catching wrong exceptions. A great surprise was that in practice in most cases writing exception specifications slows the code and introduces bugs!

Pipe in bash can be a trap!

Today a colleague at work tried to debug a script in bash that didn't want to work as he expected. He hit one of traps people get into when writing bash scripts. Let's look at the code that find the largest message that was sent from this computer:

LZMA - better than bzip2

Everyone sometimes needs to compress something: doing backup, sending files over Internet etc. Most of us uses gzip or bzip2. It's known than bzip2 has a bit better compression ratio but it's much slower. I'm one of that people who only used those two programs, often with conjunction with tar using

tar -czf
or
tar -cjf
and wasn't think I need something better. But one day I wandered if exists something else, some alternative and found LZMA.

C++ object's reference counting

Last update: 13.01.2009

Passing objects to methods by pointer or reference is very handy if you want to boost performance by avoiding invoking the copy constructor. Sometimes it's the only way to pass an object because it doesn't have a copy constructor or assignment operator defined. An example would be an object that has an open operating system resource that can't be safely shared.

Let's say that you are creating a class that does some low-level operation on a file. It contains a file descriptor as a field:

  1. class LowLevelFile
  2. {
  3. int fd
  4.  
  5. // [...]
  6. };

Syndicate content