Sleep

The Fortran standard does not include any routine for pausing a program for a given time. Most compilers, like GNU Fortran, provide a routine through extensions. As an alternative, a compiler-independent interface to the POSIX routine sleep(3) can be written, making the call compatible to all POSIX-compliant operating systems.

Extension

The sleep() routine of GNU Fortran pauses the process for the given time in seconds. Other compilers may provide similar extensions:

! sleep.f90
program main
    implicit none
    integer :: i

    do i = 1, 10
        print '("zzz ...")'
        call sleep(1) ! 1 sec.
    end do
end program main

POSIX

In order to sleep for less than a second, one might consider an ISO C binding interface to the POSIX function usleep(3), which pauses the process for a given interval in microseconds. The library fortran-unix includes the necessary Fortran interface binding, but we can instead just add a module that contains the interface declaration:

! usleep.f90
module posix
    use, intrinsic :: iso_c_binding
    implicit none
    private

    public :: c_usleep

    interface
        ! int usleep(useconds_t useconds)
        function c_usleep(useconds) bind(c, name='usleep')
            import :: c_int, c_int32_t
            implicit none
            integer(kind=c_int32_t), value :: useconds
            integer(kind=c_int)            :: c_usleep
        end function c_usleep
    end interface
end module posix

program main
    use :: posix
    implicit none
    integer :: i, rc, t

    t = 500 * 1000 ! 500 milliseconds

    do i = 1, 10
        print '("zzz ...")'
        rc = c_usleep(t)
    end do
end program main

Be aware that usleep(3) has been declared obsolescent. It is recommended to use nanosleep(2), but a little more complex derived type binding is required then.

Fortran Libraries