fork and vfork

微信公众号 2012-11-24 11:52:31
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

int global=5;

int main(void)
{
pid_t pid;
int var=1,i;
printf("The difference between fork and vfork:\n");
pid=vfork();
switch(pid)
{
case 0:
i=3;
while(i-->0)
{
printf("Child process is running\n");
global++;
var++;
}
printf("Child End:global=%d,var=%d\n",global,var);
break;
case -1:
perror("fork error:");
exit(1);
default:
i=5;
printf("%10d\n",var);
while(i-->0)
{
printf("Parent process is running\n");
global++;
var++;
}
printf("Parent End:global=%d,var=%d\n",global,var);
exit(0);
}
}


The program running result as follows:

The difference between fork and vfork:
Child process is running
Child process is running
Child process is running
Child End:global=8,var=4
4359003
Parent process is running
Parent process is running
Parent process is running
Parent process is running
Parent process is running
Parent End:global=13,var=4359008


My question is: The value of 'var' is 4359003, why not 4 ?



...全文
334 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
samuel417 2013-03-11
  • 打赏
  • 举报
回复
所以调用_exit(0)就OK了。
nadleeh 2012-11-24
  • 打赏
  • 举报
回复
看看vfork 和fork的区别。。。。
微信公众号 2012-11-24
  • 打赏
  • 举报
回复
Linux下,没有中文输入法……vfork创建的子进程,不是和父进程共享地址空间吗?那怎么全局变量可以,局部变量var没了呢???(我的调试环境是:Red hat Linux 2.6.32;GCC 4.4.4)
微信公众号 2012-11-24
  • 打赏
  • 举报
回复
引用 6 楼 enlinux 的回复:
首先,运行在哪个环境?cygwin?linux? 对vfork实现有差异 其次,如果在linux下 vfork的MAN手册中说其是未定义的. The vfork() function has the same effect as fork(), except that the behaviour is undefined if th……
calls any other function before successfully calling _exit() 非常感谢!
子善旬 2012-11-24
  • 打赏
  • 举报
回复
首先,运行在哪个环境?cygwin?linux? 对vfork实现有差异 其次,如果在linux下 vfork的MAN手册中说其是未定义的. The vfork() function has the same effect as fork(), except that the behaviour is undefined if the process cre- ated 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. 因为在退出前调用了printf函数,去掉printf,lz再试试呢
微信公众号 2012-11-24
  • 打赏
  • 举报
回复
引用 4 楼 gumh 的回复:
改爲下面的就可以。 C/C++ code?1234567891011121314151617181920212223242526272829303132333435363738394041424344#include <stdio.h>#include <sys/types.h>#include <unistd.h>#include <stdlib.h> int ……
为什么这样就可以了呢? 真心求解
prajna 2012-11-24
  • 打赏
  • 举报
回复
改爲下面的就可以。
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
 
int global=5;
 
int main(void)
{
    pid_t pid;
    int var=1,i;
    printf("The difference between fork and vfork:\n");
    pid=vfork();
    switch(pid)
    {
    case 0:
    i=3;
    while(i-->0)
    {
    printf("Child process is running\n");
    global++;
    var++;
    }
    printf("Child End:global=%d,var=%d\n",global,var);
    break;

    case -1:
    perror("fork error:");
    exit(1);

    default:break;

    }
    i=5;
    printf("%10d\n",var);
    while(i-->0)
    {
    printf("Parent process is running\n");
    global++;
    var++;
    }
    printf("Parent End:global=%d,var=%d\n",global,var);
    exit(0);
}
The difference between fork and vfork: Child process is running Child process is running Child process is running Child End:global=8,var=4 4 Parent process is running Parent process is running Parent process is running Parent process is running Parent process is running Parent End:global=13,var=9
prajna 2012-11-24
  • 打赏
  • 举报
回复
vfork最早起源于2.9BSD,它与fork的不同就在于它并不将父进程的地址空间完全复制到子进程中,因为子进程会立即调用exec.vfork出来的子进程是在父进程的空间中运行的,它的存在就是为了exec调用,所以它不需要复制这些东西。如果这时子进程修改了某个变量,这将影响到父进程. vfork与fork的另一区别是:vfork保证子进程先运行,在它调用exec或exit后父进程才可能调度运行。如果在调用这两个函数之前子进程依赖于父进程的进一步动作,则会导致死锁。 而fork的父子进程运行顺序是不定的,它取决于内核的调度算法. http://blog.csdn.net/buaalei/article/details/5348382

70,040

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧