如何开始一个过程,使其“独立” [英] How to start a process and make it 'independent'

查看:118
本文介绍了如何开始一个过程,使其“独立”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这两个事实: -



CreateProcess()启动一个进程,你的程序在运行时继续运行,但是当你的程序完成时,子进程会随之下降。此外,您的程序必须注意子进程何时退出,以便它可以正确释放进程句柄。



system() code>启动一个进程,等待它完成,然后继续你的程序。




  • 我需要知道的是如何启动一个进程,让它独立于我的程序运行,并在我的程序退出后继续。我不需要捕获它的输出或有任何进一步的控制,我只是想让用户与它交互 - 就像说我想写一个替代的开始菜单运行命令。
  • $ b $

    解决方案

    CreateProcess()默认不等待子进程完成,它立即返回。如果你想等待,请使用WaitForSingleObject。
    不,当父进程存在时,子进程不会自动终止,并且操作系统自动释放句柄,而不需要自己做(如果你使用现代版本的windows)。



    与任何操作系统资源(gdi对象,用户对象,内核对象)一样,如果你不自己销毁/释放/关闭资源,那么在应用程序运行时会有资源泄露。您应该在CreateProcess返回后立即关闭从CreateProcess返回的两个句柄。


    Assuming I've got these two facts right :-

    CreateProcess() starts a process and lets your program continue while it runs, but when your program finishes, the child process goes down with it. Furthermore, your program has to take note of when the child process exits so that it can properly release the process handle.

    system() starts a process, waits for it to complete then continues with your program.

    • what I need to know is how to start a process and let it run independently of my program, and persist after my program exits. I don't need to capture its output or have any further control over it, I just want to let the user interact with it - like say I wanted to write an alternative to the Start Menu Run command.

    So is it, please, actually possible to do that?

    解决方案

    CreateProcess() does not wait for child process to finish by default, it returns immediately. Use WaitForSingleObject if you want to wait. No, the child process is not killed automatically when parent exists, and the handle is anyway freed automatically by the operating system, not need to do it yourself (if you use a modern version of windows).

    Just like any OS resource(gdi objects, user objects, kernel objects), if you don't destroy/release/close the resource yourself then you will have resource leaks while your application runs. You should close both handles returned from CreateProcess soon after CreateProcess returns.

    这篇关于如何开始一个过程,使其“独立”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆