当前位置:   article > 正文

【fork与vfork的区别】_fork和vfork的区别

fork和vfork的区别

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

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

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

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

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

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

举例说明:

一、子进程拷贝父进程1

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <stdlib.h>
  6. int main(int argc, char *argv[])
  7. {
  8. pid_t pid = fork();
  9. if(pid == -1)
  10. {
  11. perror("fork");
  12. return -1;
  13. }
  14. if(pid == 0)
  15. {
  16. printf("Child process pid:%d My parents pid:%d \n",getpid(),getppid());
  17. }
  18. else
  19. {
  20. printf("prent pid:%d My parents pid:%d \n",getpid(),getppid());
  21. }
  22. return 0;
  23. }

运行结果:

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

子进程拷贝父进程2

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <stdlib.h>
  6. int main(int argc, char *argv[])
  7. {
  8. int count = 0;
  9. pid_t pid = fork();
  10. if(pid == -1)
  11. {
  12. perror("fork");
  13. return -1;
  14. }
  15. if(pid == 0)
  16. {
  17. count++;
  18. printf("count = %d\n",count);
  19. printf("Child process pid:%d My parents pid:%d \n",getpid(),getppid());
  20. }
  21. else
  22. {
  23. count ++;
  24. printf("count = %d\n",count);
  25. printf("prent pid:%d My parents pid:%d \n",getpid(),getppid());
  26. }
  27. return 0;
  28. }

运行结果:

为什么count都等于1呢?

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

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

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <stdlib.h>
  6. int main(int argc, char *argv[])
  7. {
  8. int count = 0;
  9. pid_t pid = vfork();
  10. if(pid == -1)
  11. {
  12. perror("fork");
  13. return -1;
  14. }
  15. if(pid == 0)
  16. {
  17. count++;
  18. printf("count = %d\n",count);
  19. printf("Child process pid:%d My parents pid:%d \n",getpid(),getppid());
  20. }
  21. else
  22. {
  23. count ++;
  24. printf("count = %d\n",count);
  25. printf("prent pid:%d My parents pid:%d \n",getpid(),getppid());
  26. }
  27. return 0;
  28. }

运行结果:

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

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

改进程序 

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <stdlib.h>
  6. int main(int argc, char *argv[])
  7. {
  8. int count = 0;
  9. pid_t pid = vfork();
  10. if(pid == -1)
  11. {
  12. perror("fork");
  13. return -1;
  14. }
  15. if(pid == 0)
  16. {
  17. count++;
  18. printf("count = %d\n",count);
  19. printf("Child process pid:%d My parents pid:%d \n",getpid(),getppid());
  20. _exit(0);
  21. }
  22. else
  23. {
  24. count ++;
  25. printf("count = %d\n",count);
  26. printf("prent pid:%d My parents pid:%d \n",getpid(),getppid());
  27. _exit(0);
  28. }
  29. return 0;
  30. }

运行结果:

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/181149
推荐阅读
相关标签
  

闽ICP备14008679号