【fork与vfork的区别】

fork()与vfork()都是创建一个进程,不过他们有以下几个区别:

1.fork(): 子进程拷贝父进程的数据段,代码段

   vfork(): 子进程与父进程共享数据段

2.fork(): 父子进程执行次序不确定

   vfork(): 保证子进程先运行,在调用exec()或exit()之前,与父进程数据共享,在exec()或exit()调用之后,父进程才能运行

3. 在使用vfork函数在调用exec()或exit()之前,子进程依赖于父进程的进一步动作,将会导致死锁。

举例说明:

一、子进程拷贝父进程1

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{ 
    pid_t pid = fork();
    if(pid == -1)
    {
        perror("fork");
        return -1;
    }
    
    if(pid == 0)
    {
        printf("Child process pid:%d My parents pid:%d \n",getpid(),getppid());    
    }
    else
    {
        printf("prent pid:%d My parents pid:%d \n",getpid(),getppid());    
        
    }

    return 0;
} 

运行结果:

 fork()函数的返回值有两个,子进程返回0,父进程返回子进程的进程号,进程号都是非零的正整数,所以父进程返回的值一定大于零。

子进程拷贝父进程2

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    int count = 0;
    pid_t pid = fork();
    if(pid == -1)
    {
        perror("fork");
        return -1;
    }
    
    if(pid == 0)
    {
        count++;
        printf("count = %d\n",count);
        printf("Child process pid:%d My parents pid:%d \n",getpid(),getppid());    
    }
    else
    {
        count ++;
        printf("count = %d\n",count);
        printf("prent pid:%d My parents pid:%d \n",getpid(),getppid());    
        
    }

    return 0;
} 

运行结果:

为什么count都等于1呢?

因为fork()函数子进程拷贝父进程数据段代码,所以count++将被父子进程各执行一遍,但是子进程执行自己的数据段里的count++,同样父进程执行时使自己的 数据段里的count++,他们互不影响,于是便出现了上面的结果。

二、vfork()函数执行上面的代码

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    int count = 0;
    pid_t pid = vfork();
    if(pid == -1)
    {
        perror("fork");
        return -1;
    }
    
    if(pid == 0)
    {
        count++;
        printf("count = %d\n",count);
        printf("Child process pid:%d My parents pid:%d \n",getpid(),getppid());    
    }
    else
    {
        count ++;
        printf("count = %d\n",count);
        printf("prent pid:%d My parents pid:%d \n",getpid(),getppid());    
    }

    return 0;
} 

运行结果:

本来vfork()是共享数据段的,结果应该是2,为什么不是呢?

因为vfork()保证子程序先运行,在他调用exex()或exit()后父进程才可能呗调度运行,如果在调度这两个函数之前子进程依赖于父进程的进一步动作,则会导致死锁,出现上面的结果。

改进程序 

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    int count = 0;
    pid_t pid = vfork();
    if(pid == -1)
    {
        perror("fork");
        return -1;
    }
    
    if(pid == 0)
    {
        count++;
        printf("count = %d\n",count);
        printf("Child process pid:%d My parents pid:%d \n",getpid(),getppid());  
        _exit(0);  
    }
    else
    {
        count ++;
        printf("count = %d\n",count);
        printf("prent pid:%d My parents pid:%d \n",getpid(),getppid());  
        _exit(0);  
    }

    return 0;
} 

运行结果:

加了_exit(0) 之后,则是在子进程退出后,父进程执行,这样else后的语句就会被父进程执行。

又因在子进程调用exec()或exit()之前与父进程共享的,所以子进程退出后把父进程的数据段count改成1,子进程退出后,父进程又执行,最终将count变成2,得到上面的结果。

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值