function [name, alive] = dct_psname(pid)
%DCT_PSNAME - retrieve the name of a process from its PID
%   [name, alive] = DCT_PSNAME( pid ) retrieves the process name from the
%   system in a system-dependent way (via DCT_PSFCNS). If the pid is found not
%   to be alive, then the "alive" flag will be false, and the name will be
%   empty.

%  Copyright 2006-2019 The MathWorks, Inc.

alive = dct_psfcns('isalive', pid);
name  = '';

if ~alive
    return;
end

% Use the dct_psfcns MEX file
try
    name = strtrim(dct_psfcns('procname', pid));
catch err
    if dct_psfcns( 'isalive', pid )
        % Couldn't calculate name of living process
        error(message('parallel:internal:cluster:ProcessNameError', pid, err.message));
    else
        alive = false;
        name  = '';
    end
end
