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 3220024
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:42:50+00:00 2026-05-17T15:42:50+00:00

On Linux, I have some C++ code where I want to execv another application.

  • 0

On Linux, I have some C++ code where I want to execv another application. That program outputs some data to the stderr. I therefore redirect the stderr by calling freopen() with stderr as the stream parameter. The thing is, I want to redirect the stderr for another process that is run.

Here is the scenario I am working with. I fork() the current process; in the child process, I redirect the stderr; then I execv() to run the separate application.

Firstly, I have a Sentry set up to redirect the stderr output. Here is the code:

class StderrSentry {
public:
  StderrSentry() {
    freopen( "NUL", "wt", stderr );
  }
  ~StderrSentry() {
    fclose( stderr );
  }
};

Then later in the code:

pid_t pid = fork();
int retval=-1;

if( pid < 0 ) {
  success = false;
}
else if( ! pid ) {  // child process
  StderrSentry stdErrSentry;   // REDIRECTING STDERR HERE!
  pid_t chid = setsid();
  if (chid == -1 ) {
    exit(-1);
  }
  else {
    //  HERE IS THE execv() call:
    if( execv(command[0].c_str(), const_cast<char**>(&c_args[0])) < 0 ) {
      exit( -1 );
    }
  }
}
// ... else etc...

Will the stderr redirect actually still technically be valid once the execv() call replaces the current process with the specified one?

This is actually behaving as I desire, but is it a fluke, or is this the way to do it?

I cannot activate the stderr redirect in the application that is run in the execv, since it is not my code.

Thanks for any info on this.

  • 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-17T15:42:50+00:00Added an answer on May 17, 2026 at 3:42 pm

    The redirection is very much valid.
    The execv call changes the process image, but the file descriptors remain untouched.

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

Sidebar

Related Questions

I have some Linux code that monitors our hardware by collecting temperatures, voltages, and
I have 3 Linux machines, and want some way to keep the dotfiles in
We have a Linux server application that is comprised of a number of open-source
We have a Linux application that makes use of OpenSSL's Python bindings and I
I have some source code that was compiled on Windows. I am converting it
I have come across various situations, where I want to store some formatted data
I have a Linux web server and I'd like to make some database tables
How do Window's programmers profile their native C++ code? On Unix/Linux you have gprof
Under Linux, my C++ application is using fork() and execv() to launch multiple instances
Assume that the following code is being executed by 10 threads. pthread_mutex_lock(&lock) Some trivial

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.