Using signal function to handle the signal of CTRL + C under Linux

Source: Internet
Author: User
Tags signal handler sleep function

Preface

It is a very useful programming method to realize the soft interrupt of the program through the signal mechanism under Linux. When we press CTRL-C, ctrl-z or kill a process when the program is running, it is actually equivalent to sending a specific signal to the process, and when the process captures the signal, the process is interrupted and immediately jumps to the signal handler function. By default, the signal (SIGINT) that a program sends to CTRL-C is the exit process, so when we press CTRL-C, we can terminate the operation of a process.

signal Function

but sometimes we want our program to perform some specific finishing processes before the signal is terminated, or we want our program to be able to perform our own defined interrupt operations after receiving a certain signal, and under Linux we can the signal function implements the above functions.

For example: write a program under Linux, if there is a dead loop in the program, we should press CTRL + C on the keyboard to terminate our program, then we can also capture this signal, and then execute our own signal processing program, output some useful information to help us debug the program, It's kind of a trick, too. If we do not capture this signal, then the signal is generated to execute the OS signal handler. Signals and interrupts are similar, we can either use an OS interrupt handler, or intercept interrupts to execute their own interrupt handlers. Here is an example of multithreading:

#include <stdio.h> #include <pthread.h> #include <unistd.h> #include <sys/wait.h> #include < Sys/types.h> #define TRUE 1void * One (void * No) {    while (true)    {    printf ("nuaacs1\n");    Sleep (1);}    } void * (void * No) {    while (true)    {    printf ("nuaacs2\n");    Sleep (1);}    } void Stop (int signo) {    printf ("oops! Stop!!! \ n ");    _exit (0);} int main () {    int res;    pthread_t A, B;    Signal (SIGINT, Stop);     res = pthread_create (&a, NULL, one, null);    res = pthread_create (&b, NULL, both, NULL);    res = Pthread_join (A, NULL);    res = Pthread_join (B, NULL);    return 0;}


The above is the online information, but it is also important to note that the program is interrupted and after execution of the interrupt function, which is returned in the interrupt function, then the program will return to the position before the break to continue to execute the previous program. The signal handler function can only return void and cannot return the specified argument.

In addition, for some functions, the program interrupts may interrupt the normal execution of these functions, for example, for the sleep function, if a program is interrupted on the way to sleep then the program will immediately end sleep.

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.