Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-05

how to find out if a TProcess was killed

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mattias Gaertner

Hi,

If I start a TProcess and the process is being killed by the system (for example, because it runs amok and allocates too much mem) the ExitStatus is
0. How can I find out, that the program was killed?

Mattias

Michael Van Canneyt
to Mattias Gaertner
Mattias Gaertner wrote 5 lines
Hi, If I start a TProcess and the process is being killed by the system (for example, because it runs amok and allocates too much mem) the ExitStatus is 0. How can I find out, that the program was killed?

The exit status are the high bits of a word describing how a process exited. The Wifsignaled function tells you whether the process was signaled, and wtermsig tells you what signal caused the exit. TProcess (being cross-platform) doesn't support this.

Here are the relevant functions (unit unix, 1.1 branch):

function WEXITSTATUS(Status: Integer): Integer;
begin
  WEXITSTATUS:=(Status and $FF00) shr 8;
end;
function WTERMSIG(Status: Integer): Integer;
begin
  WTERMSIG:=(Status and $7F);
end;
function WSTOPSIG(Status: Integer): Integer;
begin
  WSTOPSIG:=WEXITSTATUS(Status);
end;
Function WIFEXITED(Status: Integer): Boolean;
begin
  WIFEXITED:=(WTERMSIG(Status)=0);
end;
Function WIFSTOPPED(Status: Integer): Boolean;
begin
  WIFSTOPPED:=((Status and $FF)=$7F);
end;
Function WIFSIGNALED(Status: Integer): Boolean;
begin
  WIFSIGNALED:=(not WIFSTOPPED(Status)) and
               (not WIFEXITED(Status));
end;
Function W_EXITCODE(ReturnCode, Signal: Integer): Integer;
begin
  W_EXITCODE:=(ReturnCode shl 8) or Signal;
end;
Function W_STOPCODE(Signal: Integer): Integer;
begin
  W_STOPCODE:=(Signal shl 8) or $7F;
end;

If you have suggestions for adding support for these functions to TProcess, let me know.

Michael.