对于fork函数:

子进程只继承父进程的文件描述表,不继承但共享文件表项和i-node

父进程创建一个子进程之后,文件表项中的引用计数加1变为2,当父进程作close操作之后计数器减1,子进程还是可以使用文件表项,只有计数器减到0的时候才会释放该文件表项

fork函数测试:

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>int      glob = 6;      /* external variable in initialized data */int
main(void)
{int        var;        /* automatic variable on the stack */pid_t  pid;var = 88;printf("before vfork\n");   /* we don't flush stdio */FILE *fp = fopen("s.txt", "w");int fd = open("s_fd.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG);char *s = "hello andrew";ssize_t size = strlen(s) * sizeof(char);//标准IO函数带缓存功能-->全缓存fprintf(fp, "s: %s, pid : %d", s, getpid());  //实际上是先写到缓存中去的//缓存没有满程序没有结束,信息就会一直停留在缓存中//内核提供的IO系统调用(不带缓存)write(fd, s, size);//直接写入文件if ((pid = fork()) < 0) {perror("vfork error");} else if (pid == 0) {      /* child */glob++;                    /* modify parent's variables */var++;//fprintf(fp, "pid = %d, glob = %d, var = %d\n", getpid(), glob, var);//   _exit(0);               /* child terminates */}else{//父进程}//父子进程都要执行fprintf(fp, "pid = %d, glob = %d, var = %d\n", getpid(), glob, var);//fprintf(fp, "s: %s, pid : %d\n", s, getpid()); close(fp);close(fd);sleep(1);   exit(0);
}

测试结果:

使用fork函数的缓存区会复制到子进程中,在父子进程都需要执行

//父子进程都要执行fprintf(fp, "pid = %d, glob = %d, var = %d\n", getpid(), glob, var);

的时候父子进程中都含有上面:

fprintf(fp, "s: %s, pid : %d", s, getpid());  //实际上是先写到缓存中去的

fp缓存区中的内容,因此在执行的时候父子进程都能将  hello andrew输出到自己的缓存中去,一旦程序结束父子进程缓存区中的内容都会写入fp所指向的文件;

vfork函数测试:

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>int      glob = 6;      /* external variable in initialized data */int
main(void)
{int        var;        /* automatic variable on the stack */pid_t  pid;var = 88;printf("before vfork\n");   /* we don't flush stdio */FILE *fp = fopen("s.txt", "w");int fd = open("s_fd.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG);char *s = "hello andrew";ssize_t size = strlen(s) * sizeof(char);//标准IO函数带缓存功能-->全缓存fprintf(fp, "s: %s, pid : %d", s, getpid());  //实际上是先写到缓存中去的//缓存没有满程序没有结束,信息就会一直停留在缓存中//内核提供的IO系统调用(不带缓存)write(fd, s, size);//直接写入文件if ((pid = vfork()) < 0) {   //注意这里使用的是vfork函数perror("vfork error");} else if (pid == 0) {     /* child */glob++;                    /* modify parent's variables */var++;//fprintf(fp, "pid = %d, glob = %d, var = %d\n", getpid(), glob, var);//   _exit(0);               /* child terminates */}else{//父进程}//父子进程都要执行fprintf(fp, "pid = %d, glob = %d, var = %d\n", getpid(), glob, var);//fprintf(fp, "s: %s, pid : %d\n", s, getpid()); close(fp);close(fd);sleep(1);   exit(0);
}

测试结果:

在使用vfork的时候,可以看到,只有一个 hello andrew输出,因为vfork函数是不为子进程创建单独的分区的,而是和父进程共用一个,一旦子进程将缓存区中的内容输出,那么另一位缓存区中也不会在有内容,因为两者缓存区是相同的;

总结:vfork创建的子进程,子进程会先运行并且,不会复制父进程的内存空间。

linux中fork()函数与vfork()函数的区别相关推荐

  1. linux中fork函数与vfork函数的区别

    fork函数跟vfork函数一样能够创建进程,它们主要有两个区别 (1)区别一: vfork直接使用父进程存储空间,不拷贝. (2)区别二: vfork保证子进程先运行,当子进程调用exit退出后,父 ...

  2. 转:linux中fork()函数详解

    转:linux中fork()函数详解 https://blog.csdn.net/jason314/article/details/5640969 转载于:https://www.cnblogs.co ...

  3. Linux中fork()函数详解

    Linux中fork()函数详解 一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事, ...

  4. fork()函数与vfork()函数的区别

    1.fork()函数与vfork()函数 头文件: #include <sys/types.h> #include <unistd.h> 函数原型: pid_t fork(vo ...

  5. Linux中fork的秘密

    linux中fork()函数详解 一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事, ...

  6. 【linux】linux中fork()详解(实例讲解)|fork的运行机制

    目录 linux中fork()函数详解 从一道面试题谈linux下fork的运行机制 linux中fork()函数详解 原文:linux中fork()函数详解(原创!!实例讲解)_jason314的博 ...

  7. linux中export和source的作用和区别

    linux中export和source的作用和区别 2013-11-12 12:36 1039人阅读 评论(0) 收藏 举报 分类: linux(82) shell与export命令 用户登录到Lin ...

  8. linux中cat、more、less命令区别详解

    linux中cat.more.less命令区别详解 转自:https://blog.csdn.net/xyw_blog/article/details/16861681 众所周知linux中命令cat ...

  9. Linux中kil命令和pkill命令的区别

    Linux中kil命令和pkill命令的区别 kill命令格式: kill [参数][目标程序的端口号] 所以使用kill命令前需要使用ps命令查看目标程序的端口号: ps -ef | grep fi ...

最新文章

  1. creo扫描选择多条链作为轨迹_ProE/Creo圆轨迹可变扫描法创建弧顶面,用上便爱上(一)...
  2. ionic4监听返回事件 AppMinimize navController
  3. python数据建模数据集_Python中的数据集
  4. 前端学习(3266):js中this的指向
  5. 关于tensorflow的碎片
  6. 阿里云、腾讯云和华为云618活动细节对比
  7. Android开发笔记(九十三)装饰模式
  8. Linux下Grub命令配置详解
  9. angular.js之作用域scope'@','=',''
  10. java SSM(Spring+SpringMVC+MyBatis)maven项目 intellij idea 2017配置 MAC,(Linux,ubuntu,centos 只要更改相应目录即可)
  11. idea vscode快捷键
  12. rosbridge入门教程
  13. 分享一篇酷炫粒子风暴代码!
  14. 编程-----相反数求解算法
  15. jsp网页在线编辑器
  16. cs_Censorship_CreateUpdateDelete///cs_Censorships_Get
  17. 菜的抠脚团队正式成立
  18. 用ruby实现latex自动编译
  19. 软考题目之头结点、头指针和首元节点
  20. 如何鼠标悬浮显示隐藏图片

热门文章

  1. 教你配CISCO RIP(二)
  2. poj 3308(最小割求解最小点权覆盖)
  3. 数据结构(严蔚敏)之五——循环队列(c语言实现)
  4. golang之‘...‘的用法
  5. 微信小程序支付最容易犯的坑notify_url(支付回调)
  6. 基于vue的颜色选择器vue-color-picker
  7. 为ubuntu添加多媒体以及flash等等常用包
  8. 百度地图 使用两条平行线表示路线
  9. iOS学习 plist读取和写入文件
  10. cvThreshold()函数理解