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

心不动则不痛 提交于 2019-12-10 18:14:53

问题


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.



来源:https://stackoverflow.com/questions/3864004/how-do-i-printf-after-a-call-to-execlp-in-a-child-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!