DaanDeMeyer / reproc

A cross-platform (C99/C++11) process library
MIT License
552 stars 65 forks source link

Correct api for stopping a process cleanly or killing it? #51

Closed cmaughan closed 3 years ago

cmaughan commented 3 years ago

If I have a process that I suspect has already exited, but want to kill it if not, is this the right sequence? i.e. Is the noop the 'exit' if already exited part? Followed by terminate and kill?

reproc::stop_actions stop = {
            { reproc::stop::noop, reproc::milliseconds(0) },
            { reproc::stop::terminate, reproc::milliseconds(TerminateProcessMilliseconds) },
            { reproc::stop::kill, reproc::milliseconds(KillProcessMilliseconds) }
        };
proc->stop(stop);

It wasn't clear to me from the examples the correct approach here. The scenario is a process that has been running for a while, but has been asked to exit by another mechanism (it has received a network message, for example). So I 'hope' that it has already gone, but if not, I wish to close it.

DaanDeMeyer commented 3 years ago

Replace the noop with reproc::stop::wait. Waiting 0 milliseconds is equivalent to just checking if the process is still running.

cmaughan commented 3 years ago

Thanks for the quick response ;) So what does the noop mean?

DaanDeMeyer commented 3 years ago

To do absolutely nothing at all. stop_actions is a struct with three members but it's not required to supply three actions. Sometimes a single action is sufficient so we need an action to indicate that nothing should happen which is noop. Usually you don't specify it explicitly as noop is the default action.

For example:

  reproc::stop_actions stop = {
    { reproc::stop::terminate, reproc::milliseconds(5000) },
    { reproc::stop::kill, reproc::milliseconds(2000) },
    {}
  };

The last action is not necessary so we just default initialize it which corresponds to the noop action as it is the first member of the stop enum.

cmaughan commented 3 years ago

OK, that's great, and makes perfect sense. Thanks.

DaanDeMeyer commented 3 years ago

I enabled Github Discussions for reproc, please post any further questions on using reproc there

cmaughan commented 3 years ago

Will do, thanks for the help :)