Tuesday, May 19, 2009

How Unix shell executes commands

Unix shell runs all the commands in a new forked child process. It means, the shell in which the command is invoked becomes the parent that creates a child process ( a shell process). The child in turn exec's the command by overlaying itself with the command's image. Why would the new process be created for executing the command? Can't the shell run the command in the same process. The answer is:
If the shell overlays itself with the command's process image, it would have nowhere to return when the command finishes executing. This would close the parent shell. Running "exec command_name" effectively does this i.e. runs the command that evetually closes the shell when the command execution is over. To avoid closing the invoking shell itself, all commands are run in a new child process by the shell.

We can use "strace" utility to track down the child process creation and execution system calls. Note that doing strace on the command will only trace the command after fork i.e. the output will show execve(...) followed by other syscalls ending with some exit call. This is so because we are tracing only the command which really is in the child process. To trace how the shell has created a new child process by fork() (clone() family in Linux), do strace on the current shell in a separate terminal and then run a command in the current shell.

PS: edited off the 'subshell' part as that is a different beast altogether...


Marc said...

Builtins can be executed directly in the process without creating a child.

The subshell may never really exist if the shell starts commands using vfork (or a wrapper like posix_spawn).

Rahul Singh said...

Hi Marc, Is it correct when we execute some command say cd, ls, mkdir, find or anything that is a builtin command, in this case shell doesnt executes the command in a new child shell.
But when we run a shell script, it runs the script in a new child shell.
Just want to know whether my understanding is correct.

Priscilla said...

I'm … very happy with your editing. Your writing reduced the length and
also improved clarity of the sentences. Most importantly, you kept the major...

Peridot Systems Chennai Complaints

Aasha said...

Really nice information you had posted. Its very informative and definitely it will be useful for many people

Digital Marketing For Small Business in Chennai

Nikisha mathi said...

It is really a great and useful piece of info. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thank you for sharing.
Seo Company in Chennai