View source code Display the source code in std/process.d from which this page was generated on github. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

Function std.process.kill

Attempts to terminate the process associated with pid.

The effect of this function, as well as the meaning of codeOrSignal, is highly platform dependent. Details are given below. Common to all platforms is that this function only initiates termination of the process, and returns immediately. It does not wait for the process to end, nor does it guarantee that the process does in fact get terminated.

Always call wait to wait for a process to complete, even if kill has been called on it.

Prototypes

void kill(
  Pid pid
);

void kill(
  Pid pid,
  int codeOrSignal
);

Windows specific

The process will be forcefully and abruptly terminated. If codeOrSignal is specified, it must be a nonnegative number which will be used as the exit code of the process. If not, the process wil exit with code 1. Do not use codeOrSignal = 259, as this is a special value (aka. STILL_ACTIVE) used by Windows to signal that a process has in fact not terminated yet.

auto pid = spawnProcess("some_app");
kill(pid, 10);
assert (wait(pid) == 10);

POSIX specific

A signal will be sent to the process, whose value is given by codeOrSignal. Depending on the signal sent, this may or may not terminate the process. Symbolic constants for various POSIX signals are defined in core.sys.posix.signal, which corresponds to the signal.h POSIX header. If codeOrSignal is omitted, the SIGTERM signal will be sent. (This matches the behaviour of the kill shell command.)

import core.sys.posix.signal: SIGKILL;
auto pid = spawnProcess("some_app");
kill(pid, SIGKILL);
assert (wait(pid) == -SIGKILL); // Negative return value on POSIX!

Throws

ProcessException on error (e.g. if codeOrSignal is invalid). Note that failure to terminate the process is considered a "normal" outcome, not an error.

Authors

Lars Tandle Kyllingstad, Steven Schveighoffer, Vladimir Panteleev

License

Boost License 1.0.

Comments