Short quesion:
I want wait in the parent for the child to be replaced with some exec call, not wait for terminate.
How can I do it?
(c language, linux platform)
You can't wait in a parent for the child to do some exec
, except by having some convention about IPC, e.g. deciding to send something (in the child) on a pipe(7) just before the exec
. You'll set up the pipe(2) before the fork(2). You might also use the Linux specific eventfd(2) for such IPC.
After the fork(2) and before any exec
you are running (in the child process) the same code as the parent. So it is up to you to implement such conventional communications.
BTW, generally, the child process does not do a lot of things after the fork
and before the exec
, so waiting for the exec
to happen is useless.... In the unlikely case an error happens -including failure of exec
- you just _exit
(usually with an exit code like 127).
You might consider ptrace(2) (with PTRACE_SYSCALL
...) but I would not do it that way.
Read Advanced Linux Programming and study the source code of some free software shells (sash
or bash
). Use also strace
to understand what is happening in a shell.