在Fortran睡觉 [英] Sleep in Fortran

查看:280
本文介绍了在Fortran睡觉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道在Fortran中给定毫秒数的睡眠方法?我不想使用不可移植的系统调用,因此Fortran或C库内在的任何内容都将是首选。

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

 模块Fortran_Sleep 

use,intrinsic :: iso_c_binding,只有:c_int

隐式无

接口

!应该是unsigned int ...在Fortran
中不可用! OK,直到最高位被设置。
函数FortSleep(秒)bind(C,name =sleep)
导入
整数(c_int):: FortSleep $ b $整数(c_int),intent(in),VALUE :: seconds
结束函数FortSleep

结束接口

结束模块Fortran_Sleep


程序test_Fortran_Sleep

use,intrinsic :: iso_c_binding,仅:c_int

使用Fortran_Sleep

隐式无

整型(c_int):: wait_sec,how_long $ *
$ b write(*,'(Input sleep time:)',advance ='no')
read(*,*)wait_sec
how_long = FortSleep(wait_sec)

write(*,*)how_long

stop

end program test_Fortran_Sleep


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.

解决方案

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

这篇关于在Fortran睡觉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆