views:

44

answers:

3

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?

+2  A: 

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.

sharth
Thank you. Both your explanation of exec() functions method of overwriting the current memory space and the recommendation to work around this are correct.
XBigTK13X
+1  A: 

"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.

Jas
+2  A: 

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.

Bart van Ingen Schenau