Re: "system" extension on Windows



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


Relevant Pages

  • Re: API for TaskBar options
    ... Const ABS_ALWAYSONTOP As Long = &H2 ... Dim BarData As APPBARDATA, Ret As Long ... Call SHAppBarMessage(ABM_GETTASKBARPOS, BarData) ... > Can someone help me with the Windows task bar. ...
    (microsoft.public.excel.programming)
  • Problem starting windows forms application from ASP.Net 2.0
    ... I'm having trouble starting desktop windows forms application with gui. ... const int SecurityImpersonation = 2; ... ret = DuplicateTokenEx(Token,GENERIC_ALL,ref ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Is it possible to use %SystemRoot% in code
    ... Window system folder (different depending on the Windows OS version)? ... Dim sSave As String, Ret As Long ...
    (microsoft.public.vb.general.discussion)
  • Re: Removing duplicated XP
    ... The road to happiness. ... Do you know why you can't spell, Ret.? ... education, perhaps? ...
    (alt.os.windows-xp)
  • Re: Simulating SPI with AVR Studio
    ... I'm using avr studio for simulation. ... > Has anyone had any success simulating SPI with AVR Studio? ...
    (comp.arch.embedded)