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

PID 0, ppid(), parent() inconsistencies #1438

Closed
giampaolo opened this issue Feb 26, 2019 · 2 comments
Closed

PID 0, ppid(), parent() inconsistencies #1438

giampaolo opened this issue Feb 26, 2019 · 2 comments
Labels

Comments

@giampaolo
Copy link
Owner

giampaolo commented Feb 26, 2019

On OSX:

>>> psutil.pids()[0]  # lowest pid
0
>>> psutil.Process(0)
psutil.Process(pid=0, name='kernel_task', started='2019-02-24 04:41:17')
>>> psutil.Process(0).ppid()
0
>>> psutil.Process(0).parent()
psutil.Process(pid=0, name='kernel_task', started='2019-02-24 04:41:17')
>>> 

On Linux:

>>> psutil.pids()[0]  # lowest pid
1
>>> psutil.Process(1)
psutil.Process(pid=1, name='systemd', started='2019-02-22 10:56:55')
>>> psutil.Process(1).ppid() 
0
>>> psutil.Process(1).parent()
>>> psutil.Process(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "psutil/__init__.py", line 444, in __init__
    self._init(pid)
  File "psutil/__init__.py", line 484, in _init
    raise NoSuchProcess(pid, None, msg)
psutil.NoSuchProcess: psutil.NoSuchProcess no process found with pid 0

On OSX, ppid() return 0, which is "weird", but also understandable because it's the only possible "null" value. On the other hand parent() should return None (and this should be fixed for sure).

On Linux the "weird" thing is that ppid() return 0 (this comes from the kernel), but PID 0 does not exist. Perhaps it should return 1 instead.

@giampaolo
Copy link
Owner Author

giampaolo commented Feb 26, 2019

Probably a reasonable solution here is to make sure that parent() always return None when dealing with PID 0 (or 1 on Linux) and leave ppid() alone.
ppid() should keep returning 0 also on Linux, even if PID 0 does not exist, because it's the only possible "null" value.

@giampaolo
Copy link
Owner Author

Fixed in 682fae3.

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

No branches or pull requests

1 participant