#include #include #include #include #include extern char **environ; int main(void) { pid_t pid; char *args[] = {"/usr/bin/ls", "-l", NULL}; // Similar to doing a fork then an execve // spawn "/bin/ls" as a separate process if (posix_spawn(&pid, args[0], NULL, NULL, args, environ) != 0) { perror("spawn"); exit(1); } int status; // wait for spawned processes to finish if (wait(&status) == -1) { perror("waitpid"); exit(1); } if(WIFEXITED(status)){ printf("/bin/ls exited normally with status %d\n",WEXITSTATUS(status)); } return 0; }