posix_spawn(3) Library Functions Manual posix_spawn(3) posix_spawn, posix_spawnp - (libc -lc) #include int posix_spawn(pid_t *restrict pid, const char *restrict path, const posix_spawn_file_actions_t *restrict file_actions, const posix_spawnattr_t *restrict attrp, char *const argv[restrict], char *const envp[restrict]); int posix_spawnp(pid_t *restrict pid, const char *restrict file, const posix_spawn_file_actions_t *restrict file_actions, const posix_spawnattr_t *restrict attrp, char *const argv[restrict], char *const envp[restrict]); posix_spawn() posix_spawnp() . POSIX fork(2). MMU. posix_spawn() posix_spawnp() fork(2) exec(3) exec(3). fork(2) execve(2). . posix_spawn() posix_spawnp() . posix_spawn() ( ). posix_spawnp() PATH ( execvp(3)). posix_spawn() posix_spawnp() . : pid . file_actions fork(2) exec(3). posix_spawn() posix_spawn_file_actions_init(3) posix_spawn_file_actions_*(). attrp . posix_spawn() posix_spawnattr_init(3) posix_spawnattr_*(). argv envp execve(2). : fork() exec() ( ) exec() ( ). fork() glibc 2.24 posix_spawn() clone(2) CLONE_VM CLONE_VFORK. fork(2) vfork(2) ( ). PID *pid. posix_spawn() . wait(2) . 127. glibc 2.24 vfork(2) fork(2) : o spawn-flags attrp GNU POSIX_SPAWN_USEVFORK o file_actions NULL spawn-flags attrp POSIX_SPAWN_SETSIGMASK POSIX_SPAWN_SETSIGDEF POSIX_SPAWN_SETSCHEDPARAM POSIX_SPAWN_SETSCHEDULER POSIX_SPAWN_SETPGROUP POSIX_SPAWN_RESETIDS. vfork(2) exec(3) . (pre-exec()): fork() exec() . posix_spawn() posix_spawnp() . attrp file_actions. : (1) : attrp. (2) file_actions posix_spawn_file_actions_add*(). (3) FD_CLOEXEC. attrp file_actions fork(2) execve(2). attrp. spawn-flags ( posix_spawnattr_setflags(3)) . spawn-flags : POSIX_SPAWN_SETSIGMASK spawn-sigmask attrp. POSIX_SPAWN_SETSIGMASK . POSIX_SPAWN_SETSIGDEF spawn-sigdefault attrp . spawn-sigdefault POSIX_SPAWN_SETSIGDEF execve(2). POSIX_SPAWN_SETSCHEDPARAM POSIX_SPAWN_SETSCHEDULER spawn-schedparam attrp. POSIX_SPAWN_SETSCHEDULER : o spawn-schedpolicy attrp. o spawn-schedparam attrp ( ). POSIX_SPAWN_SETSCHEDPARAM POSIX_SPAWN_SETSCHEDPOLICY . POSIX_SPAWN_RESETIDS UID GID UID GID . UID GID . set-user-ID set-group-ID UID GID ( execve(2)). POSIX_SPAWN_SETPGROUP spawn-pgroup attrp. spawn-pgroup 0 . POSIX_SPAWN_SETPGROUP . POSIX_SPAWN_USEVFORK glibc 2.24 . fork() vfork(2) fork(2). _GNU_SOURCE . POSIX_SPAWN_SETSID ( glibc 2.26) . ( setsid(2)). _GNU_SOURCE . attrp (NULL) . file_actions exec(3). file_actions (NULL) exec(3) -- exec FD_CLOEXEC. . file_actions (NULL) open(2) close(2) dup2(2). file_actions posix_spawn_file_actions_addopen(3) posix_spawn_file_actions_addclose(3) posix_spawn_file_actions_adddup2(3). file_actions. ( ) 127. exec() exec . envp execve(2). argv execve(2). posix_spawn() posix_spawnp() (PID) pid 0. fork() *pid . exec(). exec(3). 127. posix_spawn() posix_spawnp() fork(2) vfork(2) clone(2) fork(2) vfork(2) clone(2). : ENOSYS . POSIX.1-2008. glibc 2.2. POSIX.1-2001. attrp ( ) file_actions. POSIX posix_spawnattr_t posix_spawn_file_actions_t . POSIX . ( .) POSIX fork pthread_atfork(3) posix_spawn(). glibc 2.24 fork . fork fork(2). "posix_fspawn" ( posix_spawn() fexecve(3) execve(2)). path /proc/self/fd . POSIX.1 POSIX_SPAWN_SETSCHEDULER spawn-flags POSIX_SPAWN_SETSCHEDPARAM ( ) . glibc 2.14 posix_spawn() POSIX_SPAWN_SETSCHEDULER POSIX_SPAWN_SETSCHEDPARAM . spawn POSIX. . . date(1) posix_spawn() . $ ./a.out date; PID of child: 7634 Tue Feb 1 19:47:50 CEST 2011 Child status: exited, status=0 -c . date(1) 1. $ ./a.out -c date; PID of child: 7636 date: write error: Bad file descriptor Child status: exited, status=1 -s ( ) . kill(1) ( SIGTERM) . SIGKILL ( SIGKILL). $ ./a.out -s sleep 60 & [1] 7637 $ PID of child: 7638 $ kill 7638; $ kill -KILL 7638; $ Child status: killed by signal 9 [1]+ Done ./a.out -s sleep 60 exec(3) 127. $ ./a.out xxxxx; PID of child: 10190 Child status: exited, status=127 #include #include #include #include #include #include #include #include #include char **environ; int main(int argc, char *argv[]) { pid_t child_pid; int s, opt, status; sigset_t mask; posix_spawnattr_t attr; posix_spawnattr_t *attrp; posix_spawn_file_actions_t file_actions; posix_spawn_file_actions_t *file_actionsp; /* Parse command-line options, which can be used to specify an attributes object and file actions object for the child. */ attrp = NULL; file_actionsp = NULL; while ((opt = getopt(argc, argv, "sc")) != -1) { switch (opt) { case 'c': /* -c: close standard output in child */ /* Create a file actions object and add a "close" action to it. */ s = posix_spawn_file_actions_init(&file_actions); if (s != 0) errc(EXIT_FAILURE, s, "posix_spawn_file_actions_init"); s = posix_spawn_file_actions_addclose(&file_actions, STDOUT_FILENO); if (s != 0) errc(EXIT_FAILURE, s, "posix_spawn_file_actions_addclose"); file_actionsp = &file_actions; break; case 's': /* -s: block all signals in child */ /* Create an attributes object and add a "set signal mask" action to it. */ s = posix_spawnattr_init(&attr); if (s != 0) errc(EXIT_FAILURE, s, "posix_spawnattr_init"); s = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK); if (s != 0) errc(EXIT_FAILURE, s, "posix_spawnattr_setflags"); sigfillset(&mask); s = posix_spawnattr_setsigmask(&attr, &mask); if (s != 0) errc(EXIT_FAILURE, s, "posix_spawnattr_setsigmask"); attrp = &attr; break; } } if (argv[optind] == NULL) { fprintf(stderr, "Usage: %s [-cs] executable [args]\n", argv[0]); exit(EXIT_FAILURE); } /* Spawn the child. The name of the program to execute and the command-line arguments are taken from the command-line arguments of this program. The environment of the program execed in the child is made the same as the parent's environment. */ s = posix_spawnp(&child_pid, argv[optind], file_actionsp, attrp, &argv[optind], environ); if (s != 0) errc(EXIT_FAILURE, s, "posix_spawn"); /* Destroy any objects that we created earlier. */ if (attrp != NULL) { s = posix_spawnattr_destroy(attrp); if (s != 0) errc(EXIT_FAILURE, s, "posix_spawnattr_destroy"); } if (file_actionsp != NULL) { s = posix_spawn_file_actions_destroy(file_actionsp); if (s != 0) errc(EXIT_FAILURE, s, "posix_spawn_file_actions_destroy"); } printf("PID of child: %jd\n", (intmax_t) child_pid); /* Monitor status of the child until it terminates. */ do { s = waitpid(child_pid, &status, WUNTRACED | WCONTINUED); if (s == -1) err(EXIT_FAILURE, "waitpid"); printf("Child status: "); if (WIFEXITED(status)) { printf("exited, status=%d\n", WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { printf("killed by signal %d\n", WTERMSIG(status)); } else if (WIFSTOPPED(status)) { printf("stopped by signal %d\n", WSTOPSIG(status)); } else if (WIFCONTINUED(status)) { printf("continued\n"); } } while (!WIFEXITED(status) && !WIFSIGNALED(status)); exit(EXIT_SUCCESS); } close(2), dup2(2), execl(2), execlp(2), fork(2), open(2), sched_setparam(2), sched_setscheduler(2), setpgid(2), setuid(2), sigaction(2), sigprocmask(2), posix_spawn_file_actions_addclose(3), posix_spawn_file_actions_adddup2(3), posix_spawn_file_actions_addopen(3), posix_spawn_file_actions_destroy(3), posix_spawn_file_actions_init(3), posix_spawnattr_destroy(3), posix_spawnattr_getflags(3), posix_spawnattr_getpgroup(3), posix_spawnattr_getschedparam(3), posix_spawnattr_getschedpolicy(3), posix_spawnattr_getsigdefault(3), posix_spawnattr_getsigmask(3), posix_spawnattr_init(3), posix_spawnattr_setflags(3), posix_spawnattr_setpgroup(3), posix_spawnattr_setschedparam(3), posix_spawnattr_setschedpolicy(3), posix_spawnattr_setsigdefault(3), posix_spawnattr_setsigmask(3), pthread_atfork(3), , Base Definitions volume of POSIX.1-2001, http://www.opengroup.org/unix/online.html 3 . . : . 6.18 8 2026 posix_spawn(3)