fork系统调用的应用 [英] Applications of fork system call
问题描述
fork用于创建从其调用的过程的副本.通常,此后是对exec函数家族的调用.除此以外,还有叉的用法吗?我能想到一个.使用管道功能进行IPC.
fork is used to create a copy of process from which its called. This is typically followed by calls to exec family of functions. Are there any usages of fork other than this? I can think of one. Doing IPC with pipe functions.
推荐答案
是的.开始一个过程,进行一些数据初始化,然后产生多个工作程序是很常见的.它们在地址空间中都有相同的数据,即写时复制.
Yes of course. It's quite common to start a process, do some data initialization and then spawn multiple workers. They all have the same data in their address space and it's Copy On Write.
另一个常见的事情是让主进程为传入的每个连接监听TCP套接字和fork().这样,在并行处理现有连接的同时,新连接可以立即得到处理.
Another common thing is to have the main process listen to a TCP socket and fork() for every connection that comes in. That way new connections can be handled immediately while existing connections are handled in parallel.
我想您忘记了在fork()之后,两个进程都可以访问fork()之前的进程中存在的所有数据.
I think you're forgetting that after a fork(), both processes have access to all data that existed in the process before the fork().
这篇关于fork系统调用的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!