Skip to content

Commit fa02a72

Browse files
committed
added example
1 parent 2113e9b commit fa02a72

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

example/system/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ ADD_EXAMPLE(process_4)
77
ADD_EXAMPLE(process_5)
88
ADD_EXAMPLE(process_6)
99
ADD_EXAMPLE(process_7)
10+
ADD_EXAMPLE(process_8)
1011
ADD_EXAMPLE(sleep)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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

Comments
 (0)