将信号从Anthoter非过程中发送到线程,并没有发生记录堆栈
Sending signal to thread from annother non-process and logging stack not happening
我试图从另一个过程中将信号发送到posix线程(不是来自创建该线程的过程。我使用kill(...)::
int trap_handle(pid_t child_waited )
69 {
70 printf("%s, new value: %d, child_waited=<%ld>n", __func__,g_var_x, child_waited);
71 int errno_ = -1;
72 errno_ = kill(child_waited, SIGUSR1);
73 //syscall(SYS_tgkill, -1, child_waited, SIGUSR1);
74 //errno_ = pthread_kill(child_waited, SIGUSR1);
75 if(0==errno_)
76 printf("Signal sent to thread: %ldn", child_waited);
77 else
78 printf("pthread_kill failed: error:%d", errno_);
79 }
在注册sigusr1的线程中:
230 void baz() {
231 g_var_x++;
232 }
233
234 void bak() { baz(); }
235 void bar() { bak(); }
236 void foo() { bar(); }
237
238 void stack_dump()
239 {
240 printf("******trap() entry ******n");
241 void *array[100];
242 size_t size;
243 // get void*'s for all entries on the stack
244 size = backtrace(array, 100);
245
246 // print out all the frames to stderr
247 // fprintf(stderr, "Error: signal %d:n", sig);
248 backtrace_symbols_fd(array, size, STDERR_FILENO);
249 printf("*******trap() exit ******n");
250 }
251
252 void* thread_proc_one(void *lParam)
253 {
254 printf("--Entry: thread_one debugee tid<%ld> n", syscall(SYS_gettid));
255 g_arg_params.debugee_tid = syscall(SYS_gettid);
256
257 struct sigaction trap_action;
258 //printf("Childprocess <tid> %dn", syscall (SYS_gettid));
259 memset(&trap_action, 0, sizeof(trap_action));
260 sigaction(SIGUSR1, NULL, &trap_action);
261 trap_action.sa_sigaction = stack_dump;
262 trap_action.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
263 sigaction(SIGUSR1, &trap_action, NULL);
....
现在,这可以预期它将倒入thraed stack而不是调用它的主要过程。但没有发生。stack_dump被称为,但是它没有记录线程堆栈,而是记录了父母的堆栈。backtrace正在显示创建此thread_proc_one线程的过程的堆栈。
这里有人遇到了这个问题吗?希望我清楚。
sigaction()
为整个过程安装信号处理程序。
来自 man sigaction
(斜体由我):
sigaction()系统调用用于更改接收特定信号时A Process 采取的动作。
哪个过程的线程将其处理为OS。
来自man 7 signal
:
信号处置是每个程序属性:在多线程中 应用,特定信号的处理是相同的 所有线程。
确保通过特定线程处理某个信号,请使用 pthread_sigmask()
掩盖所有线程的信号,但要处理它的信号。
再次来自 man 7 signal
:
一个过程中的每个线程都有一个独立的信号掩码, 指示该线程当前阻塞的信号集。 线程可以使用 pthread_sigmask(3)来操纵其信号掩码。
因此,例如,可以通过在创建任何线程之前掩盖所讨论的信号,然后在线程内部处理信号呼叫pthread_sigmask()
以再次揭示要处理的信号,从而在主线程中呼叫pthread_sigmask()
来完成此操作。。
相关文章:
- 我是否生成线程并导致内存泄漏?
- 为什么 beginthreadex 线程参数变量在父线程中没有更新
- 生成线程并在运行时执行其他操作,只要它处于活动状态
- 从包装器类scoped_thread中运行的线程中没有ouput
- C++ std 线程并列出分段错误(核心转储)
- C++ - 在 main() 方法内的类实例中创建一个线程并调用 Sleep()
- 为什么Win API线程执行函数,而标准线程则没有
- 为什么在多线程中没有接收到发出的信号
- TBB C++多线程错误:没有匹配的调用函数
- 创建一个以存储线程并调用它们的类
- 启动多个线程并重新启动它们
- 创建线程并调用类的函数
- 打开线程并关闭多个线程句柄
- Boost:创建一组线程并等待所有线程的正确习惯用法是什么
- C++线程:在没有活动异常的情况下终止调用
- 如何将数据传输到另一个线程并使helgrind知道它
- 尝试创建 posix 线程并获得从 'void*' 到 'void* (__attribute__((__cdecl__)) *)(void*) 错误的无效转换
- 如何阻塞线程并恢复它
- 如何唤醒从另一个线程选择没有超时时间的呼叫
- 如果父线程 ID 没有作为pthread_create上的参数传入,是否可以从 C/C++ 中的线程中获取父线程 ID?