I have a C program similar in structure to: http://www.csl.mtu.edu/cs4411/www/NOTES/process/fork/exec.html (that is, it’s a shell that runs one command with execvp when entered and loops indefinitely until “exit” is entered).
What is the best way to kill a child process immediately if an unrecognized command is passed to execvp? For example, if I typed “ehco” instead of “echo” how could I quickly kill this child process? I’ve noticed if I enter a command not in my PATH and then immediately type exit it doesn’t exit until I type exit again later.
The linked example already does the right thing: the child should unconditionally call
_exit()afterexecvp(). Theexecvp()will only return if it fails.(In other words, you don’t kill the child process from the parent; you wait for the child process to exit, and write the child process so that it kills itself if the exec fails).