|
| 1 | +program wait_for_SIGTERM |
| 2 | +use, intrinsic :: ISO_C_binding |
| 3 | + |
| 4 | +use c_interface |
| 5 | + |
| 6 | +implicit none (type, external) |
| 7 | + |
| 8 | +logical, volatile :: interface_SIGTERM, interface_SIGUSR1 |
| 9 | + |
| 10 | +call init |
| 11 | + |
| 12 | +do while( .not. interface_SIGTERM) |
| 13 | +enddo |
| 14 | + |
| 15 | +contains |
| 16 | + |
| 17 | +subroutine init() |
| 18 | + |
| 19 | +call signalterm_c(c_funloc(catchSIGTERM)) |
| 20 | +call signalusr1_c(c_funloc(catchSIGUSR1)) |
| 21 | +call interface_setSIGTERM(.false.) |
| 22 | +call interface_setSIGUSR1(.false.) |
| 23 | + |
| 24 | +end subroutine init |
| 25 | + |
| 26 | +subroutine catchSIGTERM(signal) bind(C) |
| 27 | +!! Set global variable interface_SIGTERM to .true. |
| 28 | +!! details This function can be registered to catch signals send to the executable. |
| 29 | +integer(C_INT), value :: signal |
| 30 | + |
| 31 | + |
| 32 | +print'(a,i0)', ' received signal ',signal |
| 33 | +call interface_setSIGTERM(.not. interface_SIGTERM) |
| 34 | + |
| 35 | +end subroutine catchSIGTERM |
| 36 | + |
| 37 | + |
| 38 | +subroutine catchSIGUSR1(signal) bind(C) |
| 39 | +!! Set global variable interface_SIGUSR1 to .true. |
| 40 | +!! This function can be registered to catch signals send to the executable. |
| 41 | +integer(C_INT), value :: signal |
| 42 | + |
| 43 | +print'(a,i0)', ' received signal ',signal |
| 44 | +call interface_setSIGUSR1(.not. interface_SIGUSR1) |
| 45 | + |
| 46 | +end subroutine catchSIGUSR1 |
| 47 | + |
| 48 | + |
| 49 | +subroutine interface_setSIGTERM(state) |
| 50 | +!! Set global variable interface_SIGTERM. |
| 51 | +logical, intent(in) :: state |
| 52 | + |
| 53 | + |
| 54 | +interface_SIGTERM = state |
| 55 | +print*, 'set SIGTERM to',state |
| 56 | + |
| 57 | +end subroutine interface_setSIGTERM |
| 58 | + |
| 59 | + |
| 60 | +subroutine interface_setSIGUSR1(state) |
| 61 | +!! Set global variable interface_SIGUSR. |
| 62 | +logical, intent(in) :: state |
| 63 | + |
| 64 | + |
| 65 | +interface_SIGUSR1 = state |
| 66 | +print*, 'set SIGUSR1 to',state |
| 67 | + |
| 68 | +end subroutine interface_setSIGUSR1 |
| 69 | + |
| 70 | +end program |
0 commit comments