Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

When a process is started from a shell, why does the shell fork itself before executing the process?

For example, when the user inputs grep blabla foo, why can't the shell just call exec() on grep without a child shell?

Also, when a shell forks itself within a GUI terminal emulator, does it start another terminal emulator? (such as pts/13 starting pts/14)

share|improve this question

3 Answers 3

When you call a exec family method it doesn't create a new process, instead exec replaces the current process memory and instruction set etc with the process you want to run.

As an example, you want to run grep using exec. bash is a process (which has separate memory, address space). Now when you call exec(grep), exec will replace current process's memory, address space, instruction set etc with grep's data. That means bash process will no longer exist. As a result you can't get back to terminal after completing the grep command. That's why exec family methods never returns. You can't execute any code after exec; it is unreachable.

share|improve this answer
    
Almost ok --- I substituted Terminal with bash. ;-) –  Rmano Mar 2 at 18:00
1  
BTW, you can tell bash to execute grep without forking first, by using the command exec grep blabla foo. Of course, in this particular case, it won't be very useful (since your terminal window will just close as soon as the grep finishes), but it can be occasionally handy (e.g. if you're starting another shell, perhaps via ssh / sudo / screen, and don't intend to return to the original one, or if the shell process you're running this on is a sub-shell that's never meant to execute more than one command anyway). –  Ilmari Karonen Mar 2 at 19:50
6  
Instruction Set has very specific meaning. And it's not the meaning you are using it in. –  zespri Mar 3 at 19:15

As per the pts, check it yourself: in a shell, run

echo $$ 

to know your process-id (PID), I have for example

echo $$
29296

Then run for example sleep 60 and then, in another terminal

(0)samsung-romano:~% ps -edao pid,ppid,tty,command | grep 29296 | grep -v grep
29296  2343 pts/11   zsh
29499 29296 pts/11   sleep 60

So no, in general you have the same tty associated to the process. (Note that this is your sleep because it has your shell as the parent).

share|improve this answer

For each command ( example: grep) that you issue on the bash prompt, you actually intend to start a new process and then return to bash prompt after execution.

If the shell process ( bash) calls exec() to run grep, the shell process will be replaced with grep. Grep will work fine but after execution, the control cannot return to the shell because bash process is already replaced.

For this reason, bash calls fork(), which does not replace the current process.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.