blasrodri
blasrodri

Reputation: 446

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()

But still, I'm trying to find a way to clone a running process and get a fork on the 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.

New solution (not sure how to implement it)

Using fork:

process_one
    |
    |
    * -clones-->forked_process

and then

another_process
    |
    |
    * -child-->forked_process

Upvotes: 1

Views: 627

Answers (1)

Luis Colorado
Luis Colorado

Reputation: 12708

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. This will make your fork() call more difficult to interpret it's return value in the main code. 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. 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. 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. 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.

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. 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. 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. Process lifes are independent from each other and the death of a process never conditions the life of another.

Upvotes: 1

Related Questions