From 09fa2827ff84bb77216be0a76cd784f27dbdb076 Mon Sep 17 00:00:00 2001 From: Alexander Belopolsky Date: Mon, 9 Oct 2017 12:09:44 -0400 Subject: [PATCH] Implemented `getpid(p::Process)`. Closes # 4752. --- base/process.jl | 14 ++++++++++++++ src/jl_uv.c | 1 + test/spawn.jl | 10 ++++++++++ 3 files changed, 25 insertions(+) diff --git a/base/process.jl b/base/process.jl index cc188a084e6752..b531d86d07bcb6 100644 --- a/base/process.jl +++ b/base/process.jl @@ -335,6 +335,20 @@ end pipe_reader(p::Process) = p.out pipe_writer(p::Process) = p.in +""" + getpid(p::Process) -> Int32 + +Get the process ID of a running process. Returns `-1` if the +process is not running. +""" +function Base.getpid(p::Process) + if p.handle != C_NULL + ccall(:jl_uv_process_pid, Cint, (Ptr{Void},), p.handle) + else + Cint(-1) + end +end + struct ProcessChain <: AbstractPipe processes::Vector{Process} in::Redirectable diff --git a/src/jl_uv.c b/src/jl_uv.c index 77719693eb943e..66399bd2c72468 100644 --- a/src/jl_uv.c +++ b/src/jl_uv.c @@ -112,6 +112,7 @@ static void jl_uv_shutdownCallback(uv_shutdown_t *req, int status) } // getters and setters +JL_DLLEXPORT int jl_uv_process_pid(uv_process_t *p) { return p->pid; } JL_DLLEXPORT void *jl_uv_process_data(uv_process_t *p) { return p->data; } JL_DLLEXPORT void *jl_uv_buf_base(const uv_buf_t *buf) { return buf->base; } JL_DLLEXPORT size_t jl_uv_buf_len(const uv_buf_t *buf) { return buf->len; } diff --git a/test/spawn.jl b/test/spawn.jl index 3ebeb130ab500d..7202c82a0d8de7 100644 --- a/test/spawn.jl +++ b/test/spawn.jl @@ -220,6 +220,16 @@ if valgrind_off @test success(proc) end +# issue #4752 +let out = Pipe(), proc, pid + proc = spawn(pipeline(`$exename --startup-file=no -e 'println(STDERR,getpid())'`, stderr = out)) + pid = getpid(proc) + close(out.in) + @test parse(Int32, read(out, String)) == pid + @test success(proc) + @test getpid(proc) == -1 +end + # issue #6310 @test read(pipeline(`$echocmd "2+2"`, `$exename --startup-file=no`), String) == "4\n"