A posix_spawn Example in C to Create Child Process on Linux

The posix_spawn() and posix_spawnp() functions create a new child process from the specified process image constructed from a regular executable file. It can be used to replace the relative complex "fork-exec-wait" methods with fork() and exec(). However, compared to fork() and exec(), posix_spawn() is less introduced if you search on the Web. The posix_spawn() manual provides details. However, it is still not sufficient especially for beginners. Here, I give an example of C program using posix_spawn() to create child processes.

The program is to run the command by /bin/sh -c that you pass as the first argument to the program. The run.c source code is as follows.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>

extern char **environ;

void run_cmd(char *cmd)
    pid_t pid;
    char *argv[] = {"sh", "-c", cmd, NULL};
    int status;
    printf("Run command: %s\n", cmd);
    status = posix_spawn(&pid, "/bin/sh", NULL, NULL, argv, environ);
    if (status == 0) {
        printf("Child pid: %i\n", pid);
        if (waitpid(pid, &status, 0) != -1) {
            printf("Child exited with status %i\n", status);
        } else {
    } else {
        printf("posix_spawn: %s\n", strerror(status));

int main(int argc, char* argv[])
    return 0;

From the example, you can find the posix_spawn() has its advantages and flexibility over other similar ones although it is a little tedious with 6 arguments.

  • Different from system() and exec(), it returns the new child process’ pid which you can wait by waitpid(). Of course, system() is something you should forget that you have learned it in most situations.
  • Difference from fork()/vfork(), the logic you implement is within the same process and you do not need to think about which piece of code is executed by the child process and which is executed by the parent process. It also avoid problems from vfork().

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.


  1. Thanks for the example. Can you please let me know how to use the other NULL values that are used in the example?

  2. Hi

    What is environ variable here.
    And can you further explain how to let the child notinherit the parents FD.
    Is it possible that my child doesnt inherit any fd of parent.
    Well, Iam trying to run this using socket connection and the client fd is in CLOSED_WAIT state.
    As i am running a code with infite loop in it .
    This is making my socket to be in CLOSED_WAIT

    ex : run_cmd(/bin/infi_loop);

    1. Hi Tejas,

      You can find many details not covered in this post in the posix_spawn manual: https://www.systutorials.com/docs/linux/man/3p-posix_spawn/ :

      If file_actions is a null pointer, then file descriptors open in the calling process shall remain open in the child process, except for those whose close-on-exec flag FD_CLOEXEC is set (see fcntl()). For those file descriptors that remain open, all attributes of the corresponding open file descriptions, including file locks (see fcntl()), shall remain unchanged.

      If file_actions is not NULL, then the file descriptors open in the child process shall be those open in the calling process as modified by the spawn file actions object pointed to by file_actions and the FD_CLOEXEC flag of each remaining open file descriptor after the spawn file actions have been processed. The effective order of processing the spawn file actions shall be:

      The set of open file descriptors for the child process shall initially be the same set as is open for the calling process. All attributes of the corresponding open file descriptions, including file locks (see fcntl()), shall remain unchanged.
      The signal mask, signal default actions, and the effective user and group IDs for the child process shall be changed as specified in the attributes object referenced by attrp.
      The file actions specified by the spawn file actions object shall be performed in the order in which they were added to the spawn file actions object.
      Any file descriptor that has its FD_CLOEXEC flag set (see fcntl()) shall be closed.

      If file descriptor 0, 1, or 2 would otherwise be closed in the new process image created by posix_spawn() or posix_spawnp(), implementations may open an unspecified file for the file descriptor in the new process image. If a standard utility or a conforming application is executed with file descriptor 0 not open for reading or with file descriptor 1 or 2 not open for writing, the environment in which the utility or application is executed shall be deemed non-conforming, and consequently the utility or application might not behave as described in this standard.

      For your purpose to let the child not inherit parents FDs, you may have at least 2 choices.

      1. Add the FD_CLOEXEC flag to the the socket FD by fcntl https://www.systutorials.com/docs/linux/man/3p-fcntl/ .
      2. Specify a file_actions for the posix_spawn to close the FDs.

  3. “Of course, system() is something you should forget that you have learned it in most situations”

    what you mean about should forget? why?

  4. I believe you should use WIFEXITED and WEXITSTATUS when managing the child’s return status.

Leave a Reply

Your email address will not be published. Required fields are marked *