Search code examples
javaclonesingle-threadedfutex

Why a java single threaded program uses futex and clone system calls


This is a single threaded java program to find factorial of a given number passed as an argument, using recursion.

class factorial{
  public static void main(String[] args){
    int number=0;
    try {
         number = Integer.parseInt(args[0]);
    }
    catch(Exception e){
        e.printStackTrace();
    }
    double result = fact(num);
    System.out.println(result);
}

static double fact(int x){
    if(x <= 1){
        return x;
    }
    else{
        return(x * fact(x-1));
    }
  }
}

I was wondering why it is using so many futex and clone system calls are used for a program running as a single thread.

strace  -o javafact.txt -s10000 -f java factorial 5
grep -ic futex javafact.txt
481
grep -ic clone javafact.txt
53


19103 clone(child_stack=0x7f45ece2fff0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f45ece309d0, tls=0x7f45ece30700, child_tidptr=0x7f45ece309d0) = 19104
19103 futex(0x7f45ece309d0, FUTEX_WAIT, 19104, NULL <unfinished ...>

Solution

  • Java runs multiple JVM internal threads for GC, JIT compiler and some others. You could find some information here: http://blog.jamesdbloom.com/JVMInternals.html#jvm_system_threads