1

更新:这似乎是一个时间问题。在调用 kill 之前添加调用 sleep 可以使一切按预期工作。

我一直在玩 clone(2) 并试图了解它是如何工作的。我目前无法向克隆进程发送信号。我有以下代码:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sched.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h>

volatile int keep_going = 1;

typedef void (*sighandler_t)(int);

void handler(int sig) {
   printf("Signal Received\n");
   keep_going = 0;
}

int thread_main(void* arg) {
   struct sigaction usr_action;
   sigset_t block_mask;
   sigfillset(&block_mask);
   usr_action.sa_handler = &handler;
   usr_action.sa_mask = block_mask;
   usr_action.sa_flags = 0;
   sigaction(SIGUSR1, &usr_action, NULL);
   printf("Hello from cloned thread\n");
   while(keep_going);
}

int main(int argc, char **argv) {
   void* stack = malloc(4096);
   int flags = SIGCHLD;
   int child_tid = clone(&thread_main, stack + 4096, flags, NULL);
   if (child_tid < 0) {
      perror("clone");
      exit(EXIT_FAILURE);
   }
   printf("My pid: %d, child_tid: %d\n", (int) getpid(), (int) child_tid);
   int kill_ret = kill(child_tid, SIGUSR1);
   if (kill_ret < 0) {
      perror("kill");
      exit(EXIT_FAILURE);
   }
   int status = 0;
   pid_t returned_pid = waitpid(child_tid, &status, 0);
   if (returned_pid < 0) {
      perror("waitpid");
      exit(EXIT_FAILURE);
   }
   if (WIFEXITED(status)) {
      printf("exited, status=%d\n", WEXITSTATUS(status));
   } else if (WIFSIGNALED(status)) {
      printf("killed by signal %d\n", WTERMSIG(status));
   } else if (WIFSTOPPED(status)) {
      printf("stopped by signal %d\n", WSTOPSIG(status));
   } else if (WIFCONTINUED(status)) {
      printf("continued\n");
   }
   exit(EXIT_SUCCESS);
}

产生以下输出:

My pid: 14101, child_tid: 14102
killed by signal 10

孩子明明被信号杀死了,为什么没有调用信号处理程序?

4

3 回答 3

2

为避免竞争条件,请在clone()调用之前捕获父级上的信号。子继承父信号处理程序的副本。如果需要,您可以稍后在父级上将其重置为 SIG_DFL。(另外,getpid()如果你想在父节点上模拟 SIG_DFL 行为,它是异步信号安全的)。

于 2011-07-15T20:19:22.423 回答
1

孩子没有收到信号,因为在孩子到达对sigaction父母的呼叫之前正在发送信号,这就是它被杀死的原因。您应该避免以这种方式设置信号处理程序。尽管如此,如果您只想这样做,请确保父母正在等待,直到孩子设置信号处理程序。在这种情况下,您应该会看到预期的结果。

于 2013-04-26T05:37:06.830 回答
0

首先奇怪的是您没有收到此消息:

"Hello from cloned thread\n"

因此,您的孩子在设法设置信号处理程序之前就被终止了。

编辑:我刚刚看到你关于睡眠的评论。尝试添加另一个变量,该变量在sigaction执行时设置。主线程应该被阻塞,直到这个变量没有被设置。

于 2011-07-15T19:36:15.960 回答