Tutorial :Create a new independent process from another C process


Two C executables A and B exist. A and B communicate to each other through a socket.

B can be started independently or through A.

  1. If B is started first and A is started next, then A and B start properly without issues. Even if A is restarted, then there are no issues.

  2. If B is started through A, then A and B starts properly. But here the communication port is bound to both A and B. Here, if A is restarted, then A fails to start.

Since B is started through A, Process A is the parent of Process B.

So, is there any means by which the Process B can be started independently from Process A?

We tried using fork, but with fork when we try to start the exe, two process is being started instead of one.


Have you tried using a 'wrapper' executable that forks off twice - once for A and once for B - and then kills itself? This would have A and B alive as separate processes that are then inherited by the init process, and should be safely restartable.


Are you sure you are checking correctly the return value of fork()?


pid_t pid;  if (pid == 0) {      /* child */  }  else if (pid > 0) {      /* parent */  }  else {      /* error */  }  


Are you using a Unix domain socket or a regular network socket?

Which of the processes, A or B, is listening on the socket (passive open), and which is doing the active open?

When you decide that A should run B, how does the code determine that this is necessary?

Are you opening the socket before you fork and exec?

My impression from the information given is:

  • You use a network socket
  • B is the listener

But I could easily be mistaken.

I wonder if you run into issues because A has already created the active socket before you fork and exec B, so the active end of the socket is not closed cleanly when A terminates because B has a copy of the socket open for writing as well as A. When you fork, the child process should clean up unneeded file descriptors (such as sockets) before executing another process.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »