To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

C signal handling

From Wikipedia, the free encyclopedia

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).

YouTube Encyclopedic

  • 1/3
    Views:
    3 364
    1 182
    2 147
  • Interrupts and Signals Intro
  • Signals in Linux/Unix
  • Program 10 | VTU | USPCD Lab | SIGALRM using alarm system call

Transcription

Standard signals

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

  • SIGABRT – "abort", abnormal termination.
  • SIGFPEfloating 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]

Debugging

  • SIGTRAP for debugging purposes. It's platform-dependent and may be used on Unix-like operating systems.

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 with signal() or sigaction(). The behavior of signal() has been changed multiple times across history and its use is discouraged.[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 artificially sends a signal to the calling process
kill artificially sends a signal to a specified process
signal sets the action taken when the program receives a specific signal

Example usage

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

volatile sig_atomic_t status = 0;

static void catch_function(int signo) {
    status = signo;
}

int main(void) {
    // Set above function as signal handler for the SIGINT signal:
    if (signal(SIGINT, catch_function) == SIG_ERR) {
        fputs("An error occurred while setting a signal handler.\n", stderr);
        return EXIT_FAILURE;
    }
    puts("Raising the interactive attention signal.");
    if (raise(SIGINT)) {
        fputs("Error raising the signal.\n", stderr);
        return EXIT_FAILURE;
    }
    if (status == SIGINT) puts("Interactive attention signal caught.");
    puts("Exiting.");
    return EXIT_SUCCESS;
    // exiting after raising signal
}

See also

References

  1. ^ ISO/IEC 9899:1999 specification (PDF). p. 258, § 7.14 Signal handling.
  2. ^ "The Open Group Base Specifications Issue 6 – signal.h – signals". Retrieved 10 January 2012.
  3. ^ https://man7.org/linux/man-pages/man2/signal.2.html Signal(2) manpage
This page was last edited on 27 February 2024, at 23:05
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.