免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2331 | 回复: 12
打印 上一主题 下一主题

[Linux] 实在不明白这段代码为什么会发生segmentation fault。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-10-12 10:18 |只看该作者 |倒序浏览
int a=20;
if(vfork()==0)
{a=30;
cout<<"c:"<<a<<endl;
}else{
cout<<"f:"<<a<<endl;
}

论坛徽章:
0
2 [报告]
发表于 2012-10-12 10:27 |只看该作者
谁能帮忙解答一个啊!!!!
谢谢啊

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
3 [报告]
发表于 2012-10-12 11:58 |只看该作者
A504c 发表于 2012-10-12 10:27
谁能帮忙解答一个啊!!!!
谢谢啊


man 2 vfork

论坛徽章:
0
4 [报告]
发表于 2012-10-12 12:43 |只看该作者
  1. vfork() differs from fork(2) in that the parent is suspended until  the
  2.        child  terminates (either normally, by calling _exit(2), or abnormally,
  3.        after delivery of a fatal signal), or it makes  a  call  to  execve(2).
  4.        Until  that point, the child shares all memory with its parent, includ‐
  5.        ing the stack.  The child must not return from the current function  or
  6.        call exit(3), but may call _exit(2).
复制代码
第四行下加_exit(0);

论坛徽章:
0
5 [报告]
发表于 2012-10-12 12:56 |只看该作者
谢谢提醒,看了一下大概懂了。不过有一句不是特别明白:It does not work, however, to return while running in the child's context from the procedure that called vfork() since the eventual return from vfork() would then return a no longer existent stack frame.大概就是子进程返回后把父进程的栈注销了,对吧?

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
6 [报告]
发表于 2012-10-12 13:26 |只看该作者
本帖最后由 MMMIX 于 2012-10-12 13:27 编辑
观夜天象 发表于 2012-10-12 12:43
第四行下加_exit(0);


他那个代码的问题是 vfrok() 后的 a=30; 导致程序会出现未定义行为。

论坛徽章:
0
7 [报告]
发表于 2012-10-12 14:06 |只看该作者
MMMIX 发表于 2012-10-12 13:26
他那个代码的问题是 vfrok() 后的 a=30; 导致程序会出现未定义行为。

老大,是写时拷贝,好像不是未定义行为吧。

论坛徽章:
0
8 [报告]
发表于 2012-10-12 14:10 |只看该作者
A504c 发表于 2012-10-12 12:56
谢谢提醒,看了一下大概懂了。不过有一句不是特别明白:It does not work, however, to return while runni ...

这里是子进程的栈帧已不存在。

使用vfork(),父子进程的执行次序固定,子进程先执行,父进程会等待其完成,一般紧接着就是调用exec系列函数。

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
9 [报告]
发表于 2012-10-12 16:16 |只看该作者
观夜天象 发表于 2012-10-12 14:06
老大,是写时拷贝,好像不是未定义行为吧。

   Standard Description
       (From  POSIX.1) The vfork() function has the same effect as fork(2), except that the behavior is undefined if the process created by vfork() either modifies any data other than a
       variable of type pid_t used to store the return value from vfork(), or returns from the function in which vfork() was called, or calls  any  other  function  before  successfully
       calling _exit(2) or one of the exec(3) family of functions.

论坛徽章:
0
10 [报告]
发表于 2012-10-12 16:44 |只看该作者
MMMIX 发表于 2012-10-12 16:16

疏忽了,向斑竹学习。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP