time.sleep在分叉后冻结程序(Go)

huangapple go评论75阅读模式
英文:

time.sleep freezes the program after forking (Go)

问题

我正在使用以下函数将程序切换到“守护进程”模式:

func daemon(nochdir, noclose int) int {
    ret, _, err := syscall.Syscall(syscall.SYS_FORK, 0, 0, 0)
    if err != 0 {
        maybe_exit_err("Forking", err)
        return -1
    }
    switch ret {
    case 0:
        break
    default:
        os.Exit(0)
    }

    sid, err2 := syscall.Setsid()
    maybe_exit_err(fmt.Sprintf("could not set session: %s", sid), err2)
    if sid == -1 {
        return -1
    }
    if nochdir == 0 {
        os.Chdir("/")
    }
    if noclose == 0 {
        f, e := os.OpenFile("/dev/null", os.O_RDWR, 0)
        if e == nil {
            fd := int(f.Fd())
            syscall.Dup2(fd, int(os.Stdin.Fd()))
            syscall.Dup2(fd, int(os.Stdout.Fd()))
            syscall.Dup2(fd, int(os.Stderr.Fd()))
        }
    }
    return 0
}

只要我不使用time.Sleep(1 * time.Second),程序就能正常工作。但是,一旦使用了time.Sleep,程序就会停在那里什么都不做。我甚至在一个我自己打开的文件中添加了一个循环写入测试字符串的操作,但在使用time.Sleep后(如果不使用则正常工作),文件中没有任何内容被写入。

这可能是什么原因呢?

操作系统:Debian Linux 7.2 x64
Go 版本:1.2

英文:

I am forking into "daemon" mode with this function:

func daemon(nochdir, noclose int) int {

        ret, _, err := syscall.Syscall(syscall.SYS_FORK, 0, 0, 0)
        if err != 0 {
                maybe_exit_err("Forking", err)
                return -1
        }
        switch ret {
        case 0:
                break
        default:
                os.Exit(0)
        }

        sid, err2 := syscall.Setsid()
        maybe_exit_err(fmt.Sprintf("could not set session: %s", sid), err2)
        if sid == -1 {
                return -1
        }
        if nochdir == 0 {
                os.Chdir("/")
        }
        if noclose == 0 {
                f, e := os.OpenFile("/dev/null", os.O_RDWR, 0)
                if e == nil {
                        fd := int(f.Fd())
                        syscall.Dup2(fd, int(os.Stdin.Fd()))
                        syscall.Dup2(fd, int(os.Stdout.Fd()))
                        syscall.Dup2(fd, int(os.Stderr.Fd()))
                }
        }
        return 0
}

The program works fine as long as I do not use time.Sleep(1 * time.Second). After that, it just sits there doing nothing, I have even added writing test string in a loop to a file I opened myself, and nothing is being written there after time.Sleep is used (and it works fine if not).

What might be the reason here?

OS: Debian Linux 7.2 x64
go version: 1.2

答案1

得分: 1

看起来我遇到了一个与运行时相关的一般问题:https://code.google.com/p/go/issues/detail?id=6664

更新:

G++1:https://github.com/sevlyar/go-daemon

SO--1

英文:

It seems I've ran into general issue with runtime: https://code.google.com/p/go/issues/detail?id=6664

UPDATE:

G++1 : https://github.com/sevlyar/go-daemon

SO--1

huangapple
  • 本文由 发表于 2013年12月23日 20:58:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/20744105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定