A question about programming execvp function in C language under Linux

       When implementing the shell program of the Linux pipeline command, two subprocesses are created in the main function, and l calls the execvp function to execute the command, but it is very strange that when the command can be executed, the program is executed normally, and the subprocess It also exits normally every time, but if the command cannot be recognized, the child process cannot exit, so every time this happens, the exit command must be called (the exit command is called in the parent process, and the shell program judges the received characters. When the string is "exit", it exits the entire program) When exiting the entire program, you always have to lose several times of exit. Later, I printed the pid of the process at the time of exit, and found that it basically takes three exits to exit, because the first two are to exit the two sub-processes created, and the last is to exit the parent process, but in principle, no matter what, The code of the process should exit after the execution of the code, but this one did not exit.
       Later, I learned that if the command execution fails, that is, when the execvp function fails, the child process cannot exit normally. You need to use exit to force the child process to exit, so you need to add a judgment at this time. When the execvp execution fails, it returns -1 , call exit(0) to exit the child process, otherwise the process is still there, resulting in the failure of the subsequent shell program to execute normally.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325344842&siteId=291194637