Hi all!
My question is about more philosophical than technical issues.
Before begin let me to describe my objective: multiprocess (not multithread) program with one "master" process and N "worker" processes. Program is linux-only, async, event-based web-server, like nginx. So, the main problem is how to spawn "worker" processes, that will be contain threads.
In linux world there are two ways to create process:
1). fork() (or clone(), or sys_clone(), not necessary in this case)
2). fork() + exec*() family
A short description for each way and what confused me in each of them.
First way with fork() is dirty, because forked process has copy (...on-write, i know) of parent memory: signal handlers, variables, file\socket descriptors, environ and other, e.g. stack and heap. In conclusion, after fork i need to...hmm..."clear memory", for example, disable signal handlers, socket connections and other horrible things, inherited from parent, because child has access to a lot of data that he was not intended - breaks encapsulation, and many side-effects is possible.
The general way for this case is run infinite loop in forked process to handle some data and do some magic with socket pair, pipes or shared memory for creating communication channel between parent and child before and after fork(), because socket descriptors reopen in child and used same socket as parent.
Also, this is nginx-way: it has one executable binary, that use fork() for spawn child process.
The second way is similar to first, but have a difference with usage one of exec*() function in child process after fork() for run external binary. One important thing is that exec*() loads binary in current (forked) process memory, automatic clear stack, heap and do all other nasty job, so fork will look like a clearly new instance of program without copy of parent memory or something other trash.
There has another problem with communication establishing between parent and child: because forked process after exec*() remove all data inherited from parent, that i need somehow create a socket pair between parent and child. For example, create additional listen socket (domain or in another port) in parent and wait child connections and child should connect to parent after initialization.
The first way is simple, but confuse me, that is not a clear process, just a copy of parent memory, with many possible side-effects and trash, and need to keep in mind that forked process has many dependencies to parent code. Second way needs more time to support two binary, and not so elegant like single-file solution, but much more secure and stable. Maybe, the best way is use fork() for process create and something to clear it memory without exec*() call, but I cant find any solution for this. The clone() syscall, that possibly may be used to create process, has CLONE_VM flag, that determine how parent memory will be used in fork: child share parent memory (like thread) or copy it, but clone() does not have any flag, that might be means something like "create process and dont share or copy parent memory".
In conclusion, I need help to decide which way to use: create one-file executable binary like nginx, and use fork(), or create two separate files, one with "server" and one with "worker", and use fork() + exec*(worker) or system() N times from "server", and want to know for pros and cons for each way, or maybe I missed something.
P.S. Also, i know that linux does not have any hard difference between process and thread, like Windows, but, the main case for process for me is isolated virtual memory (thread share it with parent) and best CPU utilization, scalability and stability of application. So, i decided, that process in my case cant be replaced with threads, and i need to find some way for process spawn.
Cheers,
Alex.