fork: Resource temporarily unavailable

OSX 2010. 2. 15. 15:41

Mac OS X … “fork: Resource temporarily unavailable”

Almost a year ago, I made the switch to using Mac OS X as my primary workstation. As difficult as the transition was during my first 3 weeks, it has now become second nature to me. I haven’t had any major issues along the way, but now and then, I have had to tweak a thing or two. Here is the most recent such example.

Lately, I’ve been seeing a fair number of the following error, when launching commands under Bash within a local Terminal session. It’s rather annoying, because it stops me from getting any work done. As a systems administrator, I have lots of Terminal sessions open, and this is where the error has always manifested itself.

[laptop:~ root]$ ssh somehost 
-bash: fork: Resource temporarily unavailable

The first time this happened to me, I didn’t think much of it, so I rebooted my system. Today, I did not feel like rebooting my system, because I have a ton of work in progress. I suspect most people won’t hit this resource limit, but if you’re a power user, you eventually might.

Like other Unix-like operating systems, Max OS X limits the maximum number of processes, system-wide and per-user, but it does so with a rather conservative number. On my Mac, system-wide processes were limited to 512, and per-user processes were limited to 266. I’ve never encountered this problem on Linux, because these values are dynamically set in the kernel, based on the amount of memory installed in the Linux system.

Here’s how I resolved this problem.

  • First, determine what your system’s current limits are. These are the values on my installation of Mac OS X 10.5 with 4 GB of RAM.
[laptop:~ root]$ sysctl -a | grep maxproc | grep =
kern.maxproc = 512
kern.maxprocperuid = 266
  • Now set these to higher values. I suggest approximately doubling what you already have.
[laptop:~ root]$ sysctl -w kern.maxproc=1024
[laptop:~ root]$ sysctl -w kern.maxprocperuid=512
  • You’ll also want to apply these changes automatically at system bootup. To do so, add them to the /etc/sysctl.conf file.
[laptop:~ root]$ cat /etc/sysctl.conf
kern.maxproc=1024
kern.maxprocperuid=512
  • At this point, launched processes will still inherit the default maxprocperuid value of 266. I would like to change this inherited default value for new processes automatically, but I haven’t found a way to increase the default system-wide soft-limit of maxprocperuid on Mac. On Linux, I simply update the /etc/security/limits.conf file, but I don’t see such a file under Mac OS X. If you know of a way to do this in Mac OS X, leave a comment below. For now, I’ve added the following command to my user’s .bash_profile file. The value of 512 is intended to match that of kern.maxprocperuid shown above.
ulimit -u 512
  • To confirm the change, launch a new Terminal session, and run the following command. Before the change, you will have seen “266.” After the change, you should see “512″.
[laptop:~ root]$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) 6144
file size               (blocks, -f) unlimited
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 256
pipe size            (512 bytes, -p) 1
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 512 

virtual memory (kbytes, -v) unlimited

: