1

I do have a C++ program that runs on HPUX and Linux. My program creates 2 child process, parent waits for both child process to finish. When I execute my program form run directory as follows, run> myProgram

I get prints from both child process + parent process displayed. So I need to way to stop my child process to print onto the command prompt window. After child process are completed, I would like to turn on printing, so that parent can display the results.

Does anyone know how to turn on and turn off prints?

tshepang
  • 12,111
  • 21
  • 91
  • 136
usustarr
  • 418
  • 1
  • 5
  • 21
  • 1
    How are your child processes created? are they a fork from your parent, or are they a spawned from a system command? – Dan Apr 25 '11 at 20:56
  • 1
    Do you really want to turn stdout on and off? Could you not simply redirect them to something like a file and then read that upon completion? Or is that not what you want? – Bart Apr 25 '11 at 21:03

1 Answers1

1

Taking inspiration from this answer:

#include <stdio.h>

main()
{
    int    fd;
    fpos_t pos;

    printf("printing to stdout enabled\n");

    fflush(stdout);
    fgetpos(stdout, &pos);
    fd = dup(fileno(stdout));

    // Standard output redirected to the null device
    freopen("/dev/null", "w", stdout);

    f(); 

    // Standard output restored to its previous fd (the screen)
    fflush(stdout);
    dup2(fd, fileno(stdout));
    close(fd);
    clearerr(stdout);
    fsetpos(stdout, &pos);        /* for C9X */

    printf("printing to stdout enabled again\n");
}

f()
{
    printf("message sucked away by /dev/null");
}
Community
  • 1
  • 1
Julio Gorgé
  • 10,056
  • 2
  • 45
  • 60
  • Sadly I dont have enough votes to upvote u, sorry. Do you happen to know how to turn off printing into log file and then later turn it back on? – usustarr May 04 '11 at 21:56
  • I create the log file by doing output = new Outout(newLogFile); – usustarr May 04 '11 at 22:15