誰かがFortranで与えられたミリ秒数の間眠る方法を知っていますか?移植性のないシステムコールは使いたくないので、FortranまたはCライブラリに固有のものが優先されます。
11279 次
3 に答える
15
Fortran ISO Cバインディングを使用して、Cライブラリのスリープを使用して秒単位でスリープします。
module Fortran_Sleep
use, intrinsic :: iso_c_binding, only: c_int
implicit none
interface
! should be unsigned int ... not available in Fortran
! OK until highest bit gets set.
function FortSleep (seconds) bind ( C, name="sleep" )
import
integer (c_int) :: FortSleep
integer (c_int), intent (in), VALUE :: seconds
end function FortSleep
end interface
end module Fortran_Sleep
program test_Fortran_Sleep
use, intrinsic :: iso_c_binding, only: c_int
use Fortran_Sleep
implicit none
integer (c_int) :: wait_sec, how_long
write (*, '( "Input sleep time: " )', advance='no')
read (*, *) wait_sec
how_long = FortSleep ( wait_sec )
write (*, *) how_long
stop
end program test_Fortran_Sleep
于 2011-08-03T19:36:22.503 に答える
5
Fortran標準組み込み関数を使用して、Cバインディングなしでこれを行うことができます。
program sleep
!===============================================================================
implicit none
character(len=100) :: arg ! input argument character string
integer,dimension(8) :: t ! arguments for date_and_time
integer :: s1,s2,ms1,ms2 ! start and end times [ms]
real :: dt ! desired sleep interval [ms]
!===============================================================================
! Get start time:
call date_and_time(values=t)
ms1=(t(5)*3600+t(6)*60+t(7))*1000+t(8)
! Get the command argument, e.g. sleep time in milliseconds:
call get_command_argument(number=1,value=arg)
read(unit=arg,fmt=*)dt
do ! check time:
call date_and_time(values=t)
ms2=(t(5)*3600+t(6)*60+t(7))*1000+t(8)
if(ms2-ms1>=dt)exit
enddo
!===============================================================================
endprogram sleep
実行可能ファイルがslpであると仮定します。
~$ time slp 1234
real 0m1.237s
user 0m1.233s
sys 0m0.003s
真夜中頃に壊れてしまうのではないかと心配な場合は、このプログラムに特別なケースを追加してください:)
于 2011-08-04T04:31:54.773 に答える
-1
! This is another option of making your fortran code to wait for x seconds
Integer :: iStart, iNew
Real*8 :: rWait, rDT
! rWait: seconds that you want to wait for; you can also set this as an (IN)
! variable if this code goes into a subroutine that is developed to be called
! from any part of the program.
rWait = 1.d0; rDT = 0.d0
call system_clock (iStart)
do while (rDT <= rWait)
call system_clock (iNew)
rDT = floatj (iNew - iStart) / 10000.d0
enddo
于 2018-08-19T20:34:44.953 に答える