public subroutine system_signal(signum, handler_routine)
NAME
system_signal(3f) - [M_system:SIGNALS] install a signal handler
(LICENSE:PD)
SYNOPSIS
subroutine system_signal(sig,handler)
integer,intent(in) :: sig
interface
subroutine handler(signum)
integer :: signum
end subroutine handler
end interface
optional :: handler
DESCRIPTION
Calling system_signal(NUMBER, HANDLER) causes user-defined
subroutine HANDLER to be executed when the signal NUMBER is
caught. The same subroutine HANDLER maybe installed to handle
different signals. HANDLER takes only one integer argument which
is assigned the signal number that is caught. See sample program
below for illustration.
Calling system_signal(NUMBER) installs a do-nothing handler. This
is not equivalent to ignoring the signal NUMBER though, because
the signal can still interrupt any sleep or idle-wait.
Note that the signals SIGKILL and SIGSTOP cannot be handled
this way.
[Compare signal(2) and the GNU extension signal in gfortran.]
EXAMPLE
Sample program:
program demo_system_signal
use M_system, only : system_signal
implicit none
logical :: loop=.true.
integer, parameter :: SIGINT=2,SIGQUIT=3
call system_signal(SIGINT,exitloop)
call system_signal(SIGQUIT,quit)
write(*,*)'Starting infinite loop. Press Ctrl+C to exit.'
do while(loop)
enddo
write(*,*)'Reporting from outside the infinite loop.'
write(*,*)'Starting another loop. Do Ctrl+\ anytime to quit.'
loop=.true.
call system_signal(2)
write(*,*)&
& 'Just installed do-nothing handler for SIGINT. Try Ctrl+C to test.'
do while(loop)
enddo
write(*,*)'You should never see this line when running this demo.'
contains
subroutine exitloop(signum)
integer :: signum
write(*,*)'Caught SIGINT. Exiting infinite loop.'
loop=.false.
end subroutine exitloop
subroutine quit(signum)
integer :: signum
STOP 'Caught SIGQUIT. Stopping demo.'
end subroutine quit
end program demo_system_signal
AUTHOR
LICENSE
Arguments
Type |
Intent | Optional | Attributes |
|
Name |
|
integer,
|
intent(in) |
|
|
:: |
signum |
|
procedure(handler),
|
|
optional |
|
:: |
handler_routine |
|
Source Code
subroutine system_signal(signum,handler_routine)
integer, intent(in) :: signum
procedure(handler), optional :: handler_routine
type(c_funptr) :: ret,c_handler
interface
function c_signal(signal, sighandler) bind(c,name='signal')
import :: c_int,c_funptr
integer(c_int), value, intent(in) :: signal
type(c_funptr), value, intent(in) :: sighandler
type(c_funptr) :: c_signal
end function c_signal
end interface
if(present(handler_routine))then
handler_ptr_array(signum)%sub => handler_routine
else
!x!handler_ptr_array(signum)%sub => null(handler_ptr_array(signum)%sub)
handler_ptr_array(signum)%sub => null()
endif
c_handler=c_funloc(f_handler)
ret=c_signal(signum,c_handler)
end subroutine system_signal