UNIX/Linux信号处理:SIGEV_THREAD

kin*_*er1 4 c unix linux posix signals

我在我的代码中放了一个简单的信号处理程序.我初始化了sigevent结构,使用处理函数来捕获信号.

有人可以指出为什么代码不工作?理想情况下,如果有信号,我的处理程序应该被调用.但事实并非如此.

请帮帮我,谢谢Kingsmasher1

enter code here
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>

void my_handler(int sival_int, void* sival_ptr)
{
 printf("my_handler caught\n");
 signal(sig,my_handler);
}

int main()
{
 struct sigevent sevp;

 sevp.sigev_notify=SIGEV_THREAD;
 sevp.sigev_signo=SIGRTMIN;
 sevp.sigev_value.sival_ptr=NULL;
 sevp.sigev_notify_function=(void*)my_handler;
 kill(0,SIGRTMIN); // This should invoke the signal and call the function
}
Run Code Online (Sandbox Code Playgroud)

caf*_*caf 14

struct sigevent是不是指定进程将如何处理的信号- struct sigactionsigaction()你是如何做到这一点.相反,struct sigevent它用于指定如何通知某些异步事件 - 例如异步IO完成或计时器到期.

sigev_notify字段指定如何通知事件:

  • SIGEV_NONE - 根本没有通知.其余字段将被忽略.
  • SIGEV_SIGNAL - 向进程发送信号.该sigev_signo字段指定信号,该sigev_value字段包含传递给信号处理功能的补充数据,其余字段被忽略.
  • SIGEV_THREAD - 在新线程中调用函数.该sigev_notify_function字段指定被调用的函数,sigev_value包含传递给函数的补充数据,并sigev_notify_attributes指定用于创建线程的线程属性.其余字段将被忽略.

请特别注意,如果你设置SIGEV_THREAD,该sigev_signo字段被忽略- struct sigevent是关于指定任何一个线程或信号的通知方法,不是指定一个线程中的信号应该处理方式.

struct sigevent还必须传递给函数-像timer_create()- ,设置了将被通知台异步事件.简单地创建一个struct sigevent对象并没有什么特别之处.

如果您希望使用专用线程来处理信号,请在前面创建线程并使其循环,阻塞sigwaitinfo().使用sigprocmask()以阻止所有其他线程的信号.