gpt4 book ai didi

linux - 在 Go 中获取管道状态

转载 作者:IT王子 更新时间:2023-10-29 01:48:08 28 4
gpt4 key购买 nike

我无法在 Go (1.5) 中获取管道状态。

mkfifo 上书写时创建管道,我尝试获取此输出管道的状态:

  • 使用Write返回状态 EPIPE
  • 使用Write返回状态 EPIPEsignal.Ignore在 SIGPIPE 上(以防万一)
  • 使用signal.Notify在 SIGPIPE 上

我可以看到:

  • EPIPE永远不会回来
  • 当我使用 kill -13 时,信号处理程序称为:“获得信号:破管”
  • 当我 ctrl-c读者,信号处理程序没有被调用,我的程序退出并输出:“信号:破损的管道”

请指出我的错误好吗?

// tee.go
package main

import (
"fmt"
"os"
"os/signal"
"syscall"

sys "golang.org/x/sys/unix"
)

// wait for a signal and print it
func handleSignal(csig chan os.Signal) {
for {
fmt.Println("Wait signal")
s := <-csig
fmt.Println("Got signal:", s)
}
}

func main() {
csig := make(chan os.Signal, 1)

// `kill -13` outputs "Got signal: broken pipe" => ok
signal.Notify(csig, sys.SIGPIPE)

// OR disable the previous `Notify` just to be sure ?
// maybe it will help to get the EPIPE error status on `Write` ?
//signal.Ignore(sys.SIGPIPE)

go handleSignal(csig)

// open previously created named pipe (`mkfifo /tmp/test`)
pipe, _ := os.OpenFile("/tmp/test", os.O_WRONLY, 0)

for {
_, err := pipe.Write([]byte("foo\n"))
if err == syscall.EPIPE {
// never called => ko
fmt.Println("EPIPE error")
}
}
}

注意:作为一个简单的 Go 练习,我尝试实现一个几乎类似于 tee -a <a_file> 的命令(将 stdin 打印到 stdout 和 <a_file> )具有以下特性: non blocking write on a named pipe and optional reader .

最佳答案

返回的错误不是普通的 syscall.Error,而是包含在 *os.PathError 中,如下面的代码变体所示:

package main

import (
"fmt"
"os"
"syscall"
)

func main() {
// open previously created named pipe (`mkfifo /tmp/test`)
pipe, _ := os.OpenFile("/tmp/test", os.O_WRONLY, 0)
for {
n, err := pipe.Write([]byte("foo\n"))
fmt.Printf("write: n=%v, err=(%T) %[2]v\n", n, err)
if err == syscall.EPIPE {
fmt.Println("EPIPE error")
} else if perr, ok := err.(*os.PathError); ok {
fmt.Printf("op: %q; path=%q; err=(%T) %[3]q\n",
perr.Op, perr.Path, perr.Err)
if perr.Err == syscall.EPIPE {
fmt.Println("os.PathError.Err is EPIPE")
}
}
}
}

在完成 mkfifo/tmp/test 之后运行它; head/tmp/test 别处给我:

write: n=4, err=(<nil>) <nil>
[… repeated nine more times, as the head command reads ten lines …]
write: n=0, err=(*os.PathError) write /tmp/test: broken pipe
op: "write"; path="/tmp/test"; err=(syscall.Errno) "broken pipe"
os.PathError.Err is EPIPE
[… above three lines repeated nine more times …]
signal: broken pipe
Exit 1

在单个文件上返回十个管道错误后,Go runtine 停止捕获/阻止 SIGPIPE 并让它通过您的程序将其杀死。我不相信 Go 运行时允许您捕获或忽略 SIGPIPE,因为它在内部使用该信号本身。

所以有两件事:第一,要查找 syscall.EPIPE,您需要检查 *os.PathError,如图所示;第二,当 err != nil 当你没有实际处理错误时。

我不知道为什么 Go 会这样处理 SIGPIPE 的细节;也许搜索 Go 的错误跟踪器和/或 go-nuts 列表可能有助于回答这个问题。随着 Go 1.5 对 os/signal 包的添加,执行 signal.Reset(syscall.SIGPIPE) (在任何 signal.Notify 调用之前)改变这种行为。

关于linux - 在 Go 中获取管道状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32168006/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com