Skip to content

Commit d4422cf

Browse files
committed
move all examples to separate files
1 parent d8f8be7 commit d4422cf

File tree

5 files changed

+49
-100
lines changed

5 files changed

+49
-100
lines changed

doc/specs/stdlib_system.md

+9-99
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,7 @@ Returns an object of type `process_type` that contains information about the sta
9292
### Example
9393

9494
```fortran
95-
! Example usage with command line or list of arguments
96-
type(process_type) :: p(2)
97-
98-
! Run a simple command line asynchronously
99-
p(1) = runasync("echo 'Hello, world!'", want_stdout=.true.)
100-
101-
! Run a command using an argument list asynchronously
102-
p(2) = runasync(["/usr/bin/ls", "-l"], want_stdout=.true.)
95+
{!example/system/example_process_1.f90!}
10396
```
10497

10598
## `is_running` - Check if a process is still running
@@ -130,21 +123,7 @@ After a call to `is_running`, the `type(process_type)` structure is also updated
130123
### Example
131124

132125
```fortran
133-
! Example usage of is_running
134-
type(process_type) :: proc
135-
logical :: status
136-
137-
! Start an asynchronous process
138-
proc = run("sleep 10", wait=.false.)
139-
140-
! Check if the process is running
141-
status = is_running(proc)
142-
143-
if (status) then
144-
print *, "Process is still running."
145-
else
146-
print *, "Process has terminated."
147-
end if
126+
{!example/system/example_process_2.f90!}
148127
```
149128

150129
## `is_completed` - Check if a process has completed execution
@@ -177,21 +156,7 @@ After a call to `is_completed`, the `type(process_type)` structure is updated to
177156
### Example
178157

179158
```fortran
180-
! Example usage of is_completed
181-
type(process_type) :: proc
182-
logical :: status
183-
184-
! Start an asynchronous process
185-
proc = run("sleep 5", wait=.false.)
186-
187-
! Check if the process has completed
188-
status = is_completed(proc)
189-
190-
if (status) then
191-
print *, "Process has completed."
192-
else
193-
print *, "Process is still running."
194-
end if
159+
{!example/system/example_process_1.f90!}
195160
```
196161

197162
## `elapsed` - Return process lifetime in seconds
@@ -224,17 +189,7 @@ Otherwise, the total process duration from creation until completion is returned
224189
### Example
225190

226191
```fortran
227-
! Example usage of elapsed
228-
type(process_type) :: p
229-
real(RTICKS) :: delta_t
230-
231-
! Create a process
232-
p = run("sleep 5", wait=.false.)
233-
234-
! Check elapsed time after 2 seconds
235-
call sleep(2)
236-
delta_t = elapsed(p)
237-
print *, "Elapsed time (s): ", delta_t
192+
{!example/system/example_process_3.f90!}
238193
```
239194

240195
## `wait` - Wait until a running process is completed
@@ -270,15 +225,7 @@ If not provided, the subroutine will wait indefinitely until the process complet
270225
### Example
271226

272227
```fortran
273-
! Example usage of wait
274-
type(process_type) :: p
275-
276-
! Start an asynchronous process
277-
p = run("sleep 5", wait=.false.)
278-
279-
! Wait for process to complete with a 10-second timeout
280-
call wait(p, max_wait_time=10.0)
281-
print *, "Process completed or timed out."
228+
{!example/system/example_process_2.f90!}
282229
```
283230

284231
## `update` - Update the internal state of a process
@@ -306,20 +253,7 @@ This is an `intent(inout)` argument, and its internal state is updated on comple
306253
### Example
307254

308255
```fortran
309-
! Example usage of update
310-
type(process_type) :: p
311-
312-
! Start an asynchronous process
313-
p = run("sleep 5", wait=.false., want_stdout=.true., want_stderr=.true.)
314-
315-
! Periodically update the process state
316-
call update(p)
317-
318-
! After completion, print the captured stdout and stderr
319-
if (p%completed) then
320-
print *, "Standard Output: ", p%stdout
321-
print *, "Standard Error: ", p%stderr
322-
endif
256+
{!example/system/example_process_5.f90!}
323257
```
324258

325259
## `kill` - Terminate a running process
@@ -347,21 +281,7 @@ This is an `intent(inout)` argument, and on return is updated with the terminate
347281
### Example
348282

349283
```fortran
350-
! Example usage of kill
351-
type(process_type) :: p
352-
logical :: success
353-
354-
! Start a process asynchronously
355-
p = run("sleep 10", wait=.false.)
356-
357-
! Attempt to kill the process
358-
call kill(p, success)
359-
360-
if (success) then
361-
print *, "Process successfully killed."
362-
else
363-
print *, "Failed to kill the process."
364-
end if
284+
{!example/system/example_process_4.f90!}
365285
```
366286

367287
## `sleep` - Pause execution for a specified time in milliseconds
@@ -387,13 +307,7 @@ It ensures that the requested sleep duration is honored on both Windows and Unix
387307
### Example
388308

389309
```fortran
390-
! Example usage of sleep
391-
print *, "Starting sleep..."
392-
393-
! Sleep for 500 milliseconds
394-
call sleep(500)
395-
396-
print *, "Finished sleeping!"
310+
{!example/system/example_sleep.f90!}
397311
```
398312

399313
## `is_windows` - Check if the system is running on Windows
@@ -419,9 +333,5 @@ Returns a `logical` flag: `.true.` if the system is Windows, or `.false.` otherw
419333
### Example
420334

421335
```fortran
422-
if (is_windows()) then
423-
print *, "Running on Windows!"
424-
else
425-
print *, "Not running on Windows."
426-
end if
336+
{!example/system/example_process_1.f90!}
427337
```

example/system/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ ADD_EXAMPLE(process_3)
44
ADD_EXAMPLE(process_4)
55
ADD_EXAMPLE(process_5)
66
ADD_EXAMPLE(process_6)
7+
ADD_EXAMPLE(process_7)
8+
ADD_EXAMPLE(sleep)

example/system/example_process_5.f90

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
! Process example 5: Object-oriented interface
22
program example_process_5
3-
use stdlib_system, only: process_type, runasync, is_windows, sleep
3+
use stdlib_system, only: process_type, runasync, is_windows, sleep, update
44
implicit none
55
type(process_type) :: process
66

@@ -13,6 +13,9 @@ program example_process_5
1313
! Verify the process is running
1414
do while (process%is_running())
1515

16+
! Update process state
17+
call update(process)
18+
1619
! Wait a bit before killing the process
1720
call sleep(millisec=1500)
1821

example/system/example_process_7.f90

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
! Process example 7: Usage of `kill`
2+
program example_process_7
3+
use stdlib_system, only: process_type, runasync, kill
4+
implicit none
5+
6+
type(process_type) :: p
7+
logical :: success
8+
9+
! Start a process asynchronously
10+
p = runasync("sleep 10")
11+
12+
! Attempt to kill the process
13+
call kill(p, success)
14+
15+
if (success) then
16+
print *, "Process successfully killed."
17+
else
18+
print *, "Failed to kill the process."
19+
end if
20+
21+
end program example_process_7

example/system/example_sleep.f90

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
! Usage of `sleep`
2+
program example_sleep
3+
use stdlib_system, only: sleep
4+
implicit none
5+
6+
print *, "Starting sleep..."
7+
8+
! Sleep for 500 milliseconds
9+
call sleep(500)
10+
11+
print *, "Finished sleeping!"
12+
13+
end program example_sleep

0 commit comments

Comments
 (0)