dongzg2006 2013-12-23 12:58
浏览 83
已采纳

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

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条回答 默认 最新

  • doufan9290 2013-12-23 14:01
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?