Exiting child process after os.fork ()

What is the correct call function to exit the child process after os.fork() ?

Documentation for os._exit() states :

The standard way out is sys.exit(n) .

_exit() should usually be used only in the child process after fork() .

It cannot be said whether termination of the child process with sys.exit() acceptable. So:

  • It?
  • Are there any potential side effects?
+6
source share
2 answers

The unix way is that if you are a child of fork , then you call _exit . The main difference between exit and _exit is that exit displays more - calls atexit , flushes stdio , etc. handlers, while _exit does the minimal amount of material in user space, just forcing the kernel to close all its files, etc. d.

This is very well ported to the python world with sys.exit , doing what exit does, and doing more than os._exit python interpreter, where os._exit makes it as minimal as possible.

If you are a child of fork and you call exit rather than _exit , then you can end up calling call handlers, which the parent will call again when it exits due to undefined behavior.

+8
source

The part of the documentation on os._exit(n) that you did not specify

Quit a process with status n without calling cleanup handlers, flushing stdio buffers, etc.

So, as I read this, you should use os._exit() while you exchange file handlers (so they will be close () 'd to another process, and you will take care to clear the buffers yourself (if these are questions in your case). Without shared resources (for example, in "files") - it does not matter.

So, if your child processes are just calculations and raw data is passed (not resource handlers), then it is safe to use exit() .

+1
source

Source: https://habr.com/ru/post/917934/


All Articles