Skip to content

Instantly share code, notes, and snippets.

@mthomure
Created June 26, 2016 07:39
Show Gist options
  • Save mthomure/dbaf8be9359b857f3f344bf4002e46d9 to your computer and use it in GitHub Desktop.
Save mthomure/dbaf8be9359b857f3f344bf4002e46d9 to your computer and use it in GitHub Desktop.
(ns getpid
(:import [jnr.ffi.types pid_t]
[jnr.ffi LibraryLoader]))
;; see https://github.com/jnr/jnr-ffi-examples/blob/master/getpid/src/main/java/getpid/Getpid.java
;; using dependency: [com.github.jnr/jnr-ffi "2.0.9"]
;; public interface LibC {
;; public @pid_t long getpid();
;; public @pid_t long getppid();
;; }
(definterface LibC
(^{pid_t true :tag long} getpid [])
(^{pid_t true :tag long} getppid []))
;; LibC libc = LibraryLoader.create(LibC.class).load("c");
;; System.out.println("pid=" + libc.getpid() + " parent pid=" + libc.getppid());
(defn -main [& args]
(let [libc (.load (LibraryLoader/create LibC) "c")
pid (.getpid libc)
ppid (.getppid libc)]
(println (format "found pid: %d, ppid: %d" pid ppid))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment