doulaozhang0238 2014-08-02 12:19
浏览 193
已采纳

如何在Windows的golang中验证父进程已退出?

So I am writing a small utility which should be able to update itself (replace it's own binary).

The best way to do this on Windows seems to be:

  1. Download the new version of the binary as my.exe.new
  2. my.exes runs my.exe.new and exits
  3. my.exe.new waits for my.exe to exit
  4. my.exe.new copies itself as my.exe
  5. my.exe.new starts another copy of itself as my.exe and exits
  6. my.exe waits for my.exe.new to exit
  7. my.exe removes my.exe.new

Now for all of this to work I have to be able to synchronize the state between the processes (being able to know when the parent has exited), but it seems that os.Getppid (nor syscall.Getppid) in golang Windows is not implemented as it always returns -1.

I've seen that patches are underway, but I am not willing to patch my standard libraries.

Is there an easy way to make Getppid working on even older versions of Go (perhaps reimplementing it?), or perhaps anyone can suggest a better method of synchronizing between the process state?

The thing which comes to mind is binding on a socket, but thats a big hacky.

Perhaps passing a pipe to the child process, and the child waiting for the pipe to close?

Thanks

  • 写回答

2条回答 默认 最新

  • doulin2947 2014-08-02 23:54
    关注

    The parent process can pass its PID to the child.

    You could use a command-line parameter or an environment variable to do this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要