You have to exec()
if you actually want a new program running in one of the processes (usually the child but not absolutely necessary). In your specific case where the shell executes ls
, the shell first forks, then the child process execs. But it's important to realise that this is two distinct operations.
All fork()
does is give you two (nearly) identical processes and you can then use the return code from fork()
to decide if you're the parent (you get the positive PID of the child, or -1 if the fork()
failed) or child (you get 0).
See this answer for a description on how fork()
and exec()
work together (under your control) and how they can be used without each other.
Similar to do_fork()
, the exec
stuff all boils down to calls to do_execve
, located in exec.c
.