问题描述
大家好,
我想知道何时以及如何使用信号(例如SIGFPE,SIGABRT,
SIGTERM,SIGSEGV,SIGINT)?非常感谢你。
信号提供了处理异步(和意外)事件的方法。
信号(数字)与一个事件。例如:
SIGFPE:与事件浮点错误(FPE)相关联。对于
示例:当进程尝试将数字除以零时发生这种情况
,如p = 5/0。
SIGSEGV:与事件段违规相关联。当进程试图访问非法内存段时生成
(这是带指针的常见错误
)。
SIGINT:当终端按下
中断键(通常是Ctrl + C)时,由终端(控制台)生成。
每个信号都有关联默认动作,发生在生成信号的时候。但是这可以使用
信号函数之一(如signal(),sigaction())来改变。这些函数允许我们指定用户定义的动作(函数)来处理信号。
现在安装我们自己的处理程序后,如果信号发生..这次
将采取用户指定的操作来处理该信号。
但是有些信号无法捕捉和处理......
喜欢:SIGKILL,SIGSTOP。
Plzz参考你的系统手册了解更多信息...
- Gana
谢谢你很多回复。你能给我一个代码示例,显示
如何能够使用< signal.h>中的信号处理异步
(和意外)事件?我应该调用这个函数:" int raise(int sig)"?
我非常想知道如何在交流代码中使用信号,因为信号
是在标准库中提到。
Hi everyone,
I''d like to know when and how signals are used (e.g. SIGFPE, SIGABRT,
SIGTERM, SIGSEGV, SIGINT)? Thank you so much.
Signals provide a way to handle asynchronous (and unexpected) events.
A signal (number) is associated with an event. Like:
SIGFPE: associated with the event "Floating Point Error (FPE)". For
example: This occurs when the process tries to divide a number by zero
as in p=5/0.
SIGSEGV: associated with the event "Segment Violation". Is generated
when the process tries to access an illegal memory segment (this is a
common error with pointers).
SIGINT: This is generated by the terminal (console) when the terminals
interrupt key (usually Ctrl+C) is pressed.
Every signal has an associated default action, that takes place when
the signal is generated. But this can be changed using one of the
signal functions (like signal(), sigaction()). These functions allow
us to specify a user defined action (function) for handling a signal.
Now after installing our own handler, if the signal occurs.. This time
the user specified action will be taken to handle that signal.
However there are some signals which cannot be caught and handled...
Like: SIGKILL, SIGSTOP.
Plzz refer to ur systems manual for more information...
- Gana
Thank you so much for replying. Can you give me a code example that shows
how one may be able to use the signals in <signal.h> to handle asynchronous
(and unexpected) events? Should I call this function: "int raise(int sig)"?
I very much like to know how to use signals in a c code since the signals
are mentioned in the standard library.
That confuses me a lot too. Even the function prototype
takes a while to figure out.
Let us see now.
void (*signal(int sig, void (*func)(int)))(int);
singal is a function taking two parameters and teturning
a pointer to a function.
The paramters are (1) an int, and (2) a pointer to a
function taking an int and returning nothing.
The return value is a pointer to a function taking an int
and returning nothing.
Does this seem ok?
Now, how do I declare a pointer to this signal function?
这篇关于< signal.h中> - 关于信号的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!