improved signal handling

- possibility to catch SIGTERM
- functions to set SIG(TERM/USR1/USR2) explicitly
This commit is contained in:
Martin Diehl 2019-03-24 11:59:00 +01:00
parent 728dac5a48
commit 293f869fe5
3 changed files with 139 additions and 70 deletions

View File

@ -47,6 +47,10 @@ int chdir_c(const char *dir){
return chdir(dir); return chdir(dir);
} }
void signalterm_c(void (*handler)(int)){
signal(SIGTERM, handler);
}
void signalusr1_c(void (*handler)(int)){ void signalusr1_c(void (*handler)(int)){
signal(SIGUSR1, handler); signal(SIGUSR1, handler);
} }

View File

@ -13,8 +13,9 @@ module DAMASK_interface
implicit none implicit none
private private
logical, public, protected :: & logical, public, protected :: &
SIGUSR1, & !< user-defined signal 1 SIGTERM, & !< termination signal
SIGUSR2 !< user-defined signal 2 SIGUSR1, & !< 1. user-defined signal
SIGUSR2 !< 2. user-defined signal
integer, public, protected :: & integer, public, protected :: &
interface_restartInc = 0 !< Increment at which calculation starts interface_restartInc = 0 !< Increment at which calculation starts
character(len=1024), public, protected :: & character(len=1024), public, protected :: &
@ -23,7 +24,10 @@ module DAMASK_interface
public :: & public :: &
getSolverJobName, & getSolverJobName, &
DAMASK_interface_init DAMASK_interface_init, &
setSIGTERM, &
setSIGUSR1, &
setSIGUSR2
private :: & private :: &
setWorkingDirectory, & setWorkingDirectory, &
getGeometryFile, & getGeometryFile, &
@ -279,10 +283,12 @@ subroutine DAMASK_interface_init()
if (interface_restartInc > 0) & if (interface_restartInc > 0) &
write(6,'(a,i6.6)') ' Restart from increment: ', interface_restartInc write(6,'(a,i6.6)') ' Restart from increment: ', interface_restartInc
call signalusr1_c(c_funloc(setSIGUSR1)) !call signalterm_c(c_funloc(catchSIGTERM))
call signalusr2_c(c_funloc(setSIGUSR2)) call signalusr1_c(c_funloc(catchSIGUSR1))
SIGUSR1 = .false. call signalusr2_c(c_funloc(catchSIGUSR2))
SIGUSR2 = .false. call setSIGTERM(.false.)
call setSIGUSR1(.false.)
call setSIGUSR2(.false.)
end subroutine DAMASK_interface_init end subroutine DAMASK_interface_init
@ -470,9 +476,36 @@ end function makeRelativePath
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
!> @brief sets global variable SIGUSR1 to .true. if program receives SIGUSR1 !> @brief sets global variable SIGTERM to .true.
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
subroutine setSIGUSR1(signal) bind(C) subroutine catchSIGTERM(signal) bind(C)
use :: iso_c_binding
implicit none
integer(C_INT), value :: signal
SIGTERM = .true.
write(6,'(a,i2.2,a)') ' received signal ',signal, ', set SIGTERM'
end subroutine catchSIGTERM
!--------------------------------------------------------------------------------------------------
!> @brief sets global variable SIGTERM
!--------------------------------------------------------------------------------------------------
subroutine setSIGTERM(state)
implicit none
logical, intent(in) :: state
SIGTERM = state
end subroutine setSIGTERM
!--------------------------------------------------------------------------------------------------
!> @brief sets global variable SIGUSR1 to .true.
!--------------------------------------------------------------------------------------------------
subroutine catchSIGUSR1(signal) bind(C)
use :: iso_c_binding use :: iso_c_binding
implicit none implicit none
@ -481,13 +514,25 @@ subroutine setSIGUSR1(signal) bind(C)
write(6,'(a,i2.2,a)') ' received signal ',signal, ', set SIGUSR1' write(6,'(a,i2.2,a)') ' received signal ',signal, ', set SIGUSR1'
end subroutine catchSIGUSR1
!--------------------------------------------------------------------------------------------------
!> @brief sets global variable SIGUSR1
!--------------------------------------------------------------------------------------------------
subroutine setSIGUSR1(state)
implicit none
logical, intent(in) :: state
SIGUSR1 = state
end subroutine setSIGUSR1 end subroutine setSIGUSR1
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
!> @brief sets global variable SIGUSR2 to .true. if program receives SIGUSR2 !> @brief sets global variable SIGUSR2 to .true. if program receives SIGUSR2
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
subroutine setSIGUSR2(signal) bind(C) subroutine catchSIGUSR2(signal) bind(C)
use :: iso_c_binding use :: iso_c_binding
implicit none implicit none
@ -496,6 +541,19 @@ subroutine setSIGUSR2(signal) bind(C)
write(6,'(a,i2.2,a)') ' received signal ',signal, ', set SIGUSR2' write(6,'(a,i2.2,a)') ' received signal ',signal, ', set SIGUSR2'
end subroutine catchSIGUSR2
!--------------------------------------------------------------------------------------------------
!> @brief sets global variable SIGUSR2
!--------------------------------------------------------------------------------------------------
subroutine setSIGUSR2(state)
implicit none
logical, intent(in) :: state
SIGUSR2 = state
end subroutine setSIGUSR2 end subroutine setSIGUSR2
end module end module

View File

@ -12,6 +12,7 @@ module system_routines
private private
public :: & public :: &
signalterm_C, &
signalusr1_C, & signalusr1_C, &
signalusr2_C, & signalusr2_C, &
isDirectory, & isDirectory, &
@ -19,7 +20,7 @@ module system_routines
getHostName, & getHostName, &
setCWD setCWD
interface interface
function isDirectory_C(path) bind(C) function isDirectory_C(path) bind(C)
use, intrinsic :: ISO_C_Binding, only: & use, intrinsic :: ISO_C_Binding, only: &
@ -53,6 +54,12 @@ interface
character(kind=C_CHAR), dimension(1024), intent(in) :: path ! C string is an array character(kind=C_CHAR), dimension(1024), intent(in) :: path ! C string is an array
end function chdir_C end function chdir_C
subroutine signalterm_C(handler) bind(C)
use, intrinsic :: ISO_C_Binding, only: &
C_FUNPTR
type(C_FUNPTR), intent(in), value :: handler
end subroutine signalterm_C
subroutine signalusr1_C(handler) bind(C) subroutine signalusr1_C(handler) bind(C)
use, intrinsic :: ISO_C_Binding, only: & use, intrinsic :: ISO_C_Binding, only: &
C_FUNPTR C_FUNPTR
@ -65,7 +72,7 @@ interface
type(C_FUNPTR), intent(in), value :: handler type(C_FUNPTR), intent(in), value :: handler
end subroutine signalusr2_C end subroutine signalusr2_C
end interface end interface
contains contains