return 0;和exit(0);的区别
- fork 是 创建一个子进程,并把父进程的内存数据copy到子进程中。
- vfork是 创建一个子进程,并和父进程的内存数据share一起用。
这两个的差别是,一个是copy,一个是share。
你 man vfork 一下,你可以看到,vfork是这样的工作的,
1)保证子进程先执行。
2)当子进程调用exit()或exec()后,父进程往下执行。
那么,为什么要干出一个vfork这个玩意? 原因是这样的—— 起初只有fork,但是很多程序在fork一个子进程后就exec一个外部程序,于是fork需要copy父进程的数据这个动作就变得毫无意了,而且还很重,所以,搞出了个父子进程共享的vfork。所以,vfork本就是为了exec而生。
为什么return会挂掉,exit()不会?
从上面我们知道,结束子进程的调用是exit()而不是return,如果你在vfork中return了,那么,这就意味main()函数return了,注意因为函数栈父子进程共享,所以整个程序的栈就跪了。
如果你在子进程中return,那么基本是下面的过程:
- 子进程的main() 函数 return了
- 而main()函数return后,通常会调用 exit()或相似的函数(如:exitgroup())
- 这时,父进程收到子进程exit(),开始从vfork返回,但是尼玛,老子的栈都被你干废掉了,你让我怎么执行?(注:栈会返回一个诡异一个栈地址,对于某些内核版本的实现,直接报“栈错误”就给跪了,然而,对于某些内核版本的实现,于是有可能会再次调用main(),于是进入了一个无限循环的结果,直到vfork 调用返回 error)
好了,现在再回到 return 和 exit,return会释放局部变量,并弹栈,回到上级函数执行。exit直接退掉。如果你用c++ 你就知道,return会调用局部对象的析构函数,exit不会。(注:exit不是系统调用,是glibc对系统调用 _exit()或_exitgroup()的封装)
可见,子进程调用exit() 没有修改函数栈,所以,父进程得以顺利执行。
链接:https://www.zhihu.com/question/26591968/answer/33639916
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
关于写时拷贝(COW)。
就是fork后来采用的优化技术,这样,对于fork后并不是马上拷贝内存,而是只有你在需要改变的时候,才会从父进程中拷贝到子进程中,这样fork后立马执行exec的成本就非常小了。而vfork因为共享内存所以比较危险,所以,Linux的Man Page中并不鼓励使用vfork() ——
“ It is rather unfortunate that Linux revived this specter from the past. The BSD man page states: "This system call will be eliminated when proper system sharing mechanisms are implemented. Users should not depend on the memory sharing semantics of vfork() as it will, in that case, be made synonymous to fork(2)."”
于是,从BSD4.4开始,他们让vfork和fork变成一样的了。但在后来,NetBSD 1.3 又把传统的vfork给捡了回来,说是vfork的性能在 Pentium Pro 200MHz 的机器上有可以提高几秒钟的性能。详情见——“NetBSD Documentation: Why implement traditional vfork()”
关于vfork产生的原因
你可以看一下Linux Man page——
Historic Description
Under Linux, fork(2) is implemented using copy-on-write pages, so the only penalty incurred by fork(2) is the time and memory required to duplicate the parent’s page tables, and to create a unique task structure for the child. However, in the bad old days a fork(2) would require making a complete copy of the caller’s data space, often needlessly, since usually immediately afterwards an exec(3) is done. Thus, for greater efficiency, BSD introduced the vfork() system call, which did not fully copy the address space of the parent process, but borrowed the parent’s memory and thread of control until a call to execve(2) or an exit occurred. The parent process was suspended while the child was using its resources. The use of vfork() was tricky: for example, not modifying data in the parent process depended on knowing which variables are held in a register.
return 0;和exit(0);的区别的更多相关文章
- System.exit(0)作用
System.exit(0)作用 public class HelloGoodbye{ try{ System.out.println(“Hello World”); System.exit(0) ...
- exit(0) exit(1) return() 3个的区别
exit(0):正常运行程序并退出程序: exit(1):非正常运行导致退出程序: return():返回函数,若在主函数中,则会退出函数并返回一值. 详细说: 1. return返回函数值,是关键字 ...
- PHP中exit,exit(0),exit(1),exit('0'),exit('1'),die,return的区别
die('1') die()和exit()都是中止脚本执行函数:其实exit和die这两个名字指向的是同一个函数,die()是exit()函数的别名.该函数只接受一个参数,可以是一个程序返回的数值或 ...
- System.exit(0)和System.exit(1)区别:
System.exit(0)是将你的整个虚拟机里的内容都停掉了,而finish()只是退出了activity,并没有退出应用,Application还是存在于内存中的,除非被系统回收.无论如何,内存都 ...
- android Activity类中的finish()、onDestory()和System.exit(0) 三者的区别
android Activity类中的finish().onDestory()和System.exit(0) 三者的区别 Activity.finish() Call this when your a ...
- System.exit(0)和System.exit(1)区别
System.exit(0)是将你的整个虚拟机里的内容都停掉了 ,而dispose()只是关闭这个窗口,但是并没有停止整个application exit() .无论如何,内存都释放了!也就是说连JV ...
- android开发时,finish()跟System.exit(0)的区别
这两天在弄Android,遇到一个问题:所开发的小游戏中有背景音乐,玩的过程中始终有音乐在放着,然后在我退出游戏后,音乐还在播放! 我看了一下我最开始写的退出游戏的代码,就是简单的finish() ...
- exit(0)和exit(1)区别
exit(0):正常退出程序: exit(1):非正常退出程序: return:返回. return是语言级别的,表示调用堆栈的返回: exit是系统调用级别的,表示一个进程的退出.
- android Process.killProcess 和 System.exit(0) 区别
1 Process.killProcess 和 System.exit(0) 两个都会 kill 掉当前进程. 你可以打开 DDMS 查看进程号,或 adb shell 进入 shell 然后 ps ...
随机推荐
- appium问题解决
ppium 1.4.16 版本 测试安卓7.0 提示AppiumSettings.Unlock.AndroidInputManager 安装 修改 C:\Program Files (x86)\App ...
- AI学习资料
OpenAI Gym介绍 http://m.blog.csdn.net/u010510350/article/details/71450232
- 端口安全检查shell脚本
#!/bin/bash #This script name is scan_analyse.sh . /etc/profile echo "start time is $(date)&quo ...
- 第四章 consul cluster
1.vagrant 为了模拟集群效果,使用vagrant. 1.1.首先下载vagrant https://www.vagrantup.com/downloads.html 说明:浏览器下载可能比较慢 ...
- luogu p3371 单源最短路径(dijkstral
本来我写的对的 我就多手写了个 ios::sync_with_stdio(false); 我程序里面用了cin 还有scanf 本来想偷偷懒 我就说 我查了半天错 根本找不到的啊... 后来交了几次 ...
- APP AutoTestCaseID
public class AutoTestCaseID { ElementExist el = new ElementExist(); static AutoTestExcelFile ft = ne ...
- PHP+MySql+jQuery实现的“顶”和“踩”投票功能
index.html <!DOCTYPE HTML><html><head><meta charset="utf-8"><ti ...
- 使用 for 循环
for 循环通过迭代一个给定向量或列表,重复执行某个表达式.for 循环的语法是这样的:for (var in vector) {expr}var 遍历 vector 中的各个元素值,expr 被反复 ...
- SQL向一个表中批量插入&&删除大量数据
插入: 1. 数据从另一个表中获取 (1)两表结构不一样insert into tb1 需要的列名 select 按照前面写上需要的列名 from tb2(2)两表结构一样insert into tb ...
- Redis.RedisNativeClient的方法get_Db 没有实现
C#出现问题:Redis.RedisNativeClient”的方法“get_Db”没有实现 ServiceStack.Redis.RedisNativeClient”的方法“get_Db”没有实现 ...