What is the difference between fork and exec command in Unix?
Page 1 of 1
Difference between fork and exec command in Unix.
#2
Posted 14 June 2008 - 02:35 AM
Hey There,
Simply put, fork duplicates your shell (file descriptors, etc) in a subshell. Usually a forked process will then exec in that subshell.
exec exec's the command in your current shell, basically co-opting it.
In pure versions of exec, if you do, just at the shell prompt:
exec khs <-- instead of exec ksh
your shell will be replaced by khs, which doesn't exist, and you'll lose your shell (get logged out).
Redhat, OpenSolaris, and most Linux brands do a "fake exec" (really a fork and then exec) now, where, even when you call exec and flub it, you don't lose your shell.
Not a very technical explanation, but hopefully helpful
, Mike
Simply put, fork duplicates your shell (file descriptors, etc) in a subshell. Usually a forked process will then exec in that subshell.
exec exec's the command in your current shell, basically co-opting it.
In pure versions of exec, if you do, just at the shell prompt:
exec khs <-- instead of exec ksh
your shell will be replaced by khs, which doesn't exist, and you'll lose your shell (get logged out).
Redhat, OpenSolaris, and most Linux brands do a "fake exec" (really a fork and then exec) now, where, even when you call exec and flub it, you don't lose your shell.
Not a very technical explanation, but hopefully helpful
, Mike
The greatest viral marketing idea of all time, get your copy of this Free Report now!
----
Linux Tips, Trick and Advice -- The Linux and Unix Menagerie
----
Linux Tips, Trick and Advice -- The Linux and Unix Menagerie
#3
Posted 29 June 2008 - 05:37 PM
Thanks Mike. It was indeed helpful. But I am somehow still not able to know the precise difference between both as both the commands spwans a new process at the end.
Regards
Brownaryan
www.unixhelpline.blogspot.com
Regards
Brownaryan
www.unixhelpline.blogspot.com
#4
Posted 29 June 2008 - 08:10 PM
Hey, no problem,
You're correct. They both do spawn a new process. It's just the manner in which the do it. Try this experiment (dont' use RedHat bash, or any Linux shell that doesn't implement exec like it's supposed to (actually doing a fork and exec to save you from losing your session. I'm pretty sure Solaris and HP-UX, at least up to 9 do exec the "old fashioned way", or the "real way")
login to your shell
type:
blah
(this is doing the standard fork and exec - spawning a subshell and exec'ing the command there) you should get back something like
blah: command not found
and be returned to your command prompt. then type
exec blah
and you should be kicked out of your shell.
Taken with the above explanation, that kind of drives it home. exec spawns a new process by replacing the process space, and fork exec's a new process in a subshell.
Hope that helps
,Mike
You're correct. They both do spawn a new process. It's just the manner in which the do it. Try this experiment (dont' use RedHat bash, or any Linux shell that doesn't implement exec like it's supposed to (actually doing a fork and exec to save you from losing your session. I'm pretty sure Solaris and HP-UX, at least up to 9 do exec the "old fashioned way", or the "real way")
login to your shell
type:
blah
(this is doing the standard fork and exec - spawning a subshell and exec'ing the command there) you should get back something like
blah: command not found
and be returned to your command prompt. then type
exec blah
and you should be kicked out of your shell.
Taken with the above explanation, that kind of drives it home. exec spawns a new process by replacing the process space, and fork exec's a new process in a subshell.
Hope that helps
,Mike
The greatest viral marketing idea of all time, get your copy of this Free Report now!
----
Linux Tips, Trick and Advice -- The Linux and Unix Menagerie
----
Linux Tips, Trick and Advice -- The Linux and Unix Menagerie
#5
Posted 29 June 2008 - 08:30 PM
'exec spawns a new process by replacing the process space, and fork exec's a new process in a subshell.'
I got your above mentioned point Mike. Thanx
Brownaryan
I got your above mentioned point Mike. Thanx
Brownaryan
#6
Posted 01 July 2008 - 04:31 AM
Cool,
I seem to have over-explained again
Glad you're good to go!
, Mike
I seem to have over-explained again
Glad you're good to go!
, Mike
The greatest viral marketing idea of all time, get your copy of this Free Report now!
----
Linux Tips, Trick and Advice -- The Linux and Unix Menagerie
----
Linux Tips, Trick and Advice -- The Linux and Unix Menagerie
Page 1 of 1

Sign In
Register
Help


MultiQuote