c - 寻找有关死锁场景的指导

标签 c signals deadlock

我有一个程序可以产生很多 child 并运行很长时间。该程序包含一个 SIGCHLD 处理程序来获取已失效的进程。有时,该程序会卡住。我相信 pstack 表明出现了死锁情况。这是对此输出的正确解释吗?

10533:  ./asyncsignalhandler
 ff3954e4 lwp_park (0, 0, 0)
 ff391bbc slow_lock (ff341688, ff350000, 0, 0, 0, 0) + 58
 ff2c45c8 localtime_r (ffbfe7a0, 0, 0, 0, 0, 0) + 24
 ff2ba39c __posix_ctime_r (ffbfe7a0, ffbfe80e, ffbfe7a0, 0, 0, 0) + c
 00010bd8 gettimestamp (ffbfe80e, ffbfe828, 40, 0, 0, 0) + 18
 00010c50 sig_chld (12, 0, ffbfe9f0, 0, 0, 0) + 30
 ff3956fc __sighndlr (12, 0, ffbfe9f0, 10c20, 0, 0) + c
 ff38f354 call_user_handler (12, 0, ffbfe9f0, 0, 0, 0) + 234
 ff38f504 sigacthandler (12, 0, ffbfe9f0, 0, 0, 0) + 64
 --- called from signal handler with signal 18 (SIGCLD) ---
 ff391c14 pthread_mutex_lock (20fc8, 0, 0, 0, 0, 0) + 48
 ff2bcdec getenv   (ff32a9ac, 770d0, 0, 0, 0, 0) + 1c
 ff2c6f40 getsystemTZ (0, 79268, 0, 0, 0, 0) + 14
 ff2c4da8 ltzset_u (4ede65ba, 0, 0, 0, 0, 0) + 14
 ff2c45d0 localtime_r (ffbff378, 0, 0, 0, 0, 0) + 2c
 ff2ba39c __posix_ctime_r (ffbff378, ffbff402, ffbff378, ff33e000, 0, 0) + c
 00010bd8 gettimestamp (ffbff402, ffbff402, 2925, 29a7, 79c38, 10b54) + 18
 00010ae0 main     (1, ffbff4ac, ffbff4b4, 20c00, 0, 0) + 190
 00010928 _start   (0, 0, 0, 0, 0, 0) + 108

我真的不认为自己是 C 编码员,也不熟悉这种语言的细微差别。我在程序中专门使用了 ctime(_r) 的可重入版本。为什么这仍然是僵局?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <time.h>

// import pid_t type
#include <sys/types.h>

// import _exit function
#include <unistd.h>

// import WNOHANG definition
#include <sys/wait.h>

// import errno variable
#include <errno.h>

// header for signal functions
#include <signal.h>

// function prototypes
void sig_chld(int);
char * gettimestamp(char *);

// begin
int main(int argc, char **argv)
{
   time_t   sleepstart;
   time_t   sleepcheck;
   pid_t    childpid;
   int i;
   unsigned int sleeptime;
   char sleepcommand[20];
   char ctime_buf[26];

   struct sigaction act;

   /* set stdout to line buffered for logging purposes */
   setvbuf(stdout, NULL, _IOLBF, BUFSIZ);

   /* Assign sig_chld as our SIGCHLD handler */
   act.sa_handler = sig_chld;

   /* We don't want to block any other signals */
   sigemptyset(&act.sa_mask);

   /*
    * We're only interested in children that have terminated, not ones
    * which have been stopped (eg user pressing control-Z at terminal)
    */
   act.sa_flags = SA_NOCLDSTOP;

   /* Make these values effective. */
   if (sigaction(SIGCHLD, &act, NULL) < 0) 
   {
      printf("sigaction failed\n");
      return 1;
   }

   while (1) {
      for (i = 0; i < 20; i++) {
         /*   fork/exec child program                                */
         childpid = fork();
         if (childpid == 0) // child
         {
            //sleeptime = 30 + i;
            sprintf(sleepcommand, "sleep %d", i);

            printf("\t[%s][%d] Executing /bin/sh -c %s\n", gettimestamp(ctime_buf), getpid(), sleepcommand);

            execl("/bin/sh", "/bin/sh", "-c", sleepcommand, NULL);

            // only executed if exec fails
            printf("[%s][%d] Error executing program, errno: %d\n", gettimestamp(ctime_buf), getpid(), errno);
            _exit(1);
         }
         else if (childpid < 0) // error
         {
            printf("[%s][%d] Error forking, errno: %d\n", gettimestamp(ctime_buf), getpid(), errno);
         }
         else // parent
         {
            printf("[%s][%d] Spawned child, pid: %d\n", gettimestamp(ctime_buf), getpid(), childpid);
         }
      }

      // sleep is interrupted by SIGCHLD, so we can't simply sleep(5)
      printf("[%s][%d] Sleeping for 5 seconds\n", gettimestamp(ctime_buf), getpid());
      time(&sleepstart);
      while (1) {
         time(&sleepcheck);
         if (difftime(sleepcheck, sleepstart) < 5) {
            sleep(1);
         } else {
            break;
         }
      }
   }


   return(0);
}

char * gettimestamp(char *ctime_buf)
{
   time_t now;

   time(&now);

   // format the timestamp and chomp the newline
   ctime_r(&now, ctime_buf);
   ctime_buf[strlen(ctime_buf) - 1] = '\0';

   return ctime_buf;
}

/*
 * The signal handler function -- only gets called when a SIGCHLD
 * is received, ie when a child terminates.
 */
void sig_chld(int signo)
{
   pid_t childpid;
   int childexitstatus;
   char ctime_buf[26];

   while (1) {
      childpid = waitpid(-1, &childexitstatus, WNOHANG);
      if (childpid > 0)
         printf("[%s][%d] Reaped child, pid: %d, exitstatus: %d\n", gettimestamp(ctime_buf), getpid(), childpid, WEXITSTATUS(childexitstatus));
      else
         return;
   }
}

我在 Solaris 9 环境中运行。该程序是使用以下语法使用 Sun WorkShop 6 update 2 C 5.3 Patch 111679-15 2009/09/10 编译的:

cc -o asyncsignalhandler asyncsignalhandler.c -mt -D_POSIX_PTHREAD_SEMANTICS

程序有缺陷吗?是否有更好的方法来处理来自信号处理程序的日志记录(带有时间戳)?

最佳答案

您正在从信号处理程序中调用非异步信号安全的函数(参见 unix 规范的 section 2.4.3)——在本例中,ctime_r()printf () (死锁似乎是由于 ctime_r() 在您显示的堆栈跟踪中使用的锁而发生的)。这些函数可能需要锁,并且由于信号处理程序可能随时被调用,锁可能已经被持有,从而导致死锁。

通常,在信号处理程序中,您所要做的就是为主线程做一个注释,以便稍后检查。例如,您可以 write() (这是一个异步信号安全函数)到一个 pipe() 创建的文件描述符,并让您的主循环(或另一个线程)执行一个选择循环以等待一些数据显示在该管道上。

另请注意,线程安全异步信号安全 不同。 ctime_r 是线程安全的——它需要锁来确保线程不会相互干扰,并且它使用传入的缓冲区而不是静态缓冲区。但它不是异步信号安全的,因为它不能容忍在执行过程中的任意点被重入调用。

关于c - 寻找有关死锁场景的指导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8405752/

相关文章:

python - Paramiko 和 exec_command - 杀死远程进程?

c++ - 进入临界区死锁

java - 一种在不同语言/平台中使用 Openssl 的方法

c - 将 for 循环中的字符串保存到 c 中的数组

可以成功捕获 CTRL-Z,但不能捕获 SIGTSTP

python - 巴特沃斯滤波器 - 输出 x (-1)?

c - 如果我在下面的代码片段中跳过 waiting[i] = false 会发生什么?

c# - 对 WCF 客户端的异步调用会阻止后续的同步调用

c - 为什么 "memset(arr, -1, sizeof(arr)/sizeof(int))"不将整数数组清除为 -1?

c - 我如何知道程序为何会导致核心转储段错误?