Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7704737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:44:53+00:00 2026-05-31T23:44:53+00:00

I need to create a function that, among other things, spawns a child process.

  • 0

I need to create a function that, among other things, spawns a child process.
I’d like to pass to this function an optional list of file discriptors so that, depending on the user needs, the child process input/output can be redirected.

I have seen dozens of people talking about how to do this using dup2 somewhat like this:

if( pid < 0 )   // error forking.
{
//...
}
else if( pid != 0 ) // Parent process.
{
    ret = waitpid( pid, &status, 0 );
    return  WEXITSTATUS( status );
}
else    // Child process.
{
    dup2( fd, STDIN_FILENO );   // Clone passed file discriptor.
    close( fd );            // Close the passed one, since we have already cloned.
    execvp( arglist[ 0 ], arglist );
}

Alright. All of this is in the Internet.
My question now is, how (or what is the best way) to redirect to /dev/null?

Should I force the user to open( /dev/null) and pass it as fd or is there any better way?


EDIT:

This is not as pretty as I wanted, but I could not find any better way, so I ended up passing an array of file names to wherever a user could want to redirect, respectively, STDIN, STDOUT and STDERR:

static int  do_exec( arglist_t arglist, const char *fio[ 3 ] )
{
DEBUG__( OSU_DEBUG_LEVEL_1, "fio = %p\n", fio );

    if ( fio )
    {
        if ( fio[ STDIN_FILENO ] )
        {
            int fd = open( fio[ STDIN_FILENO ], O_RDONLY );

            if ( -1 < fd )
            {
                dup2( fd, STDIN_FILENO );
                close( fd );
            }
        }

        if ( fio[ STDOUT_FILENO ] )
        {
            int fd = open( fio[ STDOUT_FILENO ], O_WRONLY | O_CREAT | O_APPEND );

            if ( -1 < fd )
            {
                dup2( fd, STDOUT_FILENO );
                close( fd );
            }
        }

        if ( fio[ STDERR_FILENO ] )
        {
            int fd = open( fio[ STDERR_FILENO ], O_WRONLY | O_CREAT | O_APPEND );

            if ( -1 < fd )
            {
                dup2( fd, STDERR_FILENO );
                close( fd );
            }
        }
    }

    return  execvp( arglist[ 0 ], arglist );
}
  • I haven’t yet fully tested it, so it may have some bugs.

Really thanks to @Zack and @gbulmer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:44:54+00:00Added an answer on May 31, 2026 at 11:44 pm

    You could have it as a convention of your API that passing in -1 for the fd means to use /dev/null, and do

    // ... same as you have ...
    
    else // Child process
    {
        if (stdin_fd == -1)
            stdin_fd = open("/dev/null", O_RDONLY);
        if (stdin_fd == -1)
            _exit(127);
        dup2(stdin_fd, STDIN_FILENO);
        close(stdin_fd);
    
        // similarly for stdout and stderr
    
        execvp(arglist[0], arglist);
        _exit(127);
    }
    

    (Psst: Every time you put spaces on the inside of your parentheses, God kills a kitten.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a function that uses a loop. This function will open
I need to create a function that takes month and year as parameters and
I need to create a function that receives a string and checks if the
I need to create a javascript function that checks if it has been a
i am trying to create sql procedure or function that need to find duplicate
i need to create a function in my application to set its available memory
I need to create a php search function for names and need to change
I need to be able to create a seperate function which creates a total
I'm writing a small networking program in C++. Among other things it has to
I've read (in Nish Sivakumar's book C++/CLI In Action among other places) that you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.