Execing Processes in Co-array Fortran
Our example demonstrates how to execute external processes in Co-array Fortran. This is useful when we need to completely replace the current Fortran process with another (perhaps non-Fortran) one.
When we run our program, it will be replaced by the output of the ls
command:
Note that Co-array Fortran doesn’t offer a classic Unix fork
function. However, the EXECUTE_COMMAND_LINE
subroutine covers most use cases for executing external processes.
In this example, we’ve used the EXECUTE_COMMAND_LINE
subroutine, which is part of the Fortran 2008 standard. This subroutine allows us to execute shell commands from within our Fortran program. The wait=.true.
argument ensures that our program waits for the command to complete before continuing, and the exitstat
argument captures the exit status of the command.
Unlike in some other languages, Fortran doesn’t require us to explicitly set up environment variables or argument lists for the external command. The command is simply passed as a string to EXECUTE_COMMAND_LINE
.
Remember that the exact output you see when running this program will depend on the contents of the directory where you run it.