Please help me in clarifying the concept of these two python statements in terms of difference in functionality:
Here is Solutions:
We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.
According to the documentation:
Exit the process with status n, without calling cleanup handlers, flushing stdio buffers, etc.
Note The standard way to exit is
_exit()should normally only be used in the child process after a
os._exit calls the C function
_exit() which does an immediate program
termination. Note the statement “can never return”.
sys.exit() is identical to
raise SystemExit(). It raises a Python
exception which may be caught by the caller.
Excerpt from the book "The linux Programming Interface":
Programs generally don’t call
_exit() directly, but instead call the
exit() library function,
which performs various actions before calling
- Exit handlers (functions registered with
on_exit()) are called, in
reverse order of their registration
- The stdio stream buffers are flushed.
_exit()system call is invoked, using the value supplied in status.
Could someone expand on why
_exit() should normally only be used in the child process after a fork()?
Instead of calling
exit(), the child can call
_exit(), so that it doesn’t flush stdio
buffers. This technique exemplifies a more general principle: in an application
that creates child processes, typically only one of the processes (most often the
parent) should terminate via
exit(), while the other processes should terminate
_exit(). This ensures that only one process calls exit handlers and flushes
stdio buffers, which is usually desirable
Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂