// simple example of posix_spawn // ./spawn_cat f.txt // The child will run /usr/bin/cat #include #include #include #include #include #include #include extern char **environ; int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } pid_t child_pid; char *argv_cat[] = {"/usr/bin/cat", argv[1], NULL}; int ret = posix_spawn(&child_pid, "/usr/bin/cat", NULL, NULL, argv_cat, environ); if (ret != 0) { fprintf(stderr, "spawn: %s\n", strerror(ret)); return 1; } int return_status; if (waitpid(child_pid, &return_status, 0) < 0) { perror(""); return 1; } printf("Raw exit status of child %d\n", return_status); return 0; }