首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >进程中的死锁,Unix命令?

问进程中的死锁,Unix命令?
EN

Stack Overflow用户
提问于 2010-11-29 01:48:36
回答 3查看 7.2K关注 0票数 3

我想知道如何知道进程的线程在Unix/Linux机器上是否死锁了?另外,有没有一个命令可以知道进程所处的阶段(或状态)?如果您知道任何工具,请建议。谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-11-29 02:09:07

尝试使用跟踪系统调用的工具,例如Linux上的strace或HP-UX上的tusc。当死锁发生时,您应该看到进程在阻塞调用中挂起。然而,这并不是一个积极的证据。它可能是一个常规的块。然后,您需要确定是否可以在一段时间内解决该块。这需要了解进程正在等待的资源。

示例

在RHEL4上有一个...奇特之处。这可能会导致ctime死锁。在下面找到一个显示此行为的示例程序:

代码语言:javascript
复制
#include <sys/time.h>
#include <time.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

volatile char *r;

void handler(int sig)
{
    time_t t;

    time(&t);
    r = ctime(&t);
}

int main()
{
    struct itimerval it;
    struct sigaction sa;
    time_t t;
    int counter = 0;

    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = handler;
    sigaction(SIGALRM, &sa, NULL);

    it.it_value.tv_sec = 0;
    it.it_value.tv_usec = 1000;
    it.it_interval.tv_sec = 0;
    it.it_interval.tv_usec = 1000;
    setitimer(ITIMER_REAL, &it, NULL);

    while(1) {
        counter++;
        time(&t);
        r = ctime(&t);
        printf("Loop %d\n",counter);
    }

    return 0;
}

这通常会在几千次迭代后进入死锁。现在,像这样附加strace

代码语言:javascript
复制
strace -s4096 -p<PID>

其中PID是程序的进程id。您将看到程序在参数中带有FUTEX_WAIT的调用中挂起。(我不能引用整行,因为我目前不能访问RHEL4机器,请原谅)。

票数 1
EN

Stack Overflow用户

发布于 2010-12-01 02:42:54

多亏了/proc/<pid>/syscall,这就是我最终实现quick and dirty processes futex(op=FUTEX_WAIT) scanner的原因。

代码语言:javascript
复制
#!/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
票数 2
EN

Stack Overflow用户

发布于 2010-11-29 02:03:28

UNIX保证操作系统进程永远不会陷入死锁。然而,对于用户定义的进程,没有这样的保证。据我所知,没有直接的方法来确定进程是否已经死锁。

也就是说,您可以通过ps -o pid,uname,command,state,stime,time确定进程状态。man ps还显示了有关进程状态代码的更详细的解释。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4298104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档