|
| 1 | +! Process example 4: send a posix signal to a running process |
| 2 | +! no-op on Windows |
| 3 | +program example_process_send_signal |
| 4 | + use stdlib_system, only: process_type, runasync, is_running, send_signal, is_windows, sleep |
| 5 | + implicit none |
| 6 | + type(process_type) :: process |
| 7 | + logical :: running, success |
| 8 | + |
| 9 | + integer, parameter :: SIGTERM = 15 |
| 10 | + |
| 11 | + if (is_windows()) then |
| 12 | + print *, "This is a no-op on Windows" |
| 13 | + stop 0 |
| 14 | + end if |
| 15 | + |
| 16 | + print *, "Starting a long-running process..." |
| 17 | + ! choosing ping as SIGTERM causes it to exit |
| 18 | + process = runasync("ping -c 10 127.0.0.1") |
| 19 | + |
| 20 | + ! Verify the process is running |
| 21 | + running = is_running(process) |
| 22 | + print *, "Process running:", running |
| 23 | + |
| 24 | + ! Wait a bit before sending a signal |
| 25 | + call sleep(250) |
| 26 | + |
| 27 | + print *, "Sending SIGTERM to the process" |
| 28 | + call send_signal(process, SIGTERM, success) |
| 29 | + |
| 30 | + if (success) then |
| 31 | + print *, "Signal sent successfully" |
| 32 | + else |
| 33 | + print *, "Failed to send signal SIGTERM" |
| 34 | + endif |
| 35 | + |
| 36 | + ! wait a bit to see if process is running |
| 37 | + !call sleep(1) |
| 38 | + |
| 39 | + ! Verify the process is no longer running |
| 40 | + running = is_running(process) |
| 41 | + print *, "Process running after signal SIGTERM:", running |
| 42 | + |
| 43 | +end program example_process_send_signal |
0 commit comments