[plt-scheme] current process id

From: David J. Neu (djneu at att.net)
Date: Fri Feb 6 06:35:33 EST 2004

Eli,

Thanks for the info - your suggestion seemed to work well.

I thought I'd try to write an extension to get the current process id.
Here's what I came up with by adapting one of the examples provided in
the mzscheme distribution.  I've also included a test program.

No promises on it being, correct (if someone sees a mistake-please let
me know), but it seems to work (on Debian woody) and I thought someone
else might be able use it.

--David


; ----------<start: getpidtest.ss>----------
(load-extension "getpid.so")

(display (getpid))

(newline)

; for Linux - from Eli Barzilay
(display (string->number (resolve-path "/proc/self")))

; for Sun - from Eli Barzilay
; (display (string->number (resolve-path "/proc/self")))

; for FreeBSD - from Eli Barzilay
; (display (string->number (resolve-path "/proc/curproc")))
; ----------<end: getpidtest.ss>----------


/* **********<start: getpid.c>**********
/*
  Extension that defines getpid to get the current process id.

  Can be called in Scheme using:
  > (getpid)

  Create using:
  mzc --cc getpid.c
  mzc --ld getpid.so getpid.o
*/

#include "escheme.h"

static Scheme_Object *sch_getpid(int argc, Scheme_Object **argv)
{
  return scheme_make_integer( getpid() );
}

Scheme_Object *scheme_reload(Scheme_Env *env)
{
  Scheme_Object *proc;

  proc = scheme_make_prim_w_arity(sch_getpid, "getpid", 0, 0);

  scheme_add_global("getpid", proc, env);

  return scheme_void;
}

Scheme_Object *scheme_initialize(Scheme_Env *env)
{
  return scheme_reload(env);
}

Scheme_Object *scheme_module_name()
{
  return scheme_false;
}
/* **********<end: getpid.c>**********


On Thu, Feb 05, 2004 at 02:32:55PM -0500, Eli Barzilay wrote:
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 
> On Feb  5, David J. Neu wrote:
> > Hi all,
> > 
> > I'm wondering if there is a way to determine the current process id
> > from within a mzscheme program?
> 
> I don't think that there is any way to do it completely from inside
> MzScheme (it would probably be a useful extension).  Anyway, if you're
> using Linux, a simple way to get the current pid is:
> 
>   (string->number (resolve-path "/proc/self"))
> 
> I found similar tricks for Sun (cd into /proc/self, then check the
> current directory), and FreeBSD (use "/proc/curproc").
> 
> -- 
>           ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
>                   http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.