Using the new os.Signal

1,076 views
Skip to first unread message

Archos

unread,
Feb 17, 2012, 5:19:43 AM2/17/12
to golang-nuts
The os.Signal's API has changed in the lastest weekly release.

Before, I was using:
==
Restart = make(chan bool)
Stop = make(chan bool)

var sig os.Signal

select {
case <-Restart:
...
case sig = <-signal.Incoming:
switch sig.(os.UnixSignal) {
case syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL:
...
}
case <-Stop:
...
}
==

How convert it to use the new API?

I know that I can use:

var interrupted = make(chan os.Signal)
signal.Notify(interrupted, syscall.SIGQUIT, syscall.SIGINT,
syscall.SIGKILL)

But I'm not sure about trap all different signals

David Symonds

unread,
Feb 17, 2012, 5:47:14 AM2/17/12
to Archos, golang-nuts
On Fri, Feb 17, 2012 at 9:19 PM, Archos <raul...@sent.com> wrote:

> But I'm not sure about trap all different signals

signal.Notify(interrupted), which is what the docs say:

"Notify causes package signal to relay incoming signals to c. If no
signals are listed, all incoming signals will be relayed to c.
Otherwise, just the listed signals will."

Archos

unread,
Feb 17, 2012, 9:56:17 AM2/17/12
to golang-nuts
case <-interrupt:
The another signals are trapped of the same way (from the select). The
only change is remove:

var sig os.Signal
case sig = <-signal.Incoming:
switch sig.(os.UnixSignal) {
case syscall.SIGINT, syscall.SIGTERM,
syscall.SIGKILL:

by:
case <-interrupted:

Russ Cox

unread,
Feb 18, 2012, 8:03:11 PM2/18/12
to Archos, golang-nuts
On Fri, Feb 17, 2012 at 05:19, Archos <raul...@sent.com> wrote:
>        var interrupted = make(chan os.Signal)
>        signal.Notify(interrupted, syscall.SIGQUIT, syscall.SIGINT,
> syscall.SIGKILL)

You should really buffer that channel.


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


Notify causes package signal to relay incoming signals to c. If no
signals are listed, all incoming signals will be relayed to c.
Otherwise, just the listed signals will.

Package signal will not block sending to c: the caller must ensure that
c has sufficient buffer space to keep up with the expected signal rate.
For a channel used for notification of just one signal value, a buffer
of size 1 is sufficient.

minux

unread,
Feb 19, 2012, 12:59:52 AM2/19/12
to Archos, golang-nuts
Another tiny problem: you can't catch SIGKILL.

Rob 'Commander' Pike

unread,
Feb 19, 2012, 1:08:00 AM2/19/12
to minux, Archos, golang-nuts

On 19/02/2012, at 4:59 PM, minux wrote:

Another tiny problem: you can't catch SIGKILL.

Or SIGTAXES.

-rob

Tonic Artos

unread,
Feb 19, 2012, 5:42:51 AM2/19/12
to Rob 'Commander' Pike, golang-nuts, minux, Archos

Be careful not to catch SIGFLU.

Tonic

Archos

unread,
Feb 19, 2012, 5:43:29 AM2/19/12
to golang-nuts
The, since it's necessary to use a buffered channel, and if you need
trap a signal forever, would be "right" to set the channel after of
each caught? instead of use a fixed value

func TrapSize() {
change := make(chan os.Signal, 1)
signal.Notify(change, syscall.SIGWINCH)

go func() {
for {
<-change
// another code
change = make(chan os.Signal, 1) // set the channel again
}
}()
}


On Feb 19, 1:03 am, Russ Cox <r...@golang.org> wrote:
> On Fri, Feb 17, 2012 at 05:19, Archos <raul....@sent.com> wrote:
> >        var interrupted = make(chanos.Signal)
> >        signal.Notify(interrupted, syscall.SIGQUIT, syscall.SIGINT,
> > syscall.SIGKILL)
>
> You should really buffer that channel.
>
> func Notify(c chan<-os.Signal, sig ...os.Signal)

Rémy Oudompheng

unread,
Feb 19, 2012, 7:09:09 AM2/19/12
to Archos, golang-nuts
On 2012/2/19 Archos <raul...@sent.com> wrote:
> The, since it's necessary to use a buffered channel, and if you need
> trap a signal forever, would be "right" to set the channel after of
> each caught? instead of use a fixed value
>
> func TrapSize() {
>        change := make(chan os.Signal, 1)
>        signal.Notify(change, syscall.SIGWINCH)
>
>        go func() {
>                for {
>                        <-change
>                        // another code
>                        change = make(chan os.Signal, 1) // set the channel again
>                }
>        }()
> }

If you replace 'change' by a new channel, it won't receive the signal
notifications.

Rémy.

Reply all
Reply to author
Forward
0 new messages