Skip to content

Commit 6ea72d1

Browse files
committed
add examples
1 parent 71facb3 commit 6ea72d1

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

example/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ add_subdirectory(stats_distribution_uniform)
3030
add_subdirectory(stringlist_type)
3131
add_subdirectory(strings)
3232
add_subdirectory(string_type)
33+
add_subdirectory(system)
3334
add_subdirectory(version)

example/system/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ADD_EXAMPLE(process_1)
2+
ADD_EXAMPLE(process_2)
3+
ADD_EXAMPLE(process_3)

example/system/example_process_1.f90

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
! Process example 1: Run a Command Synchronously and Capture Output
2+
program run_sync
3+
use stdlib_system, only: run, is_completed, process_type
4+
implicit none
5+
6+
type(process_type) :: p
7+
logical :: completed
8+
9+
! Run a synchronous process to list directory contents
10+
p = run("ls -l", wait=.true., want_stdout=.true.)
11+
12+
! Check if the process is completed (should be true since wait=.true.)
13+
if (is_completed(p)) then
14+
print *, "Process completed successfully. The current directory: "
15+
print *, p%stdout
16+
else
17+
print *, "Process is still running (unexpected)."
18+
end if
19+
20+
end program run_sync

example/system/example_process_2.f90

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
! Process example 2: Run an Asynchronous Command and check its status
2+
program run_async
3+
use stdlib_system, only: process_type, run, is_running, wait
4+
implicit none
5+
6+
type(process_type) :: p
7+
8+
! Run an asynchronous process to sleep for 5 seconds
9+
p = run("sleep 3", wait=.false.)
10+
11+
! Check if the process is running
12+
if (is_running(p)) then
13+
print *, "Process is running."
14+
else
15+
print *, "Process has already completed."
16+
end if
17+
18+
! Wait for the process to complete
19+
call wait(p)
20+
print *, "Process has now completed."
21+
end program run_async

example/system/example_process_3.f90

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
! Process example 3: Run with many arguments, and check runtime
2+
program run_with_args
3+
use stdlib_system, only: process_type, run, elapsed, wait
4+
implicit none
5+
6+
type(process_type) :: p
7+
character(len=15), allocatable :: args(:)
8+
9+
! Define arguments for the `echo` command
10+
allocate(args(2))
11+
args(1) = "echo"
12+
args(2) = "Hello, Fortran!"
13+
14+
! Run the command with arguments
15+
p = run(args, wait=.true.)
16+
17+
! Print the runtime of the process
18+
print *, "Process runtime:", elapsed(p), "seconds."
19+
20+
! Clean up
21+
deallocate(args)
22+
end program run_with_args

0 commit comments

Comments
 (0)