I am currently trying to print a message from a child process after calling execlp() within the child. However, nothing appears on the terminal after the call to execlp(). What causes my printf() calls to not display anything, and how can this be resolved?
问题:
回答1:
After a successful execlp() call, no code in your previous program will ever run again. The process's memory space is overwritten with the new process.
If you still need to do some management with the child, then you will want to call fork() before you call execlp(). This will give you two processes, and you can then do some communication between the two.
回答2:
The exec*()
functions replace the process that called them with the executable provided as argument.
This means that, if the execlp
call is successful, then the child that made the call does no longer exist. Thus, any printf
statement following the execlp
can only be executed if the execlp
call fails, which typically means that the requested program does not exist.
回答3:
"The exec() family of functions replaces the current process image with a new process image"
(From: http://linux.die.net/man/3/execlp )
That explains it pretty clearly.