The Linux Channel :: Forum

what is difference between clone and fork?


what is difference between clone and fork? and also what is difference syscall between clone and fork in linux kernel?


  • Hi Ramin,

    In general you get the basic answer via man fork and man clone. But as we know it is not that sufficient ;)

    So to explain the same, I did this picture (doodle) for you. Imagine case 1, you have a large process with few threads (in this case 4 threads).
    The problem is if process crashes (or a thread crashes) entire process gets crashes.
    This problem I explained in my video. Note this video is not about process vs threads in a simple case, its about what is the true real-world usecase scenario:

    So if you use fork(), fork is essentially creating an independent child process (as described in my picture) with different PID. So you get independent memory space (int a, int b for example). But clone() is creating a template of parent, and sharing the same memory space. Its almost like thread.
    The fact is I never used clone() in my code. And I am unable to get a proper real-world use for clone() either. Personally I am not liking the clone() either. Instead of clone(), I can use a thread. Its just my opinion. May be I am missing. For now I dont like clone() for the amount of confusion it brings in a production software/code. (Lets ignore cases like test hello-world sample code.)

    For example in VirtualBox you have a clone VM option. There are 2 types of clone you can do. A complete 100% vm clone. Or a clone a VM settings, but share the VM hard-drive. This way you get 2 VMs with different setting with 2 options. 1) either get 100% clone/copy or 2) get 2 VMs with different settings but common disk/storage.

    Hope that helps.
  • And then there is the skb_clone() vs skb_copy().

    You can refer the documentation in the API comment of its source:
    * skb_clone - duplicate an sk_buff
    * @skb: buffer to clone
    * @gfp_mask: allocation priority
    * Duplicate an &sk_buff. The new one is not owned by a socket. Both
    * copies share the same packet data but not structure. The new
    * buffer has a reference count of 1. If the allocation fails the
    * function returns %NULL otherwise the new buffer is returned.
    * If this function is called from an interrupt gfp_mask() must be
    * %GFP_ATOMIC.

    struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
    { ...

    vs skb_copy() API:
    * skb_copy - create private copy of an sk_buff
    * @skb: buffer to copy
    * @gfp_mask: allocation priority
    * Make a copy of both an &sk_buff and its data. This is used when the
    * caller wishes to modify the data and needs a private copy of the
    * data to alter. Returns %NULL on failure or the pointer to the buffer
    * on success. The returned buffer has a reference count of 1.
    * As by-product this function converts non-linear &sk_buff to linear
    * one, so that &sk_buff becomes completely private and caller is allowed
    * to modify all the data of returned buffer. This means that this
    * function is not recommended for use in circumstances when only
    * header is going to be modified. Use pskb_copy() instead.

    struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
    { ...

  • Thanks of you for explain
  • welcome Ramin :)
  • Thanks for the information
Sign In or Register to comment.