Advertisement
nocturnalmk

What is the difference between clone and fork in Linux?

Mar 26th, 2014
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. What is the difference between clone and fork in Linux?
  2.  
  3. Fork: The fork call basically makes a duplicate of the current process, identical in almost every way (not everything is copied over, for example, resource limits in some implementations but the idea is to create as close a copy as possible).The new process (child) gets a different process ID (PID) and has the the PID of the old process (parent) as its parent PID (PPID). Because the two processes are now running exactly the same code, they can tell which is which by the return code of fork - the child gets 0, the parent gets the PID of the child. This is all, of course, assuming the fork call works - if not, no child is created and the parent gets an error code.
  4.  
  5. Clone: Clone, as fork, creates a new process. Unlike fork, these calls allow the child process to share parts of its execution context with the calling process, such as the memory space, the table of file descriptors, and the table of signal handlers. When the child process is created with clone, it executes the function application fn(arg). (This differs from fork, where execution continues in the child from the point of the fork call.) The fn argument is a pointer to a function that is called by the child process at the beginning of its execution. The arg argument is passed to the fn function.When the fn(arg) function application returns, the child process terminates. The integer returned by fn is the exit code for the child process. The child process may also terminate explicitly by calling exit(2) or after receiving a fatal signal.
  6.  
  7.  
  8.  
  9.  
  10. http://wiki.answers.com/Q/What_is_the_difference_between_clone_and_fork_in_Linux?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement