V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
fyyz
V2EX  ›  C++

请问用 Boost::Process 库在 Windows 下如何使子进程在父进程退出后自动退出?

  •  
  •   fyyz · Dec 23, 2019 · 4271 views
    This topic created in 2325 days ago, the information mentioned may be changed or developed.
    windows 的 CreateProcess() 能配合 JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE 使用,实现该功能。
    Boost::Process 有什么方法能做到吗?
    补充:我没有子进程源码,无法修改子进程的可执行文件。
    7 replies    2019-12-23 11:01:13 +08:00
    augustheart
        1
    augustheart  
       Dec 23, 2019
    没用过这个,不过研究一下能不能直接用 api 不行么?
    geelaw
        2
    geelaw  
       Dec 23, 2019 via iPhone
    根据 2017 年的讨论 boost 没有实现该功能。

    https://github.com/klemens-morgenstern/boost-process/issues/66

    你需要自行用 Windows job control 实现。

    为什么 boost 没有该功能?一个简单的原因是 POSIX 中似乎没有这样的 API。
    ysc3839
        3
    ysc3839  
       Dec 23, 2019 via Android
    有个非正规的做法是把进程自己也加入 job 中,这样自己的子进程也会自动加入 job。
    fengjianxinghun
        4
    fengjianxinghun  
       Dec 23, 2019
    @geelaw Linux 有 prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0);
    geelaw
        5
    geelaw  
       Dec 23, 2019 via iPhone
    @ysc3839 #3 这需要假设进程是用 CreateProcessEx 建立的(我觉得 boost 应该是),此外还要在 job 里不允许子进程离开 job。

    @fengjianxinghun #4 我说的是 POSIX,不是 Linux。
    shilyx
        6
    shilyx  
       Dec 23, 2019
    boost 几乎不可能包括 JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE 的功能,因为里面有些坑,boost 也没法从框架上解决。还是自己做吧,反正也是 windows 平台。
    ysc3839
        7
    ysc3839  
       Dec 23, 2019 via Android
    @geelaw 应该不需要某个特定 API 才能生效的,不然的话不是能轻易绕过 job 限制?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2637 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 11:10 · PVG 19:10 · LAX 04:10 · JFK 07:10
    ♥ Do have faith in what you're doing.