source: trunk/abcl/src/org/armedbear/lisp/get-pid.lisp

Last change on this file was 15569, checked in by Mark Evenson, 2 years ago

Untabify en masse

Results of running style.org source blocks on tree

File size: 1.6 KB
Line 
1(in-package :extensions)
2
3(export '(get-pid)
4        :extensions)
5
6(defun get-pid ()
7  "Get the process identifier of this lisp process.
8
9Used to be in SLIME but generally useful, so now back in ABCL proper."
10  (handler-case
11      (let* ((runtime
12              (java::jstatic "getRuntime" "java.lang.Runtime"))
13             (command
14              (java::jnew-array-from-array
15               "java.lang.String" #("sh" "-c" "echo $PPID")))
16             (runtime-exec-jmethod
17              ;; Complicated because java.lang.Runtime.exec() is
18              ;; overloaded on a non-primitive type (array of
19              ;; java.lang.String), so we have to use the actual
20              ;; parameter instance to get java.lang.Class
21              (java::jmethod "java.lang.Runtime" "exec"
22                            (java::jcall
23                             (java::jmethod "java.lang.Object" "getClass")
24                             command)))
25             (process
26              (java::jcall runtime-exec-jmethod runtime command))
27             (output
28              (java::jcall (java::jmethod "java.lang.Process" "getInputStream")
29                          process)))
30         (java::jcall (java::jmethod "java.lang.Process" "waitFor")
31                     process)
32         (loop :with b :do
33            (setq b
34                  (java::jcall (java::jmethod "java.io.InputStream" "read")
35                              output))
36            :until (member b '(-1 #x0a))        ; Either EOF or LF
37            :collecting (code-char b) :into result
38            :finally (return
39                       (parse-integer (coerce result 'string)))))
40    (t () 0)))
41
42
43
Note: See TracBrowser for help on using the repository browser.