创建子进程时,是否有必要将数据从父进程复制到子进程? [英] When creating a child process is it necessary to copy data from the parent process to the child process?

查看:78
本文介绍了创建子进程时,是否有必要将数据从父进程复制到子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,在Linux中,fork()用于创建新进程.它为子进程分配新的内存,然后将数据从父进程复制到子进程,例如文件描述符.然后exec()可用于加载其自己的数据并覆盖进程空间.但是我想知道是否有必要将父进程的数据复制到子进程吗?如何将子进程的真实数据直接加载到其进程空间?

I learned that,in linux fork() is used to create a new process. It allocates new memory for the child process then copies data from parent process to the child process such as file descriptors. Then exec() can be used to load its own data and overwrite the process space. But I wonder is it necessary to copy data of parent process to child process? How about loading the real data of a child process to its process space directly?

推荐答案

详细了解叉(系统调用)地址空间写时复制

Read more about fork (system call), address space, virtual memory (the kernel is using the MMU), copy-on-write, processes ...

也请阅读高级Linux编程.它有几章解释了这些困难的概念.

Read also Advanced Linux Programming. It has several chapters explaining these difficult concepts.

父进程和子进程具有不同的地址空间,但是在分叉之后,父&子地址空间几乎相等(这要归功于虚拟内存和写时复制技术).唯一的不同是 fork(2)系统调用的结果(这几乎是创建流程的唯一方法)

Parent and child processes have different address spaces, but after the fork the parent & child address spaces are nearly equal (thanks to virtual memory & copy-on-write techniques). The only difference being the result of the fork(2) syscall (which is [almost] the only way to create a process)

execve(2)完全替换了地址调用过程的空间(和执行上下文),并用于启动新的可执行程序(通常是 ELF 二进制可执行文件).

The execve(2) is entirely replacing the address space (and execution context) of its invoking process and is used to start a new executable program (often an ELF binary executable).

您不需要将数据从父进程复制到子进程.内核正在为您神奇地做到这一点.

You don't need to copy data from parent to child process. The kernel is doing that magically for you.

您可能希望在父级和父级之间进行一些进程间通信(IPC)子级,通常通过管道(请阅读 pipe(7)& 管道(2)& 共享内存-如果您是新手,请避免使用它,因为这样做很棘手正确使用-但您应该注意同步.参见 shm_overview(7)& sem_overview(7)了解更多.

You may want to do some inter-process communication (IPC) between parent and child, usually thru pipes (read pipe(7) & pipe(2) & poll(2)...), to be set up before the fork. You might want to use shared memory -avoid it if you are a newbie, since it is tricky to use correctly- but you should care about synchronization. See shm_overview(7) & sem_overview(7) for more.

还使用 strace(1)并研究源代码一些免费软件外壳(例如sashbash)的代码

Use also strace(1) and study the source code of some free software shell (like sash or bash)

这篇关于创建子进程时,是否有必要将数据从父进程复制到子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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