Search code examples
cmacosmultiprocessingsemaphorebsd

How to increase the maximum semaphore limit on MacOS/BSD


I want to increase the maximum number of semaphores that can be opened at the same time.

I know that the current limit can be retrieved via

long nsems_max = sysconf(_SC_SEM_NSEMS_MAX);

or from the console via

ipcs -S

semmap:     30  (# of entries in semaphore map)
semmni:      8  (# of semaphore identifiers)
semmns:    128  (# of semaphores in system)
semmnu:      0  (# of undo structures in system)
semmsl:  87381  (max # of semaphores per id)
semopm:      5  (max # of operations per semop call)
semume:     10  (max # of undo entries per process)
semusz:     32  (size in bytes of undo structure)
semvmx:  32767  (semaphore maximum value)
semaem:  16384  (adjust on exit max value)

On Linux the limit can be changed by manually editing

/proc/sys/kernel/sem

but this doesn't work for MacOS/BSD. How to change/increase the limit on Mac?


Solution

  • The following variables are editable via /usr/sbin/sysctl (available for your current session), or you can create a plist to always set the values on reboot. You must create the file.

    sudo vi /Library/LaunchDaemons/sysctl.plist`
    

    Set them as your heart desires.

    /Library/LaunchDaemons/sysctl.plist

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
     <key>Label</key>
     <string>sysctl</string>
     <key>ProgramArguments</key>
     <array>
       <string>/usr/sbin/sysctl</string>
       <string>-w</string>
       <string>kern.sysv.semmni=87381</string>
       <string>kern.sysv.semmns=87381</string>
       <string>kern.sysv.semmnu=87381</string>
       <string>kern.sysv.semmsl=87381</string>
       <string>kern.sysv.semume=10</string>
       <string>kern.posix.sem.max=10000</string>
     </array>
     <key>RunAtLoad</key>
     <true/>
    </dict>
    </plist>
    

    Then load:

    launchctl load /Library/LaunchDaemons/sysctl.plist