what is fork and exec
fork() - system call which creates a new process which is identical to the current process. Differences are dependent on the implementation but consider them to be 100% same for now. returns 0 to the newly created process (child process), returns child's pid to the parent (old) process, returns -1 if there was an error and no fork made.
exec() - actually, multiple version of exec (execlp(), execvp(), etc.) replaces the current process with a new process (create an empty main(), compile it then run it with shell's exec, the shell will die). returns nothing (since your process no longer exists as soon as exec() is done)
How it comes together. The basic shell. When calling 'ls', the shell forks, the child then proceeds to do the execlp("ls","ls") call, while the parent simply waits for the child to exit and then print its prompt again. if while 'ls' is running, the parent dies, 'ls' is said to become a 'zombie' (living dead, undead, etc.). The kernel kills the child process (and you thought GTA was violent).
fork is the ONLY way to create a new process in UNIX
Jan 16, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment