MacOS – How to check max allowed open files (ulimit) of a running process (or running app)

kernelmacos

In Linux, I can check the max allowed open files (ulimit) of a process by simply executing

$ cat /proc/<PID>/limits

Limit                     Soft Limit           Hard Limit           Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            8388608              unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             14248                14248                processes 
Max open files            1024                 1048576              files     
Max locked memory         67108864             67108864             bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       14248                14248                signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us

How can I do this in MacOS?

Thanks a lot.

PS1: I knew how to run ulimit commands to check the current ulimit setup. But I don't know how to check and verify those values for a running process.

PS2: I knew how to use lsof to check how many files a process is opening. But it doesn't tell the max number of files a process can open.

Best Answer

You cannot do this in a way that is similar to Linux.

procfs, although common on many Unix-like operating systems, is not available on all Unix-like operating systems. It is not available on macOS.

The POSIX standard prescribes a getrlimit() system call for retrieving these limits for the current process. It is available on macOS as it is POSIX-compliant.

Linux on the other hand has implemented a system call known as prlimit(), which allows getting these limits for other processes than the current process. The system call is specific to Linux and not available on other Unix-like operating systems.

All in all this means that there's no direct API for retrieving that information on macOS.

However, you can retrieve the information by the way of inspection inside the other process. For example by attaching similar to how a debugger would attach (ptrace()), or by the use of dtrace. It is though possible for the other process to actively resist this measure.

Depending on your specific use case, which you have not revealed, YMMV.