-
#include "apue.h"
-
int fun()
-
{
-
int pid;
-
if((pid=fork())<0)
-
printf("error\n");
-
else if(pid==0)
-
{
-
printf("child..........1\n");
-
//return 0;
-
exit(0);
-
}
-
else
-
{
-
printf("parent..........1\n");
-
return pid;
-
}
-
}
-
int
-
main(void)
-
{
-
int pid;
-
pid=fun();
-
if(pid==0)
-
{
-
printf("child.......2\n");
-
exit(0);
-
}
-
else
-
{
-
printf("parent..........2\n");
-
exit(2);
-
}
-
}
我们知道当return 0和exit(0)用在主函数时候是相通的,但在分主函数中却是不能代替的。
看下面一段程序。当子函数调用return 0的时候,只是函数返回,子进程并没有结束,当子函数调用exit(0)
时,子进程结束。
运行结果:

return 0

exit(0)
阅读(3138) | 评论(0) | 转发(0) |