Re: "system" extension on Windows
- From: "Jugoslav Dujic" <jdujic@xxxxxxxxx>
- Date: Mon, 7 Jul 2008 10:32:08 +0200
Jugoslav Dujic wrote:
| Michel Olagnon wrote:
|| relaxmike wrote:
||| Hi all,
|||
||| I work on a Monte-Carlo simulation tool, which is based on the
||| fortran extension "system". At the core of the simulation, there is
||| a loop which calls an external program which simulates one
||| particular randomized set of parameters :
|||
||| do i=1,100000
||| sysres = system("externaltool.exe")
||| enddo
|||
||| The problem is that the simulation fails at a random number of of
||| iterations,
||| sometimes 32 000, sometimes less. A message appears in the console,
||| which depends on the run, for example :
||| forrtl: severe (9): permission to access file denied, unit 32,
||| file ....
||| forrtl: severe (29): file not found, unit 7, file ...
||| etc...
|||
||| I cannot write here the full source code because it is quite
||| complicated.
||| The problem appears on Windows XP, with Intel Fortran 8.1.
||| Strangely, it does not appear on Windows with either gfortran or g95,
||| and I have no explanation for that fact.
||| The bug may be a bug in our source code, a bug in the compilers, or
||| a bug in the OS.
||
||
|| In my experience, this sort of call in a loop may allocate resources
|| (buffers, or tables of child process numbers, or whatsoever)
|| that may be kept (or even only not garbage collected) until the
|| program terminates, so it is usually better to avoid spanning
|| too many processes from a single program.
|
| Indeed. There's a handle leak, reported by Task Manager.
| (switch on "Handles" column in "Processes" tab to see what I mean).
| As a consequence, there's a permanent kernel memory leak (as one
| can see on "Performance" tab). At least on my computer, I had to
| make a restart to revive the system (which went sluggish) after
| 47,000 iterations.
|
| I don't currently have time to play with it; I'll try native
| Windows APIs (CreateProcess) rather than system() call, to see
| if it's a Intel RTL issue or Windows one. If the former,
| I'll have a word with Intel folks.
There's an apparent memory leak in Ifort's SYSTEM function. In the
attachment is a sample which works on Ifort using native Windows
APIs (adapted from Steve Lionel's anykey.f90 sample,
http://softwarecommunity.intel.com/isn/Community/en-US/forums/thread/116158.aspx).
It works much faster than SYSTEM and doesn't leak memory.
--
Jugoslav
___________
www.xeffort.com
Please reply to the newsgroup.
You can find my real e-mail on my home page above.
program appel
use ifport
implicit none
integer i , sysres
integer :: timestart, timestop, count,countrate,countmax
real :: timer_elapse
call system_clock(timestart)
do i=1,100000
write(32,*) i
write(*,*) i
sysres = mysystem("toto.exe")
write(32,*) "result:", sysres
enddo
call system_clock(timestop)
call system_clock(count,countrate,countmax)
timer_elapse = real(timestop - timestart)/countrate
write(32,*) "elapsed : ", timer_elapse
contains
integer function mysystem(exename)
use dfwin
character(*), intent(in):: exename
character*(MAX_PATH) path
type (T_STARTUPINFO) :: StartupInfo
type (T_PROCESS_INFORMATION) :: ProcessInfo
integer ret, path_len
character*1 cret
! Get path to this exe
path_len = GetModuleFileName (NULL, path, len(path))
! Initialize StartupInfo - we won't use any of its fields
!
StartupInfo = T_STARTUPINFO(SIZEOF(StartupInfo),&
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
! Create a new process to run this executable, passing the
! complete executable path and a "-child" switch
! as the command line (anything that has two or more tokens),
! and specifying that it should inherit the handles
! (including console) of this process.
!
ret = CreateProcess (NULL_CHARACTER, & ! Application Name
trim(exename)//char(0), & ! Command line
NULL_SECURITY_ATTRIBUTES, &
NULL_SECURITY_ATTRIBUTES, &
TRUE, & ! InheritHandles
0, & ! CreationFlags
NULL, & ! Environment variables
NULL_CHARACTER, & ! Current directory
StartupInfo, &
ProcessInfo)
if (ret == 0) then
mysystem = GetLastError ()
else
! CreateProcess succeeded. Wait for the process to finish
!
ret = WaitForSingleObject (ProcessInfo%hProcess, INFINITE)
! Close handles, otherwise resources are lost
!
ret = GetExitCodeProcess(ProcessInfo%hProcess, loc(mysystem))
ret = CloseHandle (ProcessInfo%hThread)
ret = CloseHandle (ProcessInfo%hProcess)
end if
end function mysystem
end program appel
- Follow-Ups:
- Re: "system" extension on Windows
- From: relaxmike
- Re: "system" extension on Windows
- From: Steve Lionel
- Re: "system" extension on Windows
- References:
- "system" extension on Windows
- From: relaxmike
- Re: "system" extension on Windows
- From: Michel Olagnon
- Re: "system" extension on Windows
- From: Jugoslav Dujic
- "system" extension on Windows
- Prev by Date: Re: Best way to allocate array within subroutine
- Next by Date: Re: Best way to allocate array within subroutine
- Previous by thread: Re: "system" extension on Windows
- Next by thread: Re: "system" extension on Windows
- Index(es):
Relevant Pages
|
|