Skip to content

Instantly share code, notes, and snippets.

@makeittotop
Created March 23, 2015 14:23
Show Gist options
  • Select an option

  • Save makeittotop/dade10f88d7f3a477f22 to your computer and use it in GitHub Desktop.

Select an option

Save makeittotop/dade10f88d7f3a477f22 to your computer and use it in GitHub Desktop.
select function and signal handling in a single threaded go code
package main
import (
"fmt"
"time"
"os"
"os/signal"
"syscall"
)
func main() {
var count int = 1
sig := make(chan os.Signal, 1)
//signal.Notify(sig, syscall.SIGINT, syscall.SIGUSR2, syscall.SIGUSR1)
signal.Notify(sig)
go func() {
var intr_count int = 0
for {
select {
case sig1 := <-sig:
fmt.Printf("Received signal %d\n", sig1)
if sig1 == syscall.SIGUSR2 || sig1 == syscall.SIGUSR1 {
fmt.Printf("Resetting counter to 1\n")
count = 1
} else if sig1 == syscall.SIGINT {
intr_count += 1
if intr_count == 1 {
fmt.Printf("Hit Ctrl-c again to quit\n")
} else {
fmt.Printf("Quitting\n")
os.Exit(0)
}
}
default:
}
fmt.Printf("count:= %d\n", count)
count += 1
time.Sleep(1*time.Second)
}
} ()
var input string
# To pre-empt the main thread
fmt.Scanln(&input)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment