How the Go program exits safely (CTRL + C)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

If the command-line program needs to exit, CTRL+C it is the most straightforward method.

How to handle C languageCTRL+C

CTRL+CThe interrupt signal is sent to the command-line process, and functions in the C language <signal.h> signal can register the signal's handler function.

signalThe signature of the function is as follows:

void (*signal(int sig, void (*func)(int)))(int);

For example, we have to deal with CTRL+C the corresponding SIGINT signal:

#include <stdio.h>#include <stdlib.h>#include <signal.h>void sigHandle(int sig) {    switch(sig) {    case SIGINT:        printf("sigHandle: %d, SIGINT\n", sig);        break;    default:        printf("sigHandle: %d, OTHER\n", sig);        break;    }    exit(1);}int main() {    signal(SIGINT, sigHandle);    for(;;) {}    return 0;}

After compiling and running the program, it goes into a dead loop, and by CTRL+C force exit you will see the following output:

sigHandle: 2, SIGINT

Of course, there is no way to get a signal directly from the process management kill program.

<signal.h>In addition to the signal function, there is a raise function to generate the signal:

int raise(int sig);

After we sigHandle intercept the signal, we can use the function if we want to restore the signal again raise . However, be careful not to cause infinite recursive signal/raise calls.

How the go language is handledCTRL+C

The go language also has a similar function signal.Notify (in the os/signal package) that can filter the signal.

This is an signal.Notify example of a self-bring:

// Set up channel on which to send signal notifications.// We must use a buffered channel or risk missing the signal// if we're not ready to receive when the signal is sent.c := make(chan os.Signal, 1)signal.Notify(c, os.Interrupt, os.Kill)// Block until a signal is received.s := <-cfmt.Println("Got signal:", s)

signal.NotifyThe signal that the user is interested in is forwarded to the channel c , and the channel c cannot be blocked. If the channel is not buffered enough, the signal may be lost. If we don't forward the signal again, it will be set to 1 buffer size.

signal.Notifyis a variable parameter from the second parameter and is used to specify the signal to filter.
If you do not specify a second parameter, the default is to filter all signals.

The signal is defined in general syscall . syscall Packages are system-dependent,
Different operating system signals may vary. However syscall.SIGINT syscall.SIGKILL , it is consistent with each system and corresponds to each other os.Interrupt os.Kill .

The following is a complete example of the Go language version:

package mainimport (    "fmt"    "os"    "os/signal")func main() {    c := make(chan os.Signal, 1)    signal.Notify(c, os.Interrupt, os.Kill)    s := <-c    fmt.Println("Got signal:", s)}

go run signal.goRun into a dead loop and press CTRL+C force to exit to see the following output:

Got signal: interrupt

Of course, there is no way to get a signal directly from the process management kill program.

If you want to restore the signal, call. s.Signal() if you want to stop the filtering of the signal, call signal.Stop(c) .

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.