You are not logged in.

#1 2009-12-30 08:54:50

YZMSQ
Member
From: Hangzhou,Zhejiang,China
Registered: 2009-11-18
Posts: 97

(solved)Why is subshell?

Recently,I am reading a book called <Linux For Programmers And Users>.Now I encounter a problem: As the
book states,when a user wanna execute a command or script,the subshell will be created,and the parent shell will sleep until the command or script is executed up.However,in my opinion,why should linux create a subshell to execute the command rather than we just execute that in parent shell?I think it will be easier.
I need your help,please.:)

Last edited by YZMSQ (2009-12-31 04:47:04)


This is the way the world ends
Not with a bang but a whimper
                            -------T·S·Eliot

Offline

#2 2009-12-30 10:02:37

zukka
Member
From: Bologna Italy
Registered: 2006-06-27
Posts: 26

Re: (solved)Why is subshell?

If I remember correctly this is what happens:
- The parent shell forks (man 2 fork) and creates a new process that is a copy of itself.
- The new shell execs (man 2 execve) the binary or the script the user wanted to start. The exec replaces the code of the current process with the one of the program passed as argument to the system call.

If the parent shell would just exec the new process, there would not be a shell anymore ! smile

You can try yourself, open a terminal and type 'exec ls' or something like that, this does exactly what you say. It wouldn't be so comfortable wink

Offline

#3 2009-12-31 04:46:13

YZMSQ
Member
From: Hangzhou,Zhejiang,China
Registered: 2009-11-18
Posts: 97

Re: (solved)Why is subshell?

Thank You For your help!


This is the way the world ends
Not with a bang but a whimper
                            -------T·S·Eliot

Offline

Board footer

Powered by FluxBB