Golang--Signal treatment

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

The system we operate in production requires an elegant exit, that is, after the program receives the exit notification, there is a chance to execute a cleanup code before the finishing touches are finished and then really quits. We use System signal to notify the system to exit, that is, kill Pragram-pid. We set up a processing function for some system signals in the program, and when the signal is received, it executes the relevant cleanup program or notifies each child process to do self-cleanup. Kill-9 Mandatory kill program is unacceptable, which will cause some processing process is forced to break, leaving the unrecoverable scene, causing the message to be destroyed, affecting the next system startup run.

An agent that was recently implemented with Golang also needs an elegant exit, so I try to understand how the system signal is handled in Golang, and share it with you. Golang system signal processing mainly involves the OS package, the Os.signal package, and the Syscall package. The most important of these functions is the Notify function in the signal package:

Func Notify (c chan<-os. Signal, sig ... os. Signal)

The function forwards the system signal received by the process to channel C. Which signals are forwarded is determined by the variable parameters of the function, and if you do not pass in the sig parameter, then notify forwards all signals received by the system to C. If you call notify like this:

Signal. Notify (c, Syscall. SIGINT, Syscall. SIGUSR1, Syscall. SIGUSR2)

Then go will only focus on the type of signal you passed in, and the other signal will be handled by default, mostly process exits. So you need to pass in notify the types of signal you want to focus on and deal with, which is to intercept them and provide custom handlers to change their behavior.

The following is a more complete example:

//Signal.goPackage Mainimport"FMT"Import" Time"Import"OS"Import"os/signal"Import"Syscall"Type Signalhandler func (S OS. Signal, ArgInterface{}) type Signalsetstruct{m map[os. Signal]signalhandler}func signalsetnew () (*Signalset) {ss:=New(signalset) ss.m=Make (Map[os. Signal]signalhandler)returnSs}func (Set*signalset) Register (s OS). Signal, Handler Signalhandler) {if_, Found: =Set. m[s];!found {Set. m[s] =Handler}} Func (Set*signalset) handle (SIG OS. Signal, ARGInterface{}) (err error) {if_, Found: =Set. M[sig]; found {Set. M[sig] (SIG, Arg)returnNil}Else {        returnFmt. Errorf ("No handler available for signal%v", SIG)} Panic ("won ' t reach here")}func Main () {go Syssignalhandledemo () time. Sleep (time. Hour)//Make the main goroutine wait!}func Syssignalhandledemo () {ss:=signalsetnew () handler:= func (S OS). Signal, ARGInterface{}) {Fmt. Printf ("handle signal:%v\n", s)} Ss.register (Syscall. SIGINT, Handler) Ss.register (Syscall. SIGUSR1, Handler) Ss.register (Syscall. SIGUSR2, Handler) for{c:=Make (Chan os. Signal)varsigs []os. Signal forSIG: =range SS.M {sigs=Append (SIGs, SIG)} signal. Notify (c) SIG:= <-C ERR:=Ss.handle (SIG, Nil)if(Err! =Nil) {FMT. Printf ("unknown signal received:%v\n", SIG) Os. Exit (1)        }    }}

In the example above, the Notify function has only one parameter and does not pass in the sig to be followed, so the program forwards all the type signal received to channel C. Build the source file and execute the program:

$> Go build signal.go$> signal

Under another window, execute the following command:

$> ps-ef|grep Signaltonybai  25271  1087  0 16:27 pts/1    00:00:00 signal$> kill-n 2 25271$> kill-n 25271$> Kill 25271

We will see the following output in the first window:

$> signalhandle signal:interrupthandle signal:user defined signal 2unknown signal received:terminated

In Syssignalhandledemo we can also pass in a collection of signal that we care about for notify:

Signal. Notify (c, sigs ...)

This way only the signals in the set can be captured, and the program exits more directly when it receives a signal that is not in the collection. Above is just a demo, just shows that we can capture our attention to the signal, does not reflect the program how to gracefully exit, different procedures to exit the different ways, there is no common method, do not elaborate, your program needs your special design.

Transferred from: http://tonybai.com/2012/09/21/signal-handling-in-go/

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.