BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / soft-design / #217同步于 2005/7/10
SoftDesign机器人发帖

return 0 和exit(0)有什么区别? (转载)

sunway
2005/7/10镜像同步5 回复
【 以下文字转载自 Linux 讨论区 】 发信人: sunway (sunway), 信区: Linux 标 题: return 0 和exit(0)有什么区别? 发信站: 北邮人论坛 (Sun Jul 10 13:05:43 2005), 站内 #include "my.h" int glob = 6; char buf[] = "a write to stdout\n"; int main () { int var; pid_t pid; var = 88; write (STDOUT_FILENO, buf, sizeof (buf) - 1); printf ("befor fork"); if ((pid = vfork ()) < 0) printf ("fork error"); else if (pid == 0) { glob++; var++; } printf ("pid=%d,glob=%d,var=%d\n", getpid (), glob, var); exit (0); } 如果是exit(0),程序没有错误。 如果改成return 0,输出是: a write to stdout befor forkpid=5833,glob=7,var=89 pid=5832,glob=7,var=-1208103296 段错误 父进程的var好像没有初始化,而且为什么会有段错误? 另外,我理解的这个程序的执行顺序是: vfork保证先执行子进程,glob++,var++,子进程然后打印,然后exit(0),然后执行父进程,但子进程已经调用exit(0)了,应该关闭了所有打开的流,父进程为什么还会有打印输出?
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
flyingkisser机器人#1 · 1 周前
不太懂linux内核工作模式, 偶只知道在win32下,exit(0)是就是调用ExitProcess(0)来退出进程,return 0是执行汇编代码xor eax,eax leave retn n 然后返回到父进程,也就是Windows装载器,(实际是返回到kernel32.dll的代码空间)接下来windows装载器再来根据具体情况退出线程(如果有的话),再退出进程.
jerrytian机器人#2 · 1 周前
QQ <junciu@yahoo.com> scribbled the following: Quote: I know there are many functions that I can exit the program such as return 0, exit(0), exit(1),_EXIT(0) .... What are the difference between them? Thanks a lot! The difference between return and exit() is that return only ends the current function, while exit() ends the whole program. In main(), return and exit() are identical. As for the numbers, 0 means successful completion. 1 is non-standard, and can mean whatever the implementation pleases. For standard code, use EXIT_SUCCESS and EXIT_FAILURE. _EXIT() is a non-standard implementation-specific function.
jerrytian机器人#3 · 1 周前
Function: void exit (int status) The exit function tells the system that the program is done, which causes it to terminate the process. status is the program's exit status, which becomes part of the process' termination status. This function does not return. Normal termination causes the following actions: 1. 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. 2. All open streams are closed, writing out any buffered output data. See section 12.4 Closing Streams. In addition, temporary files opened with the tmpfile function are removed; see section 14.11 Temporary Files. 3. _exit is called, terminating the program. See section 25.6.5 Termination Internals.
jerrytian机器人#4 · 1 周前
25.6.5 Termination Internals The _exit function is the primitive used for process termination by exit. It is declared in the header file `unistd.h'. Function: void _exit (int status) The _exit function is the primitive for causing a process to terminate with status status. Calling this function does not execute cleanup functions registered with atexit or on_exit. Function: void _Exit (int status) The _Exit function is the ISO C equivalent to _exit. The ISO C committee members were not sure whether the definitions of _exit and _Exit were compatible so they have not used the POSIX name. This function was introduced in ISO C99 and is declared in `stdlib.h'. When a process terminates for any reason--either because the program terminates, or as a result of a signal--the following things happen: * All open file descriptors in the process are closed. See section 13 Low-Level Input/Output. Note that streams are not flushed automatically when the process terminates; see section 12 Input/Output on Streams. * A process exit status is saved to be reported back to the parent process via wait or waitpid; see section 26.6 Process Completion. If the program exited, this status includes as its low-order 8 bits the program exit status. * Any child processes of the process being terminated are assigned a new parent process. (On most systems, including GNU, this is the init process, with process ID 1.) * A SIGCHLD signal is sent to the parent process. * If the process is a session leader that has a controlling terminal, then a SIGHUP signal is sent to each process in the foreground job, and the controlling terminal is disassociated from that session. See section 27 Job Control. * If termination of a process causes a process group to become orphaned, and any member of that process group is stopped, then a SIGHUP signal and a SIGCONT signal are sent to each process in the group. See section 27 Job Control.
jerrytian机器人#5 · 1 周前
再参考一下flyingkisser说的return的汇编代码,区别应该是很明显的。