Programming and writing about it.

echo $RANDOM

Tag: Unix

Article: Getting Started with Inotify

Update: The PDF is now available.

It’s always fun to peek into one of the umpteen features of Linux. In the April, 2011 issue of Linux For You I take a hands-on look at Inotify.

The source code for this article is available at https://bitbucket.org/amitksaha/articles_code/

 

Building a Ubuntu DEB package for a Python module

Here are some links, and some #ubuntu-motu logs that can help you create a DEB package for a pure-Python module and also upload it to your PPA.

The definitive starting point is https://wiki.ubuntu.com/PackagingGuide/Python. This guide is pretty much almost all what you need if you are looking to create a DEB package for a Python application or a Python module. Please follow the instructions given there carefully.

The above guide doesn’t tell you the things that you also need to do if you are looking to upload your package to your own PPA on launchpad, like signing the package, creating only source packages, writing a good copyright file, etc. So, I turned to the amazing folks in #ubuntu-motu

  • Use debuild -sa -S to build source packages which are also signed with your GPG key. You WILL need to have signed packages before you can upload it to your PPA. The advantage of using ‘debuild’ over directly using ‘dpkg-buildpackage‘ is that it also uses lintian to check your package for possible policy violations.
  • A good way to write the debian/copyright file is here at http://svn.debian.org/wsvn/dep/web/deps/dep5.mdwn?op=file&rev=135
  • Before uploading it to the PPA, you should check if your package has been properly constructed by setting up a pbuilder environment

That’s it. You should now upload the source package to the PPA using the instructions given.

A primer on Ext4

Now that almost all distributions offer ext4 as the default file system, it’s high time we looked at the most compelling feature set of the file system and understood how to migrate from its predecessor. Now that almost all distributionsoffer ext4 as the default file system,it’s high time we looked at the most compelling feature set of the filesystem and understood how to migrate from its predecessor.

The January 2010 issue of Linux For You has published my article on Ext4 which, as the name suggests is a primer on Ext4 focusing on the geek user. If you happen to have a copy lying by, let me know how you find it!

Article series: Getting started with GNU Octave

With the December, 2009 issue of Linux For You I have started an article series on GNU Octave.  This article marks my foray into writing about scientific computing tools and also a article series.

(The PDFs given here are pre-print PDFs and have been uploaded here with prior permission from the Linux For You team; if you are interested in republishing it in any form, please contact me)

Table of Contents:

  1. Getting Started with GNU Octave, Part -1 : December, 2009 issue of Linux For You (PDF)
  2. Getting Started with GNU Octave, Part -2: January, 2010 issue of Linux For You (PDF)
  3. Getting Started with GNU Octave, Part -3: February, 2010 issue of Linux For You (PDF)
  4. Getting Started with GNU Octave, Part -4: April, 2010 issue of Linux For You (PDF coming up)
  5. Getting Started with GNU Octave, Part -5: May, 2010 issue of Linux For You (PDF coming up)
  6. Getting Started with GNU Octave, Part -6: June, 2010 issue of Linux For You (PDF coming up)

Quick Tip: Piping to gnuplot from C

Update, 21/10/09 :Thanks to A.K’s comment, Its “gnuplot” and not “GNU plot”

Couple of things first up:

  • gnuplot supports piping, So, echo "plot sin(x)" | gnuplot will plot the sin(x) function.
  • However, the plot disappears even before you could see it. For that echo "plot sin(x)" | gnuplot -persist , is useful. It persists the GNU plot main window

The usefulness of the second point is that, if you have a “pipe descriptor” describing a pipe to the open gnuplot instance , you can plot more plots on the first plot, without opening a new gnuplot instance. We shall be using this idea in our code.

C (Cee Language)

</p>
<p>#include &lt;stdio.h&gt;<br />
#define GNUPLOT &quot;gnuplot -persist&quot;</p>
<p>int main(int argc, char **argv)<br />
{<br />
        FILE *gp;<br />
        gp = popen(GNUPLOT,&quot;w&quot;); /* 'gp' is the pipe descriptor */<br />
        if (gp==NULL)<br />
           {<br />
             printf(&quot;Error opening pipe to GNU plot. Check if you have it! \n&quot;);<br />
             exit(0);<br />
           }</p>
<p>        fprintf(gp, &quot;set samples 2000\n&quot;);<br />
        fprintf(gp, &quot;plot abs(sin(x))\n&quot;);<br />
        fprintf(gp, &quot;rep abs(cos(x))\n&quot;);<br />
        fclose(gp);</p>
<p>return 0;<br />
}<br />

The above code will produce a comparative plot of absolute value of sin(x) and cos(x) on the same plot.  The popen function call is documented here. This code/idea should work on GCC and Linux and any other language and OS that supports piping.

Utility: If you have a application which is continuously generating some data, which you will finally plot, then you can plot the data for every new set of data- that gives a nice visualization about how the data is changing with the iterations of your application. This is a perfect way to demonstrate convergence to the best solutions in Evolutionary Algorithms, such as Genetic Algorithms.

Links:

  1. Piping
  2. gnuplot Homepage

fileserveHTTP.ss: Serve your local files over HTTP

The GNU GPL source of the first release, Version 0.8  is available at http://bitbucket.org/amitksaha/foobar-scripts/src/tip/fileserveHTTP.ss

Background:

I accidentally came across the SimpleHTTPServer.py Python module, while back and immediately found it a very useful utility script. As part of my Scheme learning exercise, I implemented the same (AFAIK, complete) functionality in the Scheme language using PLT Scheme.

More: Systems Programming with PLT Scheme helped me get started with a multi-threaded server (till Step 6). From then on, I tried to understand what the code in the Python module does, and tried to implement the functionality in the Scheme code.

High Level Execution flow

The code execution starts from (serve) procedure which creates the server process and hence is ready for accepting new client connections. A new client connection is handled in a new thread. For the first GET request, the list of current sub-directories and files in the directory from which the server was started is returned (in a HTML page) to the client. This is performed by the (doGet-list) procedure. After the first request, the new GET requests are served by the (doGet-file) procedure.

Limitations:

  • I have used a lot of plt-scheme APIs, so this code will work only with plt-scheme
  • I have developed this code on Ubuntu Linux and that is the only OS, on which I know it works. Specifically, I would like to test the path name handling and MIME type detection on other Unix and may be Windows too
  • The code doesn’t do any exception handling as of now
  • This is my first proper Scheme program, so I might have written C/Java/Python in Scheme.

Usage instruction can be found in the source code itself. Please suggest comments/suggestions for improvements to amitsaha.in@gmail.com or post a comment here.

Tail Recursion Optimization, gcc, gdb

Debugging or even throwing optimized code in a debugger is a stupid idea, when you are trying to debug the code, that is. Not so lame, when you are trying to learn whether the optimization has actually taken place ;-). I wanted to check whether Tail Call Optimisation (better yet, Tail Call Elimination) has taken place on a certain piece of code. Here’s how:

Consider the following C implementation of the factorial function:

/* factorial-tail.c */

#include <stdio.h>
unsigned long long factorial(unsigned long long fact_so_far, unsigned long long count, unsigned long long max_count){
if (max_count==0 || max_count==1 || count >= max_count)
return fact_so_far;
else
{
//	printf("%llu  %p \n", count, &factorial); /*
return factorial(fact_so_far * count, ++count, max_count);
}
}

int main(int argc, char **argv)
{
unsigned long long n;
scanf("%llu", &n);
printf("\n Factorial %llu \n",factorial(1,0,n));
return 0;

}

Consider the function, factorial. As required by the definition of tail call recursion, the last executed statement is the recursive call to itself. No operation is pending in the caller, once the callee finishes its task.

Compile the code using ‘gcc’:

gcc -O2 -fno-inline -o factorial-tail factorial-tail.c

Now, fire up ‘gdb’ with the object code obtained above:

$ gdb ./factorial-tail
(gdb) b factorial
Breakpoint 1 at 0x8048426
(gdb) run
Starting program: /home/amit/Documents/Writings/cs-essays/fact-codes/factorial-tail
5
Breakpoint 1, 0x08048426 in factorial ()
Current language: auto; currently asm
(gdb) c
Continuing.
Factorial 120
Program exited normally.

As you can see the breakpoint was hit just once, whereas clearly if the code was executed unoptimized, it should have been called 5 times.

A note on ‘-fno-inline’ flag: Explicitly mentioing the ‘-fno-inline’ flag prevents any inlining of the fuctions due to the ‘-O2’ flag. On my gcc/gdb combination, without the flag, inlining was being done, due to which the breakpoint wasn’t hit even once.

Now, compile the same code, without the -O2 and -fno-inline flags:

gcc -o factorial-tail factorial-tail.c

Fire up ‘gdb’ as above:


$ gdb ./factorial-tail.
(gdb) b factorial
Breakpoint 1 at 0x80483f8
(gdb) run
Starting program: /home/amit/Documents/Writings/cs-essays/fact-codes/factorial-tail
5
Breakpoint 1, 0x080483f8 in factorial ()
Current language: auto; currently asm
(gdb) c
Continuing.
Breakpoint 1, 0x080483f8 in factorial ()
(gdb) c
Continuing.
Breakpoint 1, 0x080483f8 in factorial ()
(gdb) c
Continuing.
Breakpoint 1, 0x080483f8 in factorial ()
(gdb) c
Continuing.
Breakpoint 1, 0x080483f8 in factorial ()
(gdb) c
Continuing.
Breakpoint 1, 0x080483f8 in factorial ()
(gdb) c
Continuing.
Factorial 120
Program exited normally.
(gdb)

Point taken?

Thanks to folks here and here

Some other further readings:

If you see any errors, I stand corrected. If you want to share any thoughts/findings on something related, please leave a comment.