Eclectic

About programming and maybe more…

Archive for February, 2007

Valgrind and pthread_create – memory leak?

Posted by tread on February 26, 2007

Valgrind can be used for a number of things, but currently I’m interested in its memory leak checking ability.

Checking memory leaks with Valgrind

Compile your code (preferably with -g) and then execute it as follows:

valgrind –leak-check=full <program_name> <args>

(For more options, see the man page)

You will get something like this:


==17645== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 1)
==17645== malloc/free: in use at exit: 400 bytes in 1 blocks.
==17645== malloc/free: 1 allocs, 0 frees, 400 bytes allocated.
==17645== For counts of detected errors, rerun with: -v
==17645== searching for pointers to 1 not-freed blocks.
==17645== checked 58,860 bytes.
==17645==
==17645== 400 bytes in 1 blocks are definitely lost in loss record 1 of 1
==17645== at 0x4021396: malloc (vg_replace_malloc.c:149)
==17645== by 0x80483A5: one (test.c:6)
==17645== by 0x80483CC: main (test.c:12)
==17645==
==17645== LEAK SUMMARY:
==17645== definitely lost: 400 bytes in 1 blocks.
==17645== possibly lost: 0 bytes in 0 blocks.
==17645== still reachable: 0 bytes in 0 blocks.
==17645== suppressed: 0 bytes in 0 blocks.
==17645== Reachable blocks (those to which a pointer was found) are not shown.
==17645== To see them, rerun with: --show-reachable=yes

If you compiled with -g, you should even see the line number (test.c:6)

I then experimented with multi-threaded programs – valgrind shows the correct memory leak (i.e., my intentional leak multiplied by number of threads). However, I was surprised to see an additional leak message in the output:

==17698== 272 bytes in 2 blocks are possibly lost in loss record 1 of 1
==17698== at 0x40206D5: calloc (vg_replace_malloc.c:279)
==17698== by 0x400FA87: (within /lib/ld-2.4.so)
==17698== by 0x400FB4B: _dl_allocate_tls (in /lib/ld-2.4.so)
==17698== by 0x403AB68: pthread_create@@GLIBC_2.1 (in /lib/tls/i686/cmov/libpthread-2.4.so)
==17698== by 0x80486A7: main (test.c:43)

This leak value is constant, i.e., for 1 thread I get 136 bytes potentially lost. Initially I thought it was a memory leak in the pthread library – but a little googling revealed that it could be memory allocated for thread local storage. Since the function is _dl_allocate_tls, this seems fairly probable. Wish I knew for certain though – anyone care to comment?

Posted in Linux, Programming, Ubuntu | 7 Comments »

Expression evaluation – type conversions in C

Posted by tread on February 26, 2007

Arithmetic operations work only on the same types. When two types are dissimilar, type conversion takes place. This follows the following order:

1. (long double, anything) -> long double
2. (double, anything except 1.) -> double
3. (float, anything except 1. and 2.) -> float

i.e., if any of the operands is long double, the other is promoted to long double. Similarly for double, and so on.

Next, arithmetic operations do not happen on characters and short ints – these get promoted to ints thus:
unsigned char -> unsigned int
signed char -> int
unsigned short int ->unsigned int
short int -> int

After these integer promotions, we follow the following promotion rules:
4. (long int, unsigned int) -> long int or unsigned long int, depending on whether long int can hold all values of the unsigned int.
5. (long int, anything except 1, 2, 3, or 4) ->long int
6. (unsigned int, int) -> unsigned int
7. (int, int)

Basically we are going by size here, just in case you hadn’t observed that. Remember, this is done by the compiler – so you may not notice it .. until sudden side effects crop up. Be aware of what is happening when you write an arithmetic expression. For more details, see this.

Posted in Programming | Leave a Comment »

Converting flac files to ogg

Posted by tread on February 23, 2007

For some reason, whatever I saw on the net used bash – this has a lot of pitfalls, mainly spaces in names. Here is a better command:

find . -name "*flac" -exec oggenc -q 7 {} \;

-q 7 sets the quality, 10 being the highest.

Posted in Linux, Ubuntu | 1 Comment »

Use assert()

Posted by tread on February 21, 2007

Sadly, very few people do.

What it does is notify you if you reached some logically incorrect state -e.g., if a pointer is NULL, you definitely do not want to continue.

Steps:

include <assert.h>

assert (expression);

// if expression evaluates to false, then the program aborts

// expression can be anything, say (if ptr != NULL)

If the expression does evaluate to false, then you get a message like “assert failed in function foo at line x” – rather useful. Better sprinkle all your code with this – anyway, you can turn it off in production executables by adding

#define NDEBUG

in your code. An easier way – one that doesn’t require source modification – is to pass -DNDEBUG as a parameter when compiling the file.

e.g.,

gcc -c test.c -DNDEBUG

Posted in Programming | Leave a Comment »

HOWTO: use aptitude instead of synaptic and why.

Posted by tread on February 21, 2007

This post is an old article I once posted on ubuntu forums – I’m getting it here for my own archival.

What is aptitude?
Aptitude is a package manager for Debian and Debian based systems (similar to say, synaptic).

Why should you use aptitude over synaptic/apt-get?
The million dollar reason: it remembers dependencies downloaded to install a particular package and removes them automatically when you remove that package. Synaptic only gives you the history log, and apt-get not even that.
For example:

I install wmaker using synaptic, it installs libwraster along with it.
I purge wmaker out using synaptic, it leaves libwraster. I have to manually search for it and uninstall.

Now this may not seem a big deal, but imagine if wmaker had 20 dependencies.
On the other hand:

I install wmaker using aptitude, it installs libwraster with it.
I purge wmaker out using aptitude, it figures out libwraster isn’t needed anymore, and gets rid of that too.

Awesome, isn’t it? :)
But I held back from using this rather nifty tool because I didn’t want to learn how to use it. Now I can use it, at least to do simple tasks. So I thought I would share that knowledge.

aptitude can run in two modes:

  1. command line mode
    I will not write about this, other than to say the commands stay like apt-get .. which means you get aptitude power with apt-get commands.
    For eg.,
    sudo aptitude install wmaker
    sudo aptitude update
    sudo aptitude upgradeNote: to do a default upgrade, use the commandline above. Pressing U in the gui seems to do a smart upgrade, and that isn’t something I always want.
  2. gui mode
    You start this with: sudo aptitude
    f10 : pulls down the menu
    Note: it seems f10 pulls down the gnome-terminal menu. If you want to use f10, then you may need to turn off the gnome-terminal menu for the session. Alternately, just click on the aptitude menu, the mouse click works.
    q : quit
    u : updates package lists
    / : search for a package (firefox style)
    n : find next
    f : forget new packages, useful if you have a habit of peeking into new packages available after each update.Once you have found your package,
    enter: to see information about that package
    + : mark a package for install or upgrade
    - (minus) : mark a package for removal
    _ (underscore): mark a package for purging
    : : cancel any action on the selected packageU: to mark all upgradable packages (for upgrade :) )
    Once you have marked your changes,
    g : apply those changes.
    If you are in a screen which says displays package information, and you want to go back to the earlier screen, press f6 or q.

Once you press g, all the changes get listed. You can modify any of the changes, and then press g again to finally apply marked changes. I’ll keep adding to this HOWTO as I learn new things, and if you have any suggestions/tips/corrections please add to this thread .. I’ll update this post.

Posted in Ubuntu | 3 Comments »

Variable arguments in C

Posted by tread on February 20, 2007

Another area that I never really looked into is how to accept variable arguments in C. This time, the tutorial I am using is here.

What it boils down to is:
#include <stdargs.h> // include stdargs to use the macros va_*

int sum(int n, …) // n is the first argument, and denotes number of arguments
{
va_list args; // args here will store the list of arguments
int sum = 0, i = 0;
va_start (args, n); // initialize args – pass args and n to va_start
for (i = 0; i < n; i++) // loop n times – number of arguments
sum += va_arg (args, int); // va_arg () will return the argument – give it the initialized args and the type (int)
va_end (args); // end processing the argument list.
return sum;
}

That’s about it.
You can call this as
x = sum (5, 1, 2, 3, 4, 5) ;
or
x = sum (2, 100, 200);

Posted in Programming | 4 Comments »

Remote Procedure Calls on Linux

Posted by tread on February 20, 2007

RPCs have been around for quite some time, and I never have got around to reading up about them. Then I read this article by Jeff Bezanson (glibc on Linux) which seemed a good place to start.

Basics:

RPCs will execute your code on a remote machine without your needing to make network calls.

Client -> makes the call.

Server -> processes the call and executes whatever it is asked to do.

Passing pointers to data etc. is handled by XDR, which serializes the data before sending it across the network. Thus, even link lists can be sent across without your explicit intervention! This is done using an RPC compiler (rpcgen)

Steps:

1. Create a file file.x which contains a description of the data, and the procedures that will be called remotely. Program_id (decided arbitrarily) and version_number must be specified – program_id will be used to setup port numbers, and version_number is used to check that clients are communicating with a server that understands the language (in case of backward/forward incompatibility)

2. Invoke rpcgen on file.x – this results in file.h, file_svc.c, file_xdr.c, file_clnt.c

file.h: contains declarations, function prototypes in C – read this!

file_svc.c to be linked with the server, file_clnt.c with the client, and file_xdr.c with both.

3. For the server, function prototypes ending in _svc are what we need to implement.

(Note: handle quirky result value – functions return pointer to return value)

4. In the client main(), call clnt_create() with hostname, program_id and version_number.

5. Then call your rpc, note: result will be a pointer.

6. Link everything together, fire the server and then the client!

Questions as yet unanswered:

1. If the result is a pointer, does this mean that the memory location where the result is stored will get overwritten by multiple rpc calls?

2. What about permissions? How are they handled? Can user A call a server running under user B? If User B has sudo access on the server, but user A does not, what happens. Basically, how are permissions handled?

Of course, this is not really a detailed description – it’s only meant to jog my memory. For a proper tutorial, refer to the original article.

Posted in Programming | Leave a Comment »

Hello, World!

Posted by tread on February 20, 2007

Seems apt to have “Hello, World!” as the title to the first post – as I mentioned in my profile, I’m a software engineer who has realized he needs to keep studying something new everyday. So this blog will follow me around in my forays around assorted computer related topics – and will serve as a tool to jog my memory about what I read.

Posted in Uncategorized | 1 Comment »