Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deadlock in a process, Unix command?

I was trying to figure out how to know if the threads of a process have been deadlocked on Unix/Linux machine? Also, is there a command for knowing what stage (or status) a process is in? If you know of any tools, please suggest. Thank you.

like image 827
Chenna V Avatar asked Jul 16 '26 14:07

Chenna V


1 Answers

Thanks to /proc/<pid>/syscall, this is how I ended up implementing a quick and dirty processes futex(op=FUTEX_WAIT) scanner.

#!/bin/bash
#
# Find all processes that are executing a futex(2) call with op=FUTEX_WAIT
# In some cases this can be helpful in finding deadlock-ed processes.
#

test ! $UID -eq 0 && echo -e "WARNING: Not running as root, only processes for this user are being scanned\n" >&2;
pids=$(ps -u $UID -opid --no-headers)

for pid in $pids; do
        cat /proc/$pid/syscall |

        awk "{if (\$1 == 202 && \$3 == \"0x0\") {
                print $pid
        }}";

        # $1 is the syscall, we compare to 202 which is the futex call
        # See: /usr/include/asm/unistd.h

        # $2 is the 1st param, $3 is the 2nd param, etc
        # We compare the second param to 0x0 which is FUTEX_WAIT
        # See: /usr/include/linux/futex.h
done
like image 62
Amr Mostafa Avatar answered Jul 18 '26 16:07

Amr Mostafa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!