processes in LInux
everyone I have some question 开发者_JAVA百科regarding programs: if I have for example some program:
int main()
{
int value = 0;
...
return value;
}
my OS creates new process, which uses execv() to run this program, when I do return value
I transfer value back to the process, my question how will this process end? does it execute exit(value), when value is the value from my program? thanks in advance for any help
Returning from main()
is basically equivalent to calling exit()
, and starts the Normal Termination procedure.
Normal termination causes the following actions:
Functions that were registered with the atexit or on_exit functions are called in the reverse order of their registration. This mechanism allows your application to specify its own “cleanup” actions to be performed at program termination. Typically, this is used to do things like saving program state information in a file, or unlocking locks in shared data bases.
All open streams are closed, writing out any buffered output data. In addition, temporary files opened with the tmpfile function are removed.
_exit()
is called, terminating the program.
Finally, the system does the usual cleanup after the process termination (files closed, exit code reported, child processed terminated or reassigned to init
...) See Termination internals
The new process which was created to call execve()
is the process that is running your code. execve()
replaces the image of the calling executable with a new executable image - a successful call to execve()
never returns.
So, your code does not return a value to that process - it returns a value to the parent process, which is the one that called fork()
to create the child that called execve()
. If that parent process is not still running, the return value is passed instead to the init
process (PID 1).
When main is executed, a new process is created with parent as init. when return is executed, main process exits and sends the return status(value) to init process.
精彩评论