简体   繁体   English

信号处理程序和库工作线程

[英]signal handler and library worker threads

The client code of my application is single-threaded, but I'm using a TCP socket communication library that might create worker threads that should be transparent from the point of view of the client application. 我的应用程序的客户端代码是单线程的,但是我使用的是TCP套接字通信库,该库可能创建从客户端应用程序角度来看应该透明的工作线程。 However, I had a signal handler for catching SIGSEGV errors that tries to print a stack trace, that is now intermittently failing. 但是,我有一个捕获SIGSEGV错误的信号处理程序,该错误处理程序试图打印堆栈跟踪,现在间歇性地失败了。

I'm wondering if it is possible that the fact that worker threads are being created inside the process could be affecting the signal handler execution? 我想知道是否可能在进程内部创建工作线程这一事实是否会影响信号处理程序的执行? can the kernel decide to execute the signal handler inside some of the worker threads? 内核可以决定在某些工作线程中执行信号处理程序吗? and if it is so, is there some way to avoid that from happening and force the signal handler to execute always on the main process thread? 如果是这样,是否有某种方法可以避免这种情况的发生并强制信号处理程序始终在主进程线程上执行?

  1. SIGSEGV handlers have an inherent problem, which that as every SEGV (contrived examples aside) arise from undefined behaviour (to say nothing of unexpected behaviour), what the program does afterwards is equally undefined. SIGSEGV处理程序存在一个固有的问题,即每个SEGV (不包括人为的示例)都是由未定义的行为(更不用说意外的行为)引起的,程序随后所做的工作也同样未定义。 Or in less language-lawyerese terms, you don't know what's broken. 或者用较少的语言律师用语,您不知道发生了什么。 If you've overwritten the heap, for instance, or you SEGV whilst some lock within malloc() is held, your signal handler isn't going to work. 例如,如果您覆盖了堆,或者在保留malloc()内的某些锁的情况下执行SEGV ,则信号处理程序将无法正常工作。

  2. Getting threads and signal handlers to work together is not, at the best of times, easy. 在最好的情况下,让线程和信号处理程序协同工作并非易事。 Programs are far less tolerant of errors in signal set up when threaded (in my experience). 程序在线程化时对信号设置错误的容忍度要差得多(以我的经验)。 Firstly, makes sure you are using a modern API. 首先,请确保您使用的是现代API。 IE do not use signal (2) . IE不使用signal (2) Use sigaction and pthread_sigmask appropriately. 适当使用sigactionpthread_sigmask

  3. Note that some signals (including SIGSEGV ) are delivered to the process and not to the thread. 请注意,某些信号(包括SIGSEGV )被传递到进程而不是线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM