Example of a hot update configuration parameter (Golang)

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

In the case of not stopping the program, by sending USR1 or USR2 and other semaphores, triggering the parameter update processing of the running program. Of course, you can also deal with semaphores such as Kill,

Let the program handle the exit operation correctly.

As an example, the results are as follows.


You can see that the corresponding program parameters have changed by issuing the KILL-USR1 ID number.

The parameter values for kill can be listed by Kill-l. Various terminals also provide a number of shortcut keys to support the delivery of specific information, such as the most common CTRL + C.

However, note that the definition of the shortcut keys for each terminal may be different, and some can be customized, this should be noted, before use can be detected with stty-a.

The results of the two commands are attached to the test machine:

xcl@xclpc:~$ kill-l 1) SIGHUP 2) SIGINT 3) Sigquit 4) Sigill 5) SIGTRAP 6) SIGABRT 7) Sigbus 8) SIGFPE 9) SIGKILL10) Sigu SR111) SIGSEGV12) SIGUSR213) SIGPIPE14) SIGALRM15) SIGTERM16) SIGSTKFLT17) SIGCHLD18) SIGCONT19) SIGSTOP20) SIGTSTP21) SI GTTIN22) SIGTTOU23) SIGURG24) SIGXCPU25) SIGXFSZ26) SIGVTALRM27) SIGPROF28) SIGWINCH29) SIGIO30) SIGPWR31) SIGSYS34) SIGR TMIN35) sigrtmin+136) sigrtmin+237) sigrtmin+338) sigrtmin+439) sigrtmin+540) sigrtmin+641) sigrtmin+742 ) sigrtmin+944) sigrtmin+1045) sigrtmin+1146) sigrtmin+1247) sigrtmin+1348) sigrtmin+1449) SIGRTMAX-1451) SIGRTMAX-1352) SIGRTMAX-1253) SIGRTMAX-1154) SIGRTMAX-1055) SIGRTMAX-956) SIGRTMAX-857) SIGRTMAX-659) SIGRTMAX-560) SIGRTMAX-461) SIGRTMAX-362) SIGRTMAX-263) SIGRTMAX-164 sigrtmaxxcl@xclpc:~$ 38400 baud; Rows 22; Columns 80; line = 0;intr = ^c; Quit = ^\; erase = ^?; Kill = ^u; EOF = ^d; EOL = m-^?; Eol2 = m-^?; Swtch = m-^?; start = ^q; stop = ^s; Susp = ^z; Rprnt =^r; Werase = ^w;lnext = ^v; flush = ^o; min = 1; Time = 0;-parenb-parodd Cs8 HUPCL-CSTOPB cread-clocal-crtscts-ignbrk brkint-ignpar-parmrk-inpck-istrip-inlcr-ign CR ICRNL IXON-IXOFF-IUCLC ixany imaxbel iutf8opost-olcuc-ocrnl onlcr-onocr-onlret-ofill-ofdel nl0 cr0 tab0 bs0 vt0 Ff0isig Icanon iexten echo Echoe echok-echonl-noflsh-xcase-tostop-echoprtechoctl Echoke
The ^ in the Stty command represents the CTRL key.


Attached to the Go language example source code:

Example of a hot update configuration parameter (Golang)//author:xiong Chuan liang//date:2015-4-11package mainimport ("FMT" "OS" "Os/signal" "Syscall" " Time ") var gconfig stringfunc Main () {quit: = make (chan bool) readconfig () go Signals (quit) go Displayconfig (quit) exit:for { Select {Case <-quit:break exitdefault:}}fmt. PRINTLN ("[Main ()] exit")}func signals (Q chan bool) bool {sigs: = make (chan os). Signal) defer close (sigs) exit:for {Signal. Notify (SIGs, Syscall. Sigquit,syscall. Sigterm,syscall. Sigint,syscall. Sigusr1,syscall. SIGUSR2) SIG: = <-sigsswitch sig {case Syscall. SIGTERM, Syscall. SIGINT, Syscall. Sigquit:fmt. PRINTLN ("[Signals ()] Interrupt ...") Break Exitcase Syscall. Sigusr1:fmt. PRINTLN ("[Signals ()] syscall. SIGUSR1 ") updateconfig () Case Syscall. Sigusr2:fmt. PRINTLN ("[Signals ()] syscall. SIGUSR2 ... ")//updateversion () default:break exit}}q <-truereturn true}func readconfig () {gconfig =" init "FMT. Println ("[Readconfig ()]", Gconfig)}func updateconfig () {gconfig = "Update" FMT. Println ("[Updateconfig ()]", Gconfig)}func DisplaycoNfig (quit Chan bool) {for {select {case <-quit:fmt. Println ("[Displayconfig ()] exit") returndefault:}fmt. Println ("[Displayconfig ()] Config:", Gconfig) time. Sleep (time. Second * 2)}}

blog:http://blog.csdn.net/xcl168



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.