UNIX/Linux信号处理:SIGEV_THREAD [英] UNIX/Linux signal handling: SIGEV_THREAD

查看:116
本文介绍了UNIX/Linux信号处理:SIGEV_THREAD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中放置了一个简单的信号处理程序.我已经初始化了sigevent结构,并使用了处理程序功能来捕获信号.

I have put a simple signal handler in my code. I have initialised the sigevent structure, with a handler function to catch the signal.

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

Can someone please pin-point as to why the code is not working? Ideally if there is a signal, my handler should be called. But it is not.

请帮助我,谢谢Kingsmasher1

Please help me, Thanks 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
}

推荐答案

struct sigevent 并不是要指定进程如何处理信号- struct sigaction sigaction()是您的操作方式.取而代之的是,使用 struct sigevent 指定如何将某些异步事件通知您的进程,例如异步IO的完成或计时器到期.

struct sigevent is not about specifying how the process will handle a signal - struct sigaction and sigaction() are how you do that. Instead, struct sigevent is used to specify how your process will be informed of some asychronous event - like the completion of asychronous IO, or a timer expiring.

sigev_notify 字段指定事件的通知方式:

The sigev_notify field specifies how the event should be notified:

  • SIGEV_NONE -完全没有通知.其余字段将被忽略.
  • SIGEV_SIGNAL -将信号发送到进程. sigev_signo 字段指定信号, sigev_value 字段包含传递到信号处理功能的补充数据,其余字段将被忽略.
  • SIGEV_THREAD -在新线程中调用一个函数. sigev_notify_function 字段指定被调用的函数, sigev_value 包含传递给该函数的补充数据,而 sigev_notify_attributes 指定用于以下操作的线程属性线程创建.其余字段将被忽略.
  • SIGEV_NONE - no notification at all. The remainder of the fields are ignored.
  • SIGEV_SIGNAL - a signal is sent to the process. The sigev_signo field specifies the signal, the sigev_value field contains supplementary data that is passed to the signal handling function, and the remainder of the fields are ignored.
  • SIGEV_THREAD - a function is called in a new thread. The sigev_notify_function field specifies the function that is called, sigev_value contains supplementary data that is passed to the function, and sigev_notify_attributes specifies thread attributes to use for the thread creation. The remainder of the fields are ignored.

请特别注意,如果设置 SIGEV_THREAD ,则 sigev_signo 字段将被忽略- struct sigevent 是关于指定两个将线程或信号作为通知方法,而不是将线程指定为信号的处理方式.

Note in particular that if you set SIGEV_THREAD, the sigev_signo field is ignored - the struct sigevent is about specifying either a thread or a signal as a notification method, not about specifying a thread as the way that a signal should be handled.

还必须将 struct sigevent 传递给一个函数-例如 timer_create()-来设置将被通知的异步事件.仅仅创建一个 struct sigevent 对象并没有什么特别的事情.

The struct sigevent must also be passed to a function - like timer_create() - that sets up the asychronous event that will be notified. Simply creating a struct sigevent object does not do anything special.

如果您希望使用专用线程来处理信号,请预先创建线程并使其循环,从而阻塞 sigwaitinfo().使用 sigprocmask()来阻塞其他所有线程中的信号.

If you wish to use a dedicated thread to handle a signal, create the thread up front and have it loop around, blocking on sigwaitinfo(). Use sigprocmask() to block the signal in every other thread.

这篇关于UNIX/Linux信号处理:SIGEV_THREAD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆