Skip to content

Commit

Permalink
Implemented getpid(p::Process).
Browse files Browse the repository at this point in the history
Closes #4752.
  • Loading branch information
abalkin committed Oct 9, 2017
1 parent 49c066b commit 9156fb4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/jl_uv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
10 changes: 10 additions & 0 deletions test/spawn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 9156fb4

Please sign in to comment.