我正在尝试将信号处理程序移动为线程(以便可以使用互斥锁)。我发现诸如this之类的几页描述了我应该做什么,但是当我尝试时,信号处理程序将永远不会触发。
以下是我的处理程序:
{
int status;
// Block current thread from trapping signal:
sigset_t sigSet;
sigemptyset(&sigSet);
sigaddset(&sigSet, SIGSEGV);
status = pthread_sigmask( SIG_BLOCK, &sigSet, NULL);
dbgassert(status == 0);
// create thread to capture signal
status = pthread_create(&signalThread, NULL, signalThreadFn, NULL);
dbgassert(status == 0);
sleep(1);
debugf("about to die");
// generate segv:
rt = posix_memalign((void **)&pool, getpagesize(), getpagesize());
dbgassert(rt==0);
mprotect(pool, getpagesize(), PROT_READ);
pool[0]=10;
debugf("resurected!!!");
}
static void *
signalThreadFn(void *parm) {
sigset_t sigSet;
int sig;
siginfo_t sigInfo = {};
debugf("Starting signal thread");
sigemptyset(&sigSet);
sigaddset(&sigSet, SIGSEGV);
while( 1 ) {
debugf("waiting for SIGSEGV...");
sig=sigwaitinfo(&sigSet,&sigInfo);
debugf("Got signal %d",sig);
...
}
}
,但是线程从不捕获信号。我得到输出:
DEBUG [signalThreadFn.841]: waiting for SIGSEGV...
DEBUG [constructor.240]: about to die
Segmentation fault (core dumped)
我不确定为什么信号处理程序没有捕获信号。据我了解,子线程应该继承其父级的掩码,并且由于禁用了SIGSEGV,在该线程执行其sigwaitinfo之前,它们都不应该捕获信号-为什么子级不捕获信号?