Possible to fork a process outside from it?
Well, it is obvious, let's say we have two processes A
and F
. F
wants to fork A
when it has the CPU contr开发者_Python百科ol (and A
is suspended since CPU is on F
).
I have Googled however nothing related showed up. Is such a thing possible in Unix environments?
There is definitely no standard and/or portable way to clone a process from the outside but depending on the OS, there are certainly possible ways to divert a process from its task and force it to clone itself or do whatever you want.
I don't think it's a good idea in any way, but it may be possible for process F
to attach to A
using a debugger interface such as ptrace
. Doing something like suspending the target process, saving its state, diverting the process to run fork
, then restoring its original state.
It should be noted that your cloning process will probably need to handle some odd cases around threads and the like.
No it's not possible.
The fork()
system call makes a copy of the parent, so if you call fork()
in the F
process, the child will be a copy of F
, there's nothing you can do to change this behavior.
The reason this is not possible is because, normally with fork()
, there is exactly one difference to start out with between the two processes: the return value of the fork() call itself. Without such a call inside the code of A
, there is no way for the processes to have any difference between them, so they would both be doing exactly the same thing, when normally with you want one of the processes to start doing something different.
How exactly do you think what you want to do should work?
No, this would be a huge security hole that would result in the leakage of sensitive information if it were possible.
At best, you could setup a signal handler in the parent process that would fork(2) off a child (maybe exec(2) a pre-configured child process?).
I think you would be better served by looking in to message passing between two processes that have CPU affinity setup, but even then, I think the gains would be nominal (over-optimizing a problem?).
http://www.freebsd.org/cgi/man.cgi?query=cpuset&apropos=0&sektion=0
精彩评论