Skip to content

Commit 09fa282

Browse files
committed
Implemented getpid(p::Process).
Closes # 4752.
1 parent 49c066b commit 09fa282

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

base/process.jl

+14
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,20 @@ end
335335
pipe_reader(p::Process) = p.out
336336
pipe_writer(p::Process) = p.in
337337

338+
"""
339+
getpid(p::Process) -> Int32
340+
341+
Get the process ID of a running process. Returns `-1` if the
342+
process is not running.
343+
"""
344+
function Base.getpid(p::Process)
345+
if p.handle != C_NULL
346+
ccall(:jl_uv_process_pid, Cint, (Ptr{Void},), p.handle)
347+
else
348+
Cint(-1)
349+
end
350+
end
351+
338352
struct ProcessChain <: AbstractPipe
339353
processes::Vector{Process}
340354
in::Redirectable

src/jl_uv.c

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ static void jl_uv_shutdownCallback(uv_shutdown_t *req, int status)
112112
}
113113

114114
// getters and setters
115+
JL_DLLEXPORT int jl_uv_process_pid(uv_process_t *p) { return p->pid; }
115116
JL_DLLEXPORT void *jl_uv_process_data(uv_process_t *p) { return p->data; }
116117
JL_DLLEXPORT void *jl_uv_buf_base(const uv_buf_t *buf) { return buf->base; }
117118
JL_DLLEXPORT size_t jl_uv_buf_len(const uv_buf_t *buf) { return buf->len; }

test/spawn.jl

+10
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ if valgrind_off
220220
@test success(proc)
221221
end
222222

223+
# issue #4752
224+
let out = Pipe(), proc, pid
225+
proc = spawn(pipeline(`$exename --startup-file=no -e 'println(STDERR,getpid())'`, stderr = out))
226+
pid = getpid(proc)
227+
close(out.in)
228+
@test parse(Int32, read(out, String)) == pid
229+
@test success(proc)
230+
@test getpid(proc) == -1
231+
end
232+
223233
# issue #6310
224234
@test read(pipeline(`$echocmd "2+2"`, `$exename --startup-file=no`), String) == "4\n"
225235

0 commit comments

Comments
 (0)