Repository navigation
Description
Background
I want to list the processes on my system.
Get-Process
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName
------ ----- ----- ------ -- -- -----------
0 0.00 3.75 0.00 1270 …69 (sd-pam)
0 0.00 9.84 1.25 962 962 accounts-daemon
0 0.00 0.00 0.00 117 0 acpi_thermal_pm
...
Further, I would like to sort the processes by parent.
I'm on Linux. And I have a Parent property. I'm not sure if that is available on Windows.
Get-Process | Select-Object @{name='Parent'; expr={$_.Parent.Id}}, Id, Name | Sort-Object Parent, Id
Parent Id Name
------ -- ----
105455 Web Content
1 2617 firefox
1 135731 code --no-sandbox --force-user-env --unity-launch --enable-crashpad
1284 1362 Thunar
1370 127909 thunderbird
1667 1688 pwsh
1688 11884 vim
Issue
The issue I noticed was that some of the Parent Process IDs (PPID) were missing.
Note there are no IDs in the Parent column below.
Get-Process | Select-Object @{name='Parent'; expr={$_.Parent.Id}}, Id, Name | Sort-Object Parent, Id
Parent Id Name
------ -- ----
1 systemd
2 kthreadd
847 UVM global queue
848 UVM deferred release queue
849 UVM Tools Event Queue
...
105455 Web Content
105458 Web Content
...
It is not clear to me if there is a bug with PowerShell, a bug with .NET, or something I don't understand.
The one-liner I'm issuing seems correct to me.
But I'm not getting a Parent Process ID for worker processes, which is the output I expect.
I don't think there is a permission issue because I am the only user. And the native task manager shows the correct parent and child relationships for processes.
The affected processes are mostly Mozilla Firefox and Thunderbird workers.
Environment
$PSVersionTable | Format-Table -Wrap
Name Value
---- -----
PSVersion 7.2.4
PSEdition Core
GitCommitId 7.2.4
OS Linux 5.18.5-1-MANJARO #1 SMP PREEMPT_DYNAMIC
Thu Jun 16 12:28:47 UTC 2022
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0

Activity
jborean93 commentedon Jun 19, 2022
What version of PowerShell are you running on Windows. I still see the
Parentproperty there. TheParentproperty is a code property added by PowerShell, I believe it was in 6 so it won't be present in Windows PowerShell (powershell.exe v5.1).Keep in mind Windows recycles their PIDs fairly aggressively so you could get some false links between a process another another one if the original parent has exited.
juvtib commentedon Jun 19, 2022
You appear to be referring to the property which is always available.
I'm referring to the instances. No PPID value is shown using my calculated property.
I do not use and do not have Windows.
jborean93 commentedon Jun 19, 2022
My apologies, I thought the issue was that the
Parentproperty itself was missing on Windows but I see that is not what you were saying. Looking into the code PowerShell is essentially looking at/proc/$pid/statas perPowerShell/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Lines 882 to 905 in 9f722ef
If this fails for some reason it will at least indicate what the problem was unlike the
Parentproperty which just silently ignores any exceptions.juvtib commentedon Jun 20, 2022
After some digging, it appears the bug is splitting on space.
Most of the affected processes contain spaces in their apparent names.
A minority of processes appear to spawn directly from process 0. And I am picking those up as
$nulls.I assume these child processes should have a parent object in their output so I could
$_.Parent.Id.I don't know C# or the
/proc/$($_.Id)/statformat well.But you need something that properly captures the second column.
Then you can access the fourth column correctly.
This might also affect macOS.
Mike
jborean93 commentedon Jun 20, 2022
That does seem to be the case, the docs for
/proc/[pid]/statcan be found at https://man7.org/linux/man-pages/man5/proc.5.html with the 2nd column statingLooking into fs/proc/array.c I can see https://github.com/torvalds/linux/blob/a111daf0c53ae91e71fd2bfe7497862d14132e3e/fs/proc/array.c#L567-L572 which indicates it's a simple value wrapped in
(...)limited to 64 characters (not 16). I think you are right though and the logic in PowerShell needs to be better than to split by space. We could try and beef up the matching with regex but there's no way to guarantee the 2nd column record doesn't contain(or)itself like2 (testing (123) name) S 0 0 0 ...making it quite difficult to parse properly. What might be a better method is to read/proc/[pid/statusand find the line starting withPPid: \d+This is helpful because each line is a new record and the labels clearly indicate what the value is for. The label also does not appear to be localized so this should be fine across languages https://github.com/torvalds/linux/blob/a111daf0c53ae91e71fd2bfe7497862d14132e3e/fs/proc/array.c#L183.
macOS uses a different method to get the ppid, there's a system call that seems to be available, or at least accessible through libpsl-native as per
PowerShell/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Lines 441 to 444 in 9f722ef
Edit: Just for reference sake this is the
GetPPidcall implemented in libpsl-native https://github.com/PowerShell/PowerShell-Native/blob/677c086f20209f70ecad379ec48de2562f1db668/src/libpsl-native/src/getppid.cpp#L27-L48.iSazonov commentedon Jun 20, 2022
Dup #12908 (PR #12925)
jborean93 commentedon Jun 20, 2022
I've created an alternative solution to #12925, hopefully one of the 2 can move forward #17545.
juvtib commentedon Jun 21, 2022
Thanks for taking a look at this.
I'm sorry I didn't see the duplicate issue earlier. The connection was not obvious to me until after your comments.
Hopefully, one of the patches will be accepted.
Mike