Search code examples
fortrangfortranwindows-subsystem-for-linux

Fortran call to sleep does not block program


I have the most basic Fortran program:

program sleep
    print*, "Sleeping"
    call sleep(30)
    print*, "Done"
end program sleep

which I compile with gfortran sleep.f90 (version 9.3.0). From what I understood from the sleep documentation, this program is supposed to sleep for 30 seconds, i.e. I should expect to see "Done" being printed 30 seconds after "Sleeping". This doesn't happen: I see both print statements appearing instantaneously, suggesting that call sleep(30) does not block my program in any way. Doing call sleep(10000) didn't make any difference. I am compiling and running this program on a Windows Subsystem for Linux (WSL Ubuntu 20.04).


Solution

  • So this problem was fixed through a combination of solutions suggested by @roygvib in the comments. The main issue is that sleep in the WSL (Ubuntu 20.04) environment is broken. The first step is to replace the broken /usr/bin/sleep with this Python script:

    #!/usr/bin/env python3
    
    import sys
    import time
    
    time.sleep(int(sys.argv[1]))
    

    Then, the Fortran program is modified to make a system call to this new sleep executable:

    program sleep
        print*, "Sleeping"
        call system("sleep 30")
        print*, "Done"
    end program sleep
    

    Until the next update of WSL, this hack will have to do.