What after exec() in ls command. Is the parent process printing the output to the console or the child

execforkipcprocess

I have a simple doubt on execution of the command ls. As per my understanding from the research I have done on the internet, I understood the below points.

  1. When we type ls command shell interprets that command.

  2. Then the shell process forks and creates the child process & the parent(shell) executes the wait() system call, effectively putting itself to sleep until the child exits.

  3. Child process inherits all the open file descriptors and the environment.

  4. Then The child process executes an exec() of the ls program. This reads the ls program from its program file on disk into the existing [child] process.

  5. When the ls program runs to completion it calls exit() and it sends a signal to its parent indicating the child has terminated.

My doubt starts from here onwards,as soon as ls finishes its tasks ,does it sends the result back to the parent process or it just itself displays the output to the screen?. If it sends the o/p back to parent, then is it using pipe() implicitly?

Best Answer

Typically the parent process waits until the child process ends by calling waitpid. The parent process gets the PID of the process from fork.

This means the child never signals the parent process in any way that it exited or what happened. This is done by the system and not the child process.

If you are talking about the output of the program, the parent typically never receives the output of the child process unless it provided fds. This also means that the child process prints the output and not the parent process. The parent process just receives information about the state of the process (for more information see the macros in the waitpid manpage)

Related Question