Unix/Linux Forum: Difference between fork and exec command in Unix. - Unix/Linux Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Difference between fork and exec command in Unix. Rate Topic: -----

#1 User is offline   brownaryan Icon

  • Newbie (User Level: 0/10)
  • Group: Members
  • Posts: 5
  • Joined: 13-June 08

Posted 13 June 2008 - 09:08 PM

What is the difference between fork and exec command in Unix?
0

#2 User is offline   eggi Icon

  • User Level: 6/10
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 311
  • Joined: 25-November 07
  • Location:Grayslake, IL USA

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
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
0

#3 User is offline   brownaryan Icon

  • Newbie (User Level: 0/10)
  • Group: Members
  • Posts: 5
  • Joined: 13-June 08

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
0

#4 User is offline   eggi Icon

  • User Level: 6/10
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 311
  • Joined: 25-November 07
  • Location:Grayslake, IL USA

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
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
0

#5 User is offline   brownaryan Icon

  • Newbie (User Level: 0/10)
  • Group: Members
  • Posts: 5
  • Joined: 13-June 08

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
0

#6 User is offline   eggi Icon

  • User Level: 6/10
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 311
  • Joined: 25-November 07
  • Location:Grayslake, IL USA

Posted 01 July 2008 - 04:31 AM

Cool,

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
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Cambridge Plus :: Cambridgeshire Steam Rally :: PCB CAD Design, UK :: Classic British Bike Piston Rings
Unix Man Pages / Linux Man Pages :: HiFi Forum :: A14 Comments :: UNIX/Linux Forum Archives