#include #include int main(int argc, char *argv[]) { // Initialize MPI MPI_Init(NULL, NULL); // Get parent MPI_Comm parent; MPI_Comm_get_parent(&parent); // If the process was not spawned if (parent == MPI_COMM_NULL) { puts("I was not spawned!"); // Spawn child process in loop char *cmd = argv[0]; char **cmd_argv = MPI_ARGV_NULL; int maxprocs = 1; MPI_Info info = MPI_INFO_NULL; int root = 0; MPI_Comm comm = MPI_COMM_SELF; MPI_Comm intercomm; int *array_of_errcodes = MPI_ERRCODES_IGNORE; for (;;) { MPI_Comm_spawn(cmd, cmd_argv, maxprocs, info, root, comm, &intercomm, array_of_errcodes); MPI_Comm_disconnect(&intercomm); } // If process was spawned } else { puts("I was spawned!"); MPI_Comm_disconnect(&parent); } // Finalize MPI_Finalize(); }