unix fork exec序列真的像听起来那样昂贵吗? [英] Is the unix fork exec sequence really as expensive as it sounds?

查看:115
本文介绍了unix fork exec序列真的像听起来那样昂贵吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关forkexec的考试,并且我的书说,每当需要在unix系统中运行新的(不同的)过程时,您都将分叉当前的过程,然后是execve.

I'm reading about fork and exec for an exam, and my book says that whenever it is needed to run a new (different) process in unix systems, you would fork the current process followed by an execve.

但是,它也表示每次调用fork时,父级的整个内存映像都会复制到新进程中.

However, it also says that whenever fork is called, the whole memory image of the parent is copied to the new process.

然后我的问题是:如果您的进程具有很大的内存映像,而您只想运行一个新进程,该怎么办?如果您要立即替换父流程中的所有数据,这是否浪费资源?

Then my question is: What if you have a process with a really big memory image, and you just want to run a new process? Isn't it a waste of resources to copy all the data from the parent process if you are just going to replace it immediately?

推荐答案

通常,fork不会实际复制所有内存,而是使用写时复制",这意味着只要不修改内存,就可以复制相同的页面被使用.但是,为避免以后没有足够的内存(进程应将其写入内存),必须分配足够的内存.

Usually the fork does not actually copy all the memory, but uses a "copy on write" which means that as long as the memory is not modified the same pages are used. However, to avoid not having enough memory later on (should the process write to the memory) enough memory must be allocated.

这意味着在不允许过度使用内存的系统上从大型进程派生,该内存必须可用.因此,如果您有8 GB的进程分支,那么至少在短时间内必须有16 GB可用.

This means that forking from large process on systems that do not allow overcommitting memory the memory must be available. So, if you have a 8 GB process forking, then for at least a short period of time 16 GB must be available.

另请参见vfork和posix_spawn.

See also vfork and posix_spawn for other solutions.

这篇关于unix fork exec序列真的像听起来那样昂贵吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆