I found this question about what's the maximum PID for Linux and my question is exactly the same for OSX :
OSX doesn't seem to have the /proc/sys/kernel/pid_max file containing this value on Linux.
Is there an equivalent file or an other way to find out what is the range of PIDs on an OSX system?
The maximum PID on macOS is 99998.
Unlike on Linux, this value is not tunable. I'm not aware of any way to retrieve it in a program; the only assumption you should make is that the value of a process ID will fit into the pid_t type.
Related
I am learning OS in Linux, and want to get the executable file name.
I know I can get it from 'comm' located in task_struct, but I don't know how to get it.
Thank you!
Do you want to do this in user mode or kernel mode?
In user mode, use procfs:
https://unix.stackexchange.com/questions/449300/get-application-name-from-pid
In kernel mode, use pid_task to get the task_struct pointer, then read whatever you want:
Efficient way to find task_struct by pid
I happen to be using Python bindings here, but I suspect that the problem, and the eventual answer, are not Python-specific.
On Windows 10, using Python bindings to Windows' Core Audio library, specifically via pycaw.AudioUtilities.GetAllSessions(), I can see that there is a session with name 'conhost.exe' and pid 11512, and by elementary guesswork/experimentation I can see that this is the session that (a) governs the audio for my current process and (b) corresponds to the "Console Window Host" slider in the Windows 10 Volume Mixer. This is the one I want to manipulate from my program, and pycaw provides the bindings to do that.
So far so good. My problem is that I don't see any way of working backwards from the current process ID so that, on the next launch, my program can ask "which process corresponds to the session that governs my audio output?" Naively I expected that that session pid might appear in the current process's ancestry, but it does not:
import os, psutil
psutil.Process(os.getpid())
# prints: psutil.Process(pid=3628, name='python.exe', started='14:56:41')
psutil.Process(os.getpid()).parent()
# prints: psutil.Process(pid=16676, name='cmd.exe', started='13:00:57')
psutil.Process(os.getpid()).parent().parent()
# prints: psutil.Process(pid=1356, name='explorer.exe', started='12:21:26')
psutil.Process(os.getpid()).parent().parent().parent()
# prints nothing. It's `None`. We've reached the top.
How should I be querying the pid for whichever audio session governs the current process? (I presume the session won't always be named 'conhost.exe—sometimes I run pythonw.exe to execute the same code without a console.)
In Linux, I can use echo t > /proc/sysrq-trigger to dump the kernel call stack of all threads in system.
Is there any method in Mac OS X for the same purpose? or any method to dump kernel stack of one process?
Short answer: procexp 0 threads (as root) will do the trick, where procexp is "Process Explorer" from http://newosxbook.com/tools/procexp.html .
Slightly Longer answer:
- Dtrace is overkill and will need SIP disablement
- stackshot is deprecated since its underlying syscall (#365) was removed
- A replacement, stack_snapshot_with_config(#491) can be used programmatically as well (this is what drives the above tool)
The answer is probably dtrace. I know Instruments.app (or iprofiler) can do probe based profiling, so it takes periodic stack traces. (user or kernel; your choice) As far as I'm aware this is all based on dtrace, although I don't know it well enough to be able to tell you a way to take a one-off trace.
Hmm... I didn't code on Mac OS X for serval years. But a tool with name 'stackshot' can help you do this. Try to google it to get the usage. :-)
From http://www.brendangregg.com/DTrace/DTrace-cheatsheet.pdf:
sudo dtrace -n 'fbt:::entry { stack(10); ustack(5) }'
prints 10 kernel frames, 5 userland frames
I need to query the DeviceID of each connected monitor on Mac OS X Leopard & Snow Leopard. is this possible to do using bash? if not what would be the best approach?
would this be stored in a preference file anywhere?
i tried accessing the system_profiler info but it does not look like the device id is included for the monitors.
any help would be greatly appreciated...
Thanks!
You can find here How to Get the Display Name with the Display ID in Mac OS X? one small C program. You can compile it, and when you run it will show Device ID.
For example, for my notebook will return:
Color LCD : 69677760
it is decimal number, when you convert it to hexadecimal
echo 69677760 16 o p | dc
will return the 42732C0 hex-number what is the last part of Device ID from the colorsync.
My program reads device paths like /dev/rdisk0 from input and then it looks in IOKit for a disk with the BSD name disk0. For this I have to remove /dev/r from the path.
Hard coding this path can break in future versions of Mac OS X. Therefore I though of another way: I could match the IOService using the BSD Major and Minor version of the device.
Here's my question: Is it possible to extract the BSD minor and major numbers from a path?
Yes. Use the stat syscall. The member of struct stat you are looking for is st_dev, which I believe is an OR of major and minor after a bit shift.