Skip to content

Commit 237e9ff

Browse files
committed
add process killing example
1 parent e35b37a commit 237e9ff

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

example/system/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
ADD_EXAMPLE(process_1)
22
ADD_EXAMPLE(process_2)
33
ADD_EXAMPLE(process_3)
4+
ADD_EXAMPLE(process_4)

example/system/example_process_4.f90

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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

Comments
 (0)