|
| 1 | +! Process example 4: Kill a running process |
| 2 | +program example_process_kill |
| 3 | + use stdlib_system, only: process_type, run, is_running, kill, elapsed, has_win32, sleep |
| 4 | + implicit none |
| 5 | + type(process_type) :: process |
| 6 | + logical :: running, success |
| 7 | + |
| 8 | + print *, "Starting a long-running process..." |
| 9 | + if (has_win32()) then |
| 10 | + process = run("ping -n 10 127.0.0.1", wait=.false.) |
| 11 | + else |
| 12 | + process = run("ping -c 10 127.0.0.1", wait=.false.) |
| 13 | + endif |
| 14 | + |
| 15 | + ! Verify the process is running |
| 16 | + running = is_running(process) |
| 17 | + print *, "Process running:", running |
| 18 | + |
| 19 | + ! Wait a bit before killing the process |
| 20 | + call sleep(millisec=1250) ! Portable subroutine for sleeping |
| 21 | + |
| 22 | + print *, "Killing the process..." |
| 23 | + call kill(process, success) |
| 24 | + |
| 25 | + if (success) then |
| 26 | + print *, "Process killed successfully." |
| 27 | + else |
| 28 | + print *, "Failed to kill the process." |
| 29 | + endif |
| 30 | + |
| 31 | + ! Verify the process is no longer running |
| 32 | + running = is_running(process) |
| 33 | + print *, "Process running after kill:", running |
| 34 | +end program example_process_kill |
0 commit comments