请输入您要查询的百科知识:

 

词条 C signal handling
释义

  1. Standard signals

  2. Handling

  3. Functions

  4. Example usage

  5. See also

  6. References

{{C_Standard Library}}

In the C Standard Library, signal processing defines how a program handles various signals while it executes. A signal can report some exceptional behavior within the program (such as division by zero), or a signal can report some asynchronous event outside the program (such as someone striking an interactive attention key on a keyboard).

Standard signals

{{also|Unix signal}}

The C standard defines only 6 signals. They are all defined in signal.h header (csignal header in C++):[1]

  • SIGABRT - "abort", abnormal termination.
  • SIGFPE - floating point exception.
  • SIGILL - "illegal", invalid instruction.
  • SIGINT - "interrupt", interactive attention request sent to the program.
  • SIGSEGV - "segmentation violation", invalid memory access.
  • SIGTERM - "terminate", termination request sent to the program.

Additional signals may be specified in the signal.h header by the implementation. For example, Unix and Unix-like operating systems (such as Linux) define more than 15 additional signals; see Unix signal.[2]

Handling

A signal can be generated by calling raise() or kill() system calls. raise() sends a signal to the current process, kill() sends a signal to a specific process.

A signal handler is a function which is called by the target environment when the corresponding signal occurs. The target environment suspends execution of the program until the signal handler returns or calls longjmp().

Signal handlers can be set be with signal() or sigaction(). The behavior of signal() has been changed multiple times across history and is now considered deprecated[3]. It is only portable when used to set a signal's disposition to SIG_DFL or SIG_IGN. Signal handlers can be specified for all but two signals (SIGKILL and SIGSTOP cannot be caught, blocked or ignored).

If the signal reports an error within the program (and the signal is not asynchronous), the signal handler can terminate by calling abort(), exit(), or longjmp().

Functions

Function Description
raise}}raise artificially raises a signal
signal}}signal sets the action taken when the program receives a specific signal

Example usage

  1. include
  2. include
  3. include

static void catch_function(int signo) {

}

int main(void) {

    if (signal(SIGINT, catch_function) == SIG_ERR) {        fputs("An error occurred while setting a signal handler.\", stderr);        return EXIT_FAILURE;    }    puts("Raising the interactive attention signal.");    if (raise(SIGINT) != 0) {        fputs("Error raising the signal.\", stderr);        return EXIT_FAILURE;    }    puts("Exiting.");    return EXIT_SUCCESS;    // exiting after raising signal

}

See also

  • Unix signal

References

1. ^{{cite book | url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf | title=ISO/IEC 9899:1999 specification | at=p. 258, § 7.14 Signal handling}}
2. ^{{cite web | url=http://pubs.opengroup.org/onlinepubs/007904975/ | title=The Open Group Base Specifications Issue 6 - signal.h - signals | accessdate=10 January 2012}}
3. ^https://www.cs.drexel.edu/cgi-bin/manServer.pl/signal.2 Signal(2) manpage
{{Use dmy dates|date=January 2012}}

1 : C standard library

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/9/30 20:19:58