Semaphore debugging

I'm running one multithreaded application, in that one of my thread
is waiting infinitely in a semphore. Is there a way to determine, in
which semaphore the particular thread is waiting and which thread(s)
is holding the semaphore.

If you have a decent thread aware debugger you could check the stack of each thread to see who was blocked on a pthread_mutex_lock, then depending on the operating system and implementation of pthreads on that OS you may be able to find the thread id in the "pthread_mutex_t" structure.

Personally I solve this by using my own version of a pthreads library which lets me add additional tracing and warning of potential deadlocks.

Porter,

In my application semaphores, are not created using any threading library. Semaphores (counting) are created using sema_init. Is there any system calls or functions available to get the semaphores a thread is currently having?

Not to my knowledge, these APIs date back to not threaded days.

Do these semaphores have to work between processes?

What platform are you using? Post results of "uname -a".

These semaphores work between threads of a single process. I'm using linux-2.6.14 in PPC architecture..

If you are doing synchronisation between threads of the same process then I recommend using pthread APIs to do this. The sem APIs always involve the kernel, but the pthread APIs may be able to avoid kernel involvement for some activities.

An up/down semaphore should be able to be constructed using a pthread_mutex_t and pthread_cond_t pair.