Skip to content

Commit 2113e9b

Browse files
committed
added doc and test
1 parent 1ffda0b commit 2113e9b

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

doc/specs/stdlib_system.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,31 @@ This is an `intent(inout)` argument, and on return is updated with the terminate
284284
{!example/system/example_process_4.f90!}
285285
```
286286

287+
## `send_signal`- send (POSIX) signal to a process
288+
289+
### Status
290+
291+
Experimental
292+
293+
### Description
294+
295+
The `send_signal` interface is used to send POSIX signal to a running external process.
296+
It attempts to send the signal and returns a boolean flag indicating whether the operation was successful.
297+
It does not do anything on Windows (no-op).
298+
299+
### Syntax
300+
301+
`call ` [[stdlib_subprocess(module):send_signal(subroutine)]] `(process, signal, success)`
302+
303+
### Arguments
304+
305+
`process`: Shall be a `type(process_type)` object representing the external process to be terminated.
306+
This is an `intent(inout)` argument, and on return is updated with the terminated process state.
307+
308+
`signal`: Shall be a `integer` variable representing the signal number to be sent.
309+
310+
`success`: Shall be a `logical` variable. It is set to `.true.` if the signal was sent successfully, or `.false.` otherwise.
311+
287312
## `sleep` - Pause execution for a specified time in milliseconds
288313

289314
### Status

test/system/test_subprocess.f90

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module test_subprocess
22
use testdrive, only : new_unittest, unittest_type, error_type, check, skip_test
3-
use stdlib_system, only: process_type, run, runasync, is_running, wait, update, elapsed, is_windows, kill
3+
use stdlib_system, only: process_type, run, runasync, is_running, wait, update, elapsed, is_windows, &
4+
kill, send_signal
45

56
implicit none
67

@@ -15,6 +16,7 @@ subroutine collect_suite(testsuite)
1516
new_unittest('test_run_synchronous', test_run_synchronous), &
1617
new_unittest('test_run_asynchronous', test_run_asynchronous), &
1718
new_unittest('test_process_kill', test_process_kill), &
19+
new_unittest('test_process_send_signal', test_process_send_signal), &
1820
new_unittest('test_process_state', test_process_state) &
1921
]
2022
end subroutine collect_suite
@@ -94,6 +96,44 @@ subroutine test_process_kill(error)
9496
call check(error, process%completed, "Process should be marked as completed after being killed")
9597
end subroutine test_process_kill
9698

99+
subroutine test_process_send_signal(error)
100+
type(error_type), allocatable, intent(out) :: error
101+
type(process_type) :: process
102+
logical :: success
103+
104+
! As the function does nothing on windows
105+
if (is_windows()) return
106+
107+
! Start a long-running process asynchronously
108+
process = runasync("ping -c 10 127.0.0.1")
109+
110+
! Ensure the process starts running
111+
call check(error, .not. process%completed, "Process should not be completed immediately after starting")
112+
if (allocated(error)) return
113+
114+
call check(error, is_running(process), "Process should be running immediately after starting")
115+
if (allocated(error)) return
116+
117+
! send SIGWINCH(28) to process, this should not cause the process to exit
118+
call send_signal(process, 28, success)
119+
call check(error, success, "Failed to send signal SIGWINCH to the process")
120+
if (allocated(error)) return
121+
122+
! Verify the process is still running
123+
call check(error, .not. process%completed, "Process should not exit after SIGWINCH signal")
124+
if (allocated(error)) return
125+
126+
! send SIGKILL to process
127+
call send_signal(process, 9, success)
128+
call check(error, success, "Failed to send signal SIGKILL to the process")
129+
if (allocated(error)) return
130+
131+
! Verify the process is no longer running
132+
call check(error, .not. process%completed, "Process should be completed after being killed")
133+
if (allocated(error)) return
134+
135+
end subroutine test_process_send_signal
136+
97137
!> Test updating and checking process state
98138
subroutine test_process_state(error)
99139
type(error_type), allocatable, intent(out) :: error

0 commit comments

Comments
 (0)