Resource Limitations (setrlimit) for Mac OS X

I am trying to understand how to set resource limits for a process from this process on Mac OS X (I have 10.6). On CentOS, the following test program (python3.3) works as I expected:

import resource import signal def confirm(prompt, instructions='(y)es or (n)o only, please.'): while True: ok = input(prompt) if ok in ('y', 'ye', 'yes'): return True if ok in ('n', 'no'): return False print(instructions) def handle_cpu_soft_limit(signal, frame): print('CPU soft limit reached.') if not confirm('Do you want to continue anyway? (y/n): '): raise ResourceWarning() def long_function(): a = 0 while True: a += 1 a -= 1 def main(): # set soft limit to 1 second, hard limit to 5 seconds (CPU time) resource.setrlimit(resource.RLIMIT_CPU, (1, 5)) # set handler for soft CPU limit signal signal.signal(signal.SIGXCPU, handle_cpu_soft_limit) # do some busy work try: long_function() except ResourceWarning: print('Stopping now before being forced.') if __name__ == '__main__': main() 

The program burns processor time until a soft limit is reached (1 second, the first element in the tuple that passed to resource.setrlimit() ). The program captures the signal sent by the OS (in this case SIGXCPU) and asks me if I want to continue. On CentOS (or other Linux), if I say yes, it will ask me again after one second until a hard limit (5 seconds) is reached, and at that moment the OS kills the process. On Mac OS X, I still receive the SIGXCPU signal and process it, but I never receive the signal again, and the process will never be killed even when the hard limit is reached. This may or may not be due to the fact that I have experienced the question of this issue .

The manual page for setrlimit on Mac OS X says the following:

The resource limit is set as a soft limit and a hard limit. When the soft limit is exceeded, the process may receive a signal (for example, if the processor time or file size is exceeded), but it will be allowed to continue execution until it reaches the hard limit (or changes its resource limit).

... which is actually on the BSD 4.2 manual page on setrlimit. However, it seems that the program never gets SIGKILL when a hard limit is reached. I searched google and SO for information on mac setrlimit but did not find much useful. I have two questions:

  • Is this behavior (specifically mac os X, ignoring hard process limits) intended and documented anywhere? I wrote my example here in python, but the same thing happens in C (I can post some test code for this here on request), so this is not a language error.

  • Is there a way to make this code behave the same on Linux and Mac? Or should I just consider the soft limit as the hard limit on the Mac?

+7
source share

All Articles