免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: nola_r
打印 上一主题 下一主题

[C] fork的问题,为什么父进程ID消失了 [复制链接]

论坛徽章:
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
11 [报告]
发表于 2012-11-14 12:42 |只看该作者

  1. DESCRIPTION
  2.    Standard Description
  3.        (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()
  4.        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
  5.        vfork() was called, or calls any other function before successfully calling _exit(2) or one of the exec(3) family of functions.

  6.    Linux Description
  7.        vfork(), just like fork(2), creates a child process of the calling process.  For details and return value and errors, see fork(2).

  8.        vfork()  is  a  special  case of clone(2).  It is used to create new processes without copying the page tables of the parent process.  It may be
  9.        useful in performance-sensitive applications where a child is created which then immediately issues an execve(2).

  10.        vfork() differs from fork(2) in that the calling thread is suspended until the child terminates (either normally, by calling _exit(2), or abnor-
  11.        mally,  after  delivery  of  a  fatal  signal), or it makes a call to execve(2).  Until that point, the child shares all memory with its parent,
  12.        including the stack.  The child must not return from the current function or call exit(3), but may call _exit(2).

  13.        As with fork(2), the child process created by vfork() inherits copies of various of the caller’s process  attributes  (e.g.,  file  descriptors,
  14.        signal  dispositions,  and current working directory); the vfork() call differs only in the treatment of the virtual address space, as described
  15.        above.

  16.        Signals sent to the parent arrive after the child releases the parent’s memory (i.e., after the child terminates or calls execve(2)).

  17.    Historic Description
  18.        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 dupli-
  19.        cate 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
  20.        a complete copy of the caller’s data space, often needlessly, since usually immediately afterward an exec(3) is done.  Thus, for  greater  effi-
  21.        ciency, BSD introduced the vfork() system call, which did not fully copy the address space of the parent process, but borrowed the parent’s mem-
  22.        ory 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
  23.        resources.   The  use of vfork() was tricky: for example, not modifying data in the parent process depended on knowing which variables were held
  24.        in a register.
复制代码
对vfork调return 0不对吧?

论坛徽章:
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
12 [报告]
发表于 2012-11-14 16:10 |只看该作者
对vfork调return 0不对吧?
@linux_c_py_php

???
can you wirte it in chinese?

论坛徽章:
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
13 [报告]
发表于 2012-11-15 21:31 |只看该作者
回复 1# nola_r

楼主坑爹,特意试了一下,没有发现这个问题,很正常

parent:
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. int main( void )
  4. {
  5.   if(fork()==0)
  6.   {
  7.      printf("pid-%d, ppid-%d.\n",getpid(), getppid());
  8.      execlp("./child.exe","child.exe",NULL);
  9.      *((int *)"no reach code")=1;
  10.   }
  11.   printf("the parent  process  is %d.\n",getpid());
  12.   while(1); /*or wait();*/
  13.   return 0; /*it is not impotant, but it is needed*/
  14. }
复制代码
child:
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. int main( void )
  4. {
  5.   printf("the child process  is %d.\n",getpid());
  6.   while(1); /*or wait();*/
  7.   return 0; /*it is not impotant, but it is needed*/
  8. }
复制代码
$cat /etc/issue
Ubuntu 12.04.1 LTS \n \l

论坛徽章:
0
14 [报告]
发表于 2012-11-22 17:46 |只看该作者
回复 13# folklore

没有坑谁呢,呵呵,哪有时间干这活呀,我还不如抽口烟
复制你的代码跑了一下,还是一样
orcl :~ # ./e3.exe
pid-22618, ppid-22617.
the parent  process  is 22617.  ---这个还是先于子进程跑完前出现了,而且你用的是fork 不是vfork()
the child process  is 22618.

论坛徽章:
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
15 [报告]
发表于 2012-11-22 17:58 |只看该作者
@nola_r
---这个还是先于子进程跑完前出现了

人家没保证这个要后出现啊,现出现和后出现都符合Fork语义
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP