免费注册 查看新帖 |

Chinaunix

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

[C] vfork导致死循环 求解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-18 21:16 |只看该作者 |倒序浏览
  1. #include <unistd.h>
  2. #include <stdio.h>

  3. int main(void)
  4. {
  5.         if (vfork() == 0) {
  6.                 printf("This is the child process!\n");
  7.                 sleep(1);
  8.         } else {
  9.                 printf("This is the parent process!\n");
  10.         }
  11.         return 0;
  12. }
复制代码
未加入sleep(1) :
This is the child process!
This is the parent process!
加入sleep(1)后 成为死循环:
This is the child process!
This is the parent process!
This is the child process!
This is the parent process!
This is the child process!
This is the parent process!
This is the child process!
This is the parent process!
This is the child process!
求解!!!!!

论坛徽章:
0
2 [报告]
发表于 2011-08-18 21:53 |只看该作者
你 把 return  改为 exit 还会出现不

论坛徽章:
0
3 [报告]
发表于 2011-08-18 22:10 |只看该作者
回复 2# 0506010237


    不会 退出了  改回return之后 我获取pid 尝试理解其中的过程 结果如下
This is the child process! 1965
This is the parent process! 1964
This is the child process! 1966
This is the parent process! 1964
This is the child process! 1967
This is the parent process! 1964
This is the child process! 1968
This is the parent process! 1964

父进程不断在循环  能解不?

论坛徽章:
0
4 [报告]
发表于 2011-08-18 22:12 |只看该作者
回复 2# 0506010237


    不会了  改回return之后  获取pid 尝试理解过程 结果如下
This is the child process! 1968
This is the parent process! 1964
This is the child process! 1969
This is the parent process! 1964
This is the child process! 1970
This is the parent process! 1964
This is the child process! 1971
This is the parent process! 1964
This is the child process! 1972
This is the parent process! 1964
This is the child process! 1973
This is the parent process! 1964
This is the child process! 1974

父进程不断循环  能解不?

论坛徽章:
0
5 [报告]
发表于 2011-08-18 23:12 |只看该作者
  1. sleep(1);
复制代码
换成
  1. exit(0);
复制代码
还有就是,你不判断出错的情况么? 你真牛

论坛徽章:
0
6 [报告]
发表于 2011-08-19 09:17 |只看该作者
The vfork() function shall be equivalent to fork(), 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() or one of the exec family of functions.

调用vfork()之后,子进程要么_exit(),要么调用某个exec(),否则都是未定义行为!

另外:
This function is marked obsolescent.
别再纠结啦,放弃这个函数吧

论坛徽章:
0
7 [报告]
发表于 2011-08-19 10:59 |只看该作者
虽然没看懂,但是记得APUE上不推荐这个函数,好像说那个什么unix标准也放弃了这个接口

论坛徽章:
11
未羊
日期:2013-12-16 12:45:4615-16赛季CBA联赛之青岛
日期:2016-04-11 19:17:4715-16赛季CBA联赛之广夏
日期:2016-04-06 16:34:012015亚冠之卡尔希纳萨夫
日期:2015-11-10 10:04:522015亚冠之大阪钢巴
日期:2015-07-30 18:29:402015亚冠之城南
日期:2015-06-15 17:56:392015亚冠之卡尔希纳萨夫
日期:2015-05-15 15:19:272015亚冠之山东鲁能
日期:2015-05-14 12:38:13金牛座
日期:2014-12-04 15:34:06子鼠
日期:2014-10-16 13:40:4715-16赛季CBA联赛之八一
日期:2016-07-22 09:41:40
8 [报告]
发表于 2011-08-19 12:14 |只看该作者
This function is marked obsolescent. 有什么替代函数吗, 老实说, 在疯狂fork 然后 execl 的应用中, 这玩艺效率就算不是最高, 也肯定不是最低的。

至于为什么会循环, 很显然是vfork 修改了父进程的栈导致的。

论坛徽章:
0
9 [报告]
发表于 2011-08-19 12:32 |只看该作者
回复 5# zcheung


    这个仅仅用来测试vfork的导致的无限循环  目的非常明确  错误有没有 影响不大

论坛徽章:
0
10 [报告]
发表于 2011-08-19 12:34 |只看该作者
回复 7# 729926317


    fork 子进程   父子一起运行
  vfork 子进程   父等子之后再运行
当然  父价格wait 就等于 vfork了  .... 好吧  放弃vfork
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP