I need to fork an exec from a server. Since my servers memory foot print is large, I intend to use vfork()
/ linux clone()
. I also need to open pipes for stdin
/ stdout
/ stderr
. Is this allowed with clone()
/ vfork()
?
相关问题
- Multiple sockets for clients to connect to
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- how to get running process information in java?
I'd use
clone()
instead, usingCLONE_VFORK|CLONE_VM
flags; see man 2 clone for details.Because
CLONE_FILES
is not set, the child process has its own file descriptors, and can close and open standard descriptors without affecting the parent at all.Because the cloned process is a separate process, it has its own user and group ids, so setting them via
setresgid()
andsetresuid()
(perhaps callingsetgroups()
orinitgroups()
first to set the additional groups -- see man 2 setresuid, man 2 setgroups, and man 3 initgroups for details) will not affect the parent at all.The
CLONE_VFORK|CLONE_VM
flags mean thisclone()
should behave likevfork()
, with the child process running in the same memory space as the parent process up till theexecve()
call.This approach avoids the latency when using an intermediate executable -- it is pretty significant --, but the approach completely Linux-specific.
From the standard:
The problem with calling functions like
setuid
orpipe
is that they could affect memory in the address space shared between the parent and child processes. If you need to do anything beforeexec
, the best way is to write a small shim process that does whatever you need it to and thenexec
s to the eventual child process (perhaps arguments supplied throughargv
).