简体   繁体   English

为信号分配特定的信号处理程序

[英]Assign a specific sighandler to a signal

I have a question concerning signals in C. I want to assign some a specific sighandler to a signal. 我有一个关于C中信号的问题。我想为信号分配一些特定的信号处理程序。

My function is: 我的职能是:

void searchOccurences(char **myString, char chr, int *occurences) {
   /* I perform some search actions here 
    * at the end of this function *occurrence will contain the 
    * the number occurrences of chr in myString*/
}

I want to assign this function to the SIGILL signal (ie Illegal Instruction Signal), but I can't because the __sighandler_t handlers are of this kind of definition: 我想将此函数分配给SIGILL信号(即非法指令信号),但我不能这样做,因为__sighandler_t处理程序具有这种定义:

typedef void (*__sighandler_t) (int);

So How can I assign my function to this signal? 那么,如何为该信号分配功能呢? What are the alternative solutions to what I want to do? 我想做什么替代解决方案?

Thanks :) 谢谢 :)

Don't use SIGILL, that could mask a genuine error. 不要使用SIGILL,这可能掩盖真正的错误。 SIGUSR1 and SIGUSR2 are specifically for user defined signals. SIGUSR1和SIGUSR2专用于用户定义的信号。

You can't pass additional information, sorry, but there it is. 很抱歉,您无法传递其他信息,但是有。 The best you can do is to store information in a global before raising a signal, but you must be certain of the sequence of events, or use some sort of synchronisation like a mutex. 最好的办法是在发出信号之前将信息存储在全局变量中,但是您必须确定事件的顺序,或者使用某种同步,例如互斥锁。

By the way, are you aware that many C runtime library functions are not signal safe? 顺便说一句,您是否知道许多C运行时库函数并不安全?

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

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