免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2907 | 回复: 9

[C] vfork子进程中因为插入exit()与否为什么 会有如此大 的不同? [复制链接]

论坛徽章:
0
发表于 2013-05-09 20:21 |显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int g=100;

  4. int main()
  5. {       
  6.         pid_t pid;
  7.         int local=12;
  8.        
  9.         printf("this is in parant!\n");
  10.         if((pid=vfork())<0)
  11.         {
  12.                 printf("fork err!\0");
  13.                 exit(-1);
  14.         }else if(pid==0)
  15.         {
  16.                 ++g;
  17.                 local=20;
  18.                 exit(0);//<-------------这里,没有这句最后输出local=1, 有这句输出为local=20; 为什么 这么大的不同?求解。
  19.         }else
  20.         {
  21.                 sleep(13);
  22.                 printf("back to parant! again\n");
  23.                 printf("%d,%d\n",g,local);
  24.                 exit(0);
  25.         }
  26. }
复制代码

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
发表于 2013-05-09 20:28 |显示全部楼层
没有这句话,其实,main就Return了。Main Return后并不会马上结束,还要调用其它函数,最后会调用exit, 你看到的是其它函数中的变量的值。

论坛徽章:
0
发表于 2013-05-09 21:30 |显示全部楼层
子进程返回怎么会影响父进程,实在不懂,Main return是指子进程返回吧?如果是指父进程return,为什么 父进程还 会执行sleep()及以后的语句?
folklore 发表于 2013-05-09 20:28
没有这句话,其实,main就Return了。Main Return后并不会马上结束,还要调用其它函数,最后会调用exit, 你看 ...

论坛徽章:
0
发表于 2013-05-09 22:15 |显示全部楼层
ilogo1 发表于 2013-05-09 21:30
子进程返回怎么会影响父进程,实在不懂,Main return是指子进程返回吧?如果是指父进程return,为什么 父进 ...

    vfork 的子进程如果不exec的话,在 _exit 之前的任何变量修改都可能污染父进程,包括没有_exit()的显式或隐式exit()
    所以local是什么都不奇怪
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.

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
发表于 2013-05-10 08:13 |显示全部楼层
回复 3# ilogo1

You can image this:
  1.     applicatinentry:
  2.             crt_startup(env);
  3.             int rt =main(argc,argv,opt);
  4.             crt_leave();
  5.             exit(rt);
复制代码
memory map

crt_startup  | main | crt_leave | exit
                       |
                       +- exit    <<--

论坛徽章:
9
CU大牛徽章
日期:2013-04-17 11:06:23CU大牛徽章
日期:2013-04-17 11:08:52CU大牛徽章
日期:2013-04-17 11:09:10CU大牛徽章
日期:2013-04-17 11:09:40CU大牛徽章
日期:2013-04-17 11:09:57CU大牛徽章
日期:2013-04-17 11:10:17CU大牛徽章
日期:2013-05-20 10:43:41CU大牛徽章
日期:2013-05-20 10:44:06CU大牛徽章
日期:2013-05-20 10:44:16
发表于 2013-05-10 09:49 |显示全部楼层
:wink:

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
发表于 2013-05-10 11:13 |显示全部楼层
不要在vfork的子进程里做任何与execl无关的事情。

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
发表于 2013-05-10 15:43 |显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int g=100;
  4. int _main(){        
  5.         pid_t pid;
  6.         int local=12;
  7.         printf("address of local: %p,pid: %p\n",&local,&pid);
  8.         printf("this is in parant!\n");
  9.         if((pid=vfork())<0){
  10.                 printf("fork err!\0");
  11.                 exit(-1);
  12.         }else if(pid==0){
  13.                 ++g;
  14.                 local=20;
  15.                 exit(0);
  16.         }else{
  17.                 sleep(13);
  18.                 printf("back to parant! again\n");
  19.                 printf("%d,%d\n",g,local);
  20.                 exit(0);
  21.         }
  22.         return;        // -- Return to main
  23. }

  24. int modify_local(){
  25.         volatile pid_t pid;
  26.         volatile int _____NOTICE_THIS_VARIANT_______________________________local=1024;
  27.         printf("address of local: %p,pid: %p\n",&_____NOTICE_THIS_VARIANT_______________________________local,&pid);
  28.         return local+pid;
  29. }

  30. int main(){
  31.         _main();
  32.         modify_local();  
  33.         exit(0);
  34.         return 0;
  35. }
复制代码
回复 3# ilogo1


   

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
发表于 2013-05-10 15:49 |显示全部楼层
and you would understand that the "local variant" in the function main may be supprise modified by other functions.
in normally program don't know this. but if you call vfork in your program, the modification may became SIGNIFICANT.

it is why you local variant "local" be changed.

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
发表于 2013-05-10 15:52 |显示全部楼层
so that, in normally,it is a must to call exit or execl in your program immediately following the sys calling of vfork.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP