Custom Search

Wednesday, December 26, 2007

Checking if a process is alive or not.

In a multi-processing system - if we want to check if a given child process is alive or not (after forking it) for a longer period of time - we could send a signal 0 to the process using the kill API.


#include <sys/types.h>
#include <signal.h>

int kill(pid_t pid, int sig);


Use this API to send a signal of 0. If the errno is set to ESRCH - then the process had most probably exited.

Monday, December 24, 2007

Probability

Interesting article about probability here.

Friday, December 21, 2007

Want to trace function call stack on Linux

If you want to trace function call stack on a Linux box - then we can use backtrace family of functions on GNU/Linux.

For more details regarding the API and examples - refer here.

Wednesday, December 19, 2007

-fpic OR -fPIC

When preparing dynamic libraries on Linux, there is always a choice about selecting -fpic / -fPIC.


The difference lies mostly on RISC architectures. -fpic seems to take lesser number of instructions as compared to -fPIC .

For more details - refer to Section 2, in the document - "How to write Shared Libraries" by Ulrich Drepper.

How to write shared libraries

Ulrich Drepper got this nice PDF document, explaining how to write shared libraries on Linux. A must read before anyone ventures onto writing one.

Tuesday, December 18, 2007

Resolving unresolved symbol error in Linux shared libraries

Frequently we used to come across a case in which when we create shared libraries on a Linux box and try dynamic loading using 'dlsym' methods - the load fails with unresolved symbols error.

InformIT hosts a wonderful article on the different techniques on resolving these errors.

Option 1:

"To generate an executable that exports all of its functions and variables dynamically, specify the compiler flag -Wl,-export-dynamic when you link your executable"

Option 2:

"Your main executable may load other shared libraries at runtime, using dlopen. By default, they cannot see each other's dynamic symbols. You can make these libraries' symbols visible to each other by specifying the bitwise or of the value RTLD_GLOBAL in the second argument to dlopen."

Specifically I like Option 1 as it makes me aware of the linking going. RTLD_GLOBAL seems too generic to me.

Saturday, December 1, 2007

Why gethostbyname is evil

Ulrich Drepper discusses why gethostbyname is evil in his post.

Moral of the story: Use gethostbyaddr even if we are not talking about IPv6 .

Robust Futex

A wonderful commentary on robust futexes by Ingo Molnar available here