XBigTK13X
XBigTK13X

Reputation: 2755

How do I printf() after a call to execlp() in a child process?

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?

Upvotes: 3

Views: 4674

Answers (3)

Bill Lynch
Bill Lynch

Reputation: 81926

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.

Upvotes: 6

Bart van Ingen Schenau
Bart van Ingen Schenau

Reputation: 15768

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.

Upvotes: 3

Jas
Jas

Reputation: 1141

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

Upvotes: 1

Related Questions