making parent process wait till child has called exec - linux

In linux, after calling fork(), my child process is going to call exec soon. Is there a way for the parent process to wait() and not do anything till the child has exec'ed?
Thanks.

There is no (API) way for the parent to know that the child is performing an exec().
But there is a nice pipe-trick: have the child inherit a filedescriptor (for a pipe) and (before the fork() ) set the close-on-exec flag for the pipe. The parent will be notified by an EOF on the pipe when it is closed by the exec().
Please note that this does not need any collaboration from the child.

Use vfork() instead of fork(). That causes the parent to be suspended until the child either exits or calls one of the execve() family of functions.

You need to use waitpid using the process ID returned from the fork call that is returned to the parent.
EDIT
Or if you mean that you want to know that the child is about to call exec use pause in the parent. Get the child to call kill with a suitable signal to the parent (whose process ID can be obtained from getppid). USR1 signal might be useful to use. Do this just before the exec.

Related

what is the safe way to notify parent process in signal handler?

When my child process crash, it need a lot of time to do coredump (because I dump hugepages) and then the parent got SIGCHLD, but it is too late for me. So I use signal handler in child process to notify parent process and then do coredump, I just want to send the child pid to parent process. But I am not sure which mechanism is safe in this case. pipe or ipc message queue or unix socket?
You haven't provided enough details, but I assume in almost all the languages that allow you to explicitly spawn a new process there is a mechanism to get the child PID right when it gets created.
For example in C when you call fork() if the child process is created successfully it returns the child PID.
Yeah, I choose POSIX message queue finally, though it may be unsafe to call mq_send() in signal handler.

Why would a child process call getppid()? What can the PID of the Parent be used for?

When calling fork() the PID of the parent is saved in the child's PCB. In which case would it be useful for the child to know its parent's PID?
A process may wish to know if it was started by the init process at system startup, for example, and modify its behaviour accordingly.
Or, it may wish to communicate with its parent, say, to indicate something is complete, by sending a signal.

is there a way to tell if a process is a child if it did fork and then setsid

If a process did fork then child did setsid, is there any way to tell that it was the child of the first process? Is there any way to kill such process together with its parent?
setsid does not destroy the parent/child relation. So you still can get the children of a process, e. g. as described here (link us ubuntu specific, but this works for any other distribution).
The parent process always can keep track of its direct children easily, as fork returns their id (grand children gets more tricky...) and send a signal to any of its children on exiting (gracefully).
prctl(PR_SET_PDEATHSIG, <signal>) (convenient way to make the children receive a signal, if parent dies), too, will survive a call to setsid.

How to get the pid of a process started using system call?

In a C program, in main I am calling a system function using system().
Now I want the pid of that process started by system().
Is there any way to get that pid ?
No, and in general it's not useful. When the call of system() returns to your program, the child process has terminated and been reaped, so there's no process (not even a zombie process) for it to reference.
If you need to start a process and retain its PID, you'll need to fork() a child yourself (noting the returned value in the parent), and in the child, exec() the command. In the parent, you now have the PID, and can use it (e.g. in waitpid()).

Set a guardian process for the child processes in case parent dies

There is a function in QNX procmgr_guardian which sets a child process as the guardian of the other child process in case of the parent's death.
Is there such functionality in Linux too ? How do I implement it in Linux? Any pointers are appreciated.
There is no direct method for monitoring a process that is not your own child. However, there is a hack you can use: Create a FIFO using pipe(); ensure that the process to be monitored holds the write end of the pipe (and that it is closed in all other processes). If the process dies, an EOF condition will be signalled in the read end of the pipe (ie, select will indicate a readable condition).
If you want the process to be reparented when its immediate parent dies, you may be able to achieve this with PID namespaces. By arranging for the 'guardian' process to be PID 1 in the PID namespace, it will inherit all orphaned processes in that namespace.

Resources