Jump to content

Fork and Exec commands

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
BigLinux

BigLinux

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
I would like to know the difference between exec and fork commands?

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Fork() creates a child process (or in a shell creates a subshell from which to execute the code), whereas exec() replaces the currently running process (or shell) with the executable file given to exec, unless of course there's an error.

Essentially, fork = create new; exec = replace with.

- Zeke

#3
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
Note that (depending on the OS) fork() may replicate some stuff, like the IP, handle tables, ect... for the new process.

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
isn't it like that your program waits for exec to have ran finished, while fork starts the other process and keeps the current program running, doing whatever it should?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Actually don't think so. exec(), according to the always trustable Wikipedia, does not normally return to the calling process, and fork() starts an independent process. exec() can also return a status number to the calling process, whereas fork() cannot (at least I think not). Just technically in multithreaded environments it's all processes given unique PID's. exec() uses the same PID as it's calling process, fork() is given a unique PID, which is also returned to the parent process so that it can continue to refer to the child process through signals.

Edited by ZekeDragon, 31 July 2009 - 06:53 AM.
Learning something every day...