The program can be used to perform some tasks on a regular basis, and it can also do the appropriate warning and cleanup when the force is interrupted by the semaphore processing.
Package main//semaphore and timer//author:xiong Chuan liang//date:2015-2-25import "FMT" import "OS" import "os/signal" import "Time" Func Main () {sigs: = Make (chan os. Signal, 1) Done: = Make (chan bool, 1) Signal. Notify (sigs, OS. Interrupt, OS. Kill) go func () {sig: = <-sigsswitch sig {case OS. Interrupt:fmt. Println ("Signal:interrupt") case OS. Kill:fmt. Println ("Signal:kill") default:fmt. Println ("Signal:others")}done <-True} () Fmt. PRINTLN ("Awaiting signal")//main () .... Go jobticker (done) <-doneclose. Exit () fmt. Println ("exiting")}func Jobticker (done <-chan bool) {ticker: = time. Newticker (time. Second) defer ticker. Stop () for {select {case <-done:returncase <-ticker. C:fmt. Println ("Job .....")}}}
If you press CTRL-C, the result is as follows:
Awaiting signaljob......job......signal:interruptinterruptexiting
Go by Example:signals:https://gobyexample.com/signals
MAIL: [Email protected]
blog:http://blog.csdn.net/xcl168
An example of a semaphore and a timer (Golang)