简体   繁体   English

如何制作一个分离的fork进程?

[英]How to make a detached fork process?

I'm thinking of implementing a high available strategy by forking a process whenever certain signal is triggered.我正在考虑通过在触发某个信号时分叉一个进程来实现高可用性策略。 However, I'd like that new process not to be child of the process that executed the fork, but perhaps a child of some other process.但是,我希望新进程不是执行分叉的进程的子进程,而可能是其他进程的子进程。 This is important, since I need to ensure that this new fork does not die just because the other process did.这很重要,因为我需要确保这个新的分叉不会因为其他进程死掉而死。

I was looking at the differences between fork, ecev, execs, clone, etc here: The difference between fork(), vfork(), exec() and clone()我在这里查看了 fork、ecev、execs、clone 等之间的区别:fork()、vfork()、exec() 和 clone() 之间的区别

But still, I'm trying to find a way to clone a running process and get a fork on the go.但是,我仍然试图找到一种方法来克隆正在运行的进程并在 go 上获得一个分支。 Any ideas?有任何想法吗?

Using fork:使用分叉:

process_one
    |
    |
    * ----->forked_process

I'd like the forked_process to be a child of another process, but process_one.我希望 forked_process 成为另一个进程的子进程,但是 process_one。

New solution (not sure how to implement it)新解决方案(不确定如何实施)

Using fork:使用分叉:

process_one
    |
    |
    * -clones-->forked_process

and then接着

another_process
    |
    |
    * -child-->forked_process

You said:你说:

I'm thinking of implementing a high available strategy by forking a process whenever certain signal is triggered.我正在考虑通过在触发某个信号时分叉一个进程来实现高可用性策略。

You had better not to fork() a new process inside a signal handler, as the signal handler executes as an interrupt in user mode, after a system call.您最好不要在信号处理程序中fork()一个新进程,因为在系统调用之后,信号处理程序在用户模式下作为中断执行。 This will make your fork() call more difficult to interpret it's return value in the main code.这将使您的fork()调用更难以在主代码中解释它的返回值。 It is possible to do it, but will complicate unnecessary your code.可以这样做,但会使不必要的代码复杂化。

However, I'd like that new process not to be child of the process that executed the fork, but perhaps a child of some other process.但是,我希望新进程不是执行分叉的进程的子进程,而可能是其他进程的子进程。

The request you make is completely impossible to satisfy.你提出的要求是完全不可能满足的。 fork() just creates a child process in the paren that executed the fork() system cal. fork()只是在执行fork()系统校准的括号中创建一个子进程。 That new child process is a child of it, and is in the same state of execution as the parent process was when it called the fork() call (the one that started the fork() ) and the child process can only be distinguished from the parent by the return value of fork() , which is 0 for the child, and is the new process id of the child, in the parent.该新子进程是它的子进程,并且与父进程在调用fork()调用(启动fork()的那个)时处于相同的执行 state 中,并且子进程只能与父级由fork()的返回值,子级为0 ,是父级中子级的新进程 ID。 This makes completely impossible to select a parent (or to select which process you want the child to be the parent) as always the parent is the process that starts the call, and the child is the process that receives a return value of the call.这使得 select 成为父进程(或 select 哪个进程您希望子进程成为父进程)完全不可能,因为父进程是启动调用的进程,而子进程是接收调用返回值的进程。 I'm sorry but parent adoption is only handled automatically by the kernel, which makes the process with id 1 (this is init or systemd , depending on the system) to adopt automatically a child process when it's parent exit(2) s.很抱歉,父采用仅由 kernel 自动处理,这使得 id 为1的进程(这是initsystemd ,取决于系统)在父进程exit(2)时自动采用子进程。

This is important, since I need to ensure that this new fork does not die just because the other process did.这很重要,因为我需要确保这个新的分叉不会因为其他进程死掉而死。

There's no reason that links together the fork() or the parent/child relationship between processes, with the fact that one process has to die because because its parent did.没有理由将fork()或进程之间的父/子关系联系在一起,因为一个进程必须死掉,因为它的父进程死了。 No dying process makes another to die.没有死亡过程会导致另一个死亡。 When a parent process dies, the now orphan process is made a child of the process with id 1 .当父进程死亡时,现在的孤立进程将成为 id 为1的进程的子进程。 No reason to die.没有理由死。 Every process has a parent (always) and every process dies only when a signal is sent to it, or because it explicitly made an exit(2) system call.每个进程都有一个父进程(总是),每个进程只有在向它发送信号时才会死掉,或者因为它显式地进行了exit(2)系统调用。 Process lifes are independent from each other and the death of a process never conditions the life of another.进程的生命是相互独立的,一个进程的死亡永远不会影响另一个进程的生命。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM