Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented getpid(p::Process). #24064

Closed
wants to merge 5 commits into from
Closed

Conversation

abalkin
Copy link
Contributor

@abalkin abalkin commented Oct 9, 2017

Closes #4752.

Get the process ID of a running process. Returns `-1` if the
process is not running.
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about alternatives when the process is not running, like throwing, or returning nothing ?

Copy link
Contributor Author

@abalkin abalkin Oct 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning nothing would violate type stability. Throwing is an option, but I feel returning -1is more convenient. There was also a suggestion to save the pid so that it can be inspected after the process has ended. I don't think this is a good idea because pids can be reused and having getpid(p) return a potentially unrelated pid is error prone. (E.g. you don't want to accidentally kill an unrelated process.)

Also, as @vtjnash mentioned before, we can take guidance from nodejs where child pid is accessible via a property which does not throw.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran an experiment with node, and it looks like they cache the pid:

> const { spawn } = require('child_process');
> x = spawn('sleep',  ['1']);
> x.exitCode
0
> x._handle
null
> x.pid
45660

I still don't think this is a good idea for the reasons I mentioned above.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is essentially the same question as #18145. But upon reflection, I think an error would be better here. In some places, -1 is used as a special value to mean "all processes". So kill(getpid(p)) could accidentally cause the system to reboot (surprise!) with this choice of sentinel value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. I'll make the change. Just to make sure: caching the pid is off the table.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing may make it type unstable, but this seems to be the new way of returning what was a Nullable (ie. returning Union{T, Void} or Union{Some{T},Void}), and getpid is a function for which I would tend to return a Nullable.

@@ -335,6 +335,20 @@ end
pipe_reader(p::Process) = p.out
pipe_writer(p::Process) = p.in

"""
getpid(p::Process) -> Int32
Copy link
Member

@rfourquet rfourquet Oct 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not return Int?
EDIT: oh I see, getpid() returns Int32 already, but still...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I just copied that line from the Base.getpid() doc. The actual type is Cint, but I think that is the same as Int32 on all the supported platforms.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this was the right choice (matching getpid())

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about changing that to Int for both methods in another PR? for example, one method of kill (with ClusterManager) accepts a pid as Int.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rfourquet - isn't that a different ID? I thought kill(::ClusterManager, ..) takes the worker id, not the OS process id.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems you are right.

@Keno
Copy link
Member

Keno commented Nov 16, 2017

This seems to have fallen through the cracks, but seems fine to me. @vtjnash ?

@StefanKarpinski
Copy link
Member

Somewhat tangential to this, but I think that we should use Int for process IDs.

@stevengj
Copy link
Member

Bump

@stevengj
Copy link
Member

Rebased.

@JeffBezanson
Copy link
Member

Our getpid is actually Libc.getpid. Should we add a method to that (even though such a signature doesn't exist in libc)?

@iamed2
Copy link
Contributor

iamed2 commented Jul 30, 2018

Our getpid is actually Libc.getpid. Should we add a method to that (even though such a signature doesn't exist in libc)?

I think it would make sense to have the Libc getpid extend Base.getpid. If there are further uses for getpid that don't use Libc (e.g., some package implements some other process management system) it'll look weird to have to extend it from there again.

@JeffBezanson
Copy link
Member

Well, we don't really have a way to do that exactly. Base.getpid and Libc.getpid are either the same function or two functions.

@iamed2
Copy link
Contributor

iamed2 commented Jul 30, 2018

Oh, I meant make them the same function but put it in the Base namespace rather than Libc

@stevengj
Copy link
Member

stevengj commented Jul 30, 2018

@IAMED, other packages can already extend it via Base.pid(...) = ..., no? So it's not clear to me how it matters in practice if it's defined first in Base or Libc.

@iamed2
Copy link
Contributor

iamed2 commented Jul 30, 2018

Oh you're right, my bad, that's something good for me to remember in the future. Sorry to muddy the waters.

@vtjnash
Copy link
Member

vtjnash commented Nov 18, 2018

Thanks for the PR! Sorry it took us such a long time to land.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants