The question is the title. I couldn't figure out why the terminal will shut down immediately after 'ls' is executed. A Linux shell is like this:
1.while (1) {
2. char *cmd = read_command();
3. int child_pid = fork();
4. if (child_pid == 0) {
5. exec(cmd);
6. }else {
7. waitpid(child_pid);
8. }
9.}
So, if we run 'exec ls' in shell, cmd is a string of 'exec ls'. A child process is forked in line 3. In line 5, exec(cmd) will replace the child process but won't affect the father process. If the father process is not affected, why the terminal shuts down then?
Please show me the flaws in my reasoning above.