Fortran 语言中的睡眠

发布于 2024-11-27 12:35:19 字数 76 浏览 11 评论 0原文

有谁知道 Fortran 中休眠指定毫秒数的方法吗?我不想使用不可移植的系统调用,因此 Fortran 或 C 库固有的任何内容都是首选。

Does anyone know of an way to sleep for a given number of milliseconds in Fortran? I do not want to use non-portable system calls so anything intrinsic to Fortran or C libraries would be preferred.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淑女气质 2024-12-04 12:35:19

使用Fortran ISO C Binding使用C库sleep以秒为单位进行睡眠:

   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

Using the Fortran ISO C Binding to use the C library sleep to sleep in units of seconds:

   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
挽清梦 2024-12-04 12:35:19

您可以使用 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 

如果您担心该程序会在午夜左右崩溃,请为此程序添加一个特殊情况:)

You can use Fortran standard intrinsic functions to do this without C binding:

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

Assuming the executable is slp:

~$ time slp 1234

real        0m1.237s
user        0m1.233s
sys         0m0.003s 

Add a special case to this program if you are worried it will break around midnight :)

堇色安年 2024-12-04 12:35:19
      ! 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
      ! 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文