[fpc-pascal] check if process ID is running under BSD

Marco van de Voort marcov at stack.nl
Wed Jun 8 12:58:55 CEST 2011

In our previous episode, Mattias Gaertner said:
> > I don't know if it exist a FPC function for that.
> > 
> > i'll do an external commad like " ps -ef | grep bash | grep -v grep | awk 
> > '{print $8}'".
> Thanks, I knew that.
> I prefer a more direct way - for example via a sysctl.
> I need to check under BSD if a PID is running and what name it has.
> Is there already a function for that?

2.5.1 since r17692 try this:

uses freebsd,baseunix,sysutils;

var pid : pid_t;
    s: ansistring;
  writeln('pid (-1 for current):',pid);
  // loads name of process + args with #0's inbetween.
  if kernproc_getargs(pid,s)<>-1 then // does not work with -1
    writeln('error fetching arguments:',fpgeterrno);

  // loads full path of running binary
  if kernproc_getpath(pid,s)<>-1 then   // -1 means own process
    writeln('error fetching path:',fpgeterrno);

See the implementation of the above procedures in freebsd.pas for more info,
note that the kern_proc_* values were also updated in sysctl.

More information about the fpc-pascal mailing list