> For the complete documentation index, see [llms.txt](https://wskdsgcf.gitbook.io/mastering-go-zh-cn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wskdsgcf.gitbook.io/mastering-go-zh-cn/08.0/08.14/08.14.1.md).

# 08.14.1 处理两种信号

在本小节中，你将学习如何在`Go`程序中处理两种信号，代码见`handleTwo.go`,分为四部分。`handleTwo.go`处理的信号是`SIGINFO`和`SIGINT`，在Golang中称为`syscall.SIGINFO`和`os.Interrupt`。

> *如果你查看`os`包文档，会发现在所有系统上只保证存在两个`siganal`，分别是`syscall.SIGKILL`和`syscall.SIGINT`，在`Go`中也定义为`os.Kill`和`os.Interrupt`。*

`handleTwo.go`第一部分包含如下代码：

```go
package main

import (
    "fmt"
    "os"
    "os/signal"
    "syscall"
    "time"
)
```

`handleTwo.go`第二部分代码如下：

```go
func handleSignal(signal os.Signal) {
    fmt.Println("handleSignal() Caught:", signal)
}
```

`handleSignal`函数用于处理`syscall.SIGINFO`信号，而`os.interrupt`信号将被内联处理。 `handleTwo.go`第三部分代码如下：

```go
func main() {
    sigs := make(chan os.Signal, 1)
    signal.Notify(sigs, os.Interrupt, syscall.SIGINFO)

    go func() {
        for {
            sig := <-sigs
            switch sig {
            case os.Interrupt:
                fmt.Println("Caught:", sig)
            case syscall.SIGINFO:
                handleSignal(sig)
                return
            }
        }
    }()
```

本技术工作原理如下：首先，你需要定义一个通道`sigs`用于传递数据。然后调用`signal.Notify()`声明你感兴趣的信号。下一步，你实现一个匿名函数，作为`goroutine`运行以便在收到关心的任何一个信号时进行操作。你需要等待`Chapter 9,Go Concurrency-Goroutines,Channels,and Pipelines`，学习`goroutine`和`channels`。

`handleTwo.go`最后一部分程序如下：

```go
    for {
        fmt.Printf(".")
        time.Sleep(20 * time.Second)
    }
}
```

`time.Sleep()`调用用于阻止程序结束。在实际应用中，不需要使用类似代码。

在调用`kill(1)`时，我们需要程序的进程`ID`，我们首先编译`handleTwo.go`，并运行可执行文件，而不是`go run handleTwo.go`。`handleTwo`输出如下：

```
$ go build handleTwo.go
$ ls -l handleTwo
-rwxr-xr-x 1 mtsouk staff 2005200 Jan 18 07:49 handleTwo
$ ./handleTwo
.^CCaught: interrupt
.Caught: interrupt
handleSignal() Caught: information request
.Killed:9
```

注意你需要另一个终端和`handleTwo.go`交互，并获取输出。在终端执行命令如下：

```
$ ps ax | grep ./handleTwo | grep -v grep
47988 s003 S+  0:00.00 ./handleTwo
$ kill -s INT 47988
$ kill -s INFO 47988
$ kill -s USR1 47988
$ kill -9 47988
```

第一条命令用于查找`handleTwo`的进程`ID`，剩余的命令用于向进程发送信号。信号`SIGUSR1`被忽略了，在输出中没有显示。

`handleTwo.go`的问题是，如果它得到一个未被编程处理的信号，它将忽略它。因此，在下一节中，你将看到一种使用相对不同的方法以更有效的方式处理信号的技术。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wskdsgcf.gitbook.io/mastering-go-zh-cn/08.0/08.14/08.14.1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
