Graceful shutdown and ctl-C

Graceful shutdown and ctl-C

Hi,

I would like to gracefully shutdown a program using ctl-C in terms of making sure all intermedium results are saved to a file. I do not wish to 'interrupt' a program during writing to file.

The following program seems to do the trick. However I could only get it to work by inserting a line 

call sleep(0) 

before the if statement that checks if the is_killed variable is ==1.

Ctl-c doesn't interrupt the program if the call sleep(0) is removed.

Why is this so?

If anyone has a better way of gracefully interrupting a program with ctl-C then I would be most grateful 

Thanks,

RIchard

module global
integer :: is_killed
end module global

program test
use ifport
use global
implicit none

external :: warning_sigint
integer :: warning_sigint
integer :: result1, i
character(len=12) :: string

is_killed=0

i = signal(SIGINT, warning_sigint, -1)

do
   open(22,file='stuff',status='unknown')
   do i = 1,1000000
      write(22,*) i
   enddo
   close(22)
   print *, 'printed stuff'
   call sleep(0)
   if(is_killed == 1) stop
enddo

end program test

function warning_sigint (SIG_NUM)
use global
integer :: warning_sigint
integer :: SIG_NUM

print *, 'Process interupted by CTL-C '

warning_sigint = 1
is_killed = 1

end function warning_sigint

 

1 Beitrag / 0 neu
Nähere Informationen zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.