[Note] windows signal mechanism (signals)

Source: Internet
Author: User
Tags sigint signal signal handler

In Windows, signal and piaoger have never had a chance to use it. Today, since we have encountered it in a console program, it is necessary to take a look at the world and take notes:


> Signals in Windows (SIGNAl)

A signal is a message generated by itself or from outside the process while the process is running.

The signal is a software simulation of hardware interruption (soft interruption ). Each signal is represented by an integer constant macro starting with sig, such as sigchld and Sigint. They are defined in the system header file <signal. h>.

This is said to have been used in Linux, but it is quite different.BLook.

> Signal Source

The signal generation comes from the kernel, allowing the kernel to generate signal requests from three places: Users and kernel-protected processes.

++ User: You can request the kernel to generate a signal by entering Ctrl + C, CTRL +, or any other key that the Terminal Driver assigns to the signal control character;

++ Kernel: when a process execution error occurs, the kernel sends a signal to the process, such as illegal segment access (memory access violation) and floating point overflow.

++ Process: A process can send signals to another process by calling kill. A process can communicate with another process through signals.

The signal generated by an operation of a process is called a synchronous signal (synchronous signals), for example, Division by 0. The signal generated by external events such as user-initiated keys is called an asynchronous signal. (Asynchronous signals ).


> Signal Processing

After receiving the signal, the process can process the following three options:

++ Receive default processing: processes that receive default processing usually die out. For example, if you press Ctrl + C to connect to a terminal process, the kernel will send a SIGINT signal to the process. If the process does not process the signal specially, the system processes the signal by default, that is, the process is terminated;

++ Ignore signal: A process can display and ignore the processing of a signal, for example, signal (SIGINT, sigdef). However, some signals cannot be ignored,

++ Signal capturing and processing: A process can register a signal processing function in advance. When a signal is received, the signal processing function automatically captures and processes the signal.

Two signals cannot be ignored or captured. They are sigkill and sigstop. That is, after receiving the two signals, the process can only accept the default processing by the system, that is, terminate the thread.


> Windows SIGNAl types

# Include <signal. h>

// Ctrl-C Handler
Static int B _ctrl_c = 0;
Static int B _exit_on_ctrl_c = 0;

// Signal types
# Define SIGINT 2 // interrupt (CTRL + C interrupt)
# Define sigill 4 // illegal instruction-invalid function image (invalid command)
# Define sigfpe 8 // floating point exception (floating point exception)
# Define SIGSEGV 11 // segment violation (segment error)
# Define sigterm 5 // software termination signal from kill (software terminated by kill)
# Define sigbreak 21 // Ctrl-break sequence (CTRL + break interrupt)
# Define SIGABRT 22 // Abnormal Termination triggered by abort call (abort)


> Windows SIGNAl handling

The signal processing function is used to set the handler for signal processing.

 

#include <signal.h>

typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);

Signal is used to set the signal capture function. IF signal is called successfully, return the address of the processing function of the previous signal; otherwise, return sig_err.

++ SIGNUM: the signal to be captured,

++ Handler: function pointer, indicating the function to capture the signal. This parameter can also be sig_def (indicating that it is handled by the system by default, so ignore it) or sig_ign (indicating that the signal is ignored without any processing ).

Sighandler_t is a signal capturing function registered by the signal function. After registration, it will be valid throughout the process and can register the same signal capturing function for different signals. The unique input parameter indicates the signal value.


> Small example on msdn
This small example demonstrates how to handle the abort signal.

// Use signal to attach a signal handler to the abort routine
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <tchar.h>

void SignalHandler(int signal)
{
printf("Application aborting...\n");
}

int main()
{
typedef void (*SignalHandlerPointer)(int);

SignalHandlerPointer previousHandler;
previousHandler = signal(SIGABRT, SignalHandler);

abort();
}

> More

It is said that more Nb and more reliableSigactionSince the signal processing mechanism is just getting started, let's stay and learn about it later.

-Piaoger


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.