?
Solved

Output from child process goes to "bit bucket" (using printf)

Posted on 2005-04-04
5
Medium Priority
?
296 Views
Last Modified: 2010-04-22
I'm writing a program in C++ for linux platform.  The program uses fork() to run a certain task as a child process.

When I place printf() calls in the child process, that output never shows up anywhere.  From everything I've found on the web, it seems that stdin and stdout are supposed to work the same in the child process as in the parent, and the child should inherit the parent's stdin, stdout, and stderr.  Doesn't printf go to stdout by default?  I've found that I can output messages using sprinf() / write(STDOUT_FILENO,...), but this is somewhat inconvenient.  More importantly, it's a sign that there's something I'm not getting about how this is supposed to work.

Where is the printf output is going, and how would I make it work from within a child process?  (I'm a veteran programmer, but new to linux.)

if (fork() == 0)
{
   // In child process

   printf( "This message goes nowhere\n");
   
   char buf[200] = "This message goes to the console\n";
   write( STDOUT_FILENO, buf, strlen(buf));
   _exit(0);
}


Thanks!
0
Comment
Question by:AgentFriday
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 360 total points
ID: 13704293
> Doesn't printf go to stdout by default?
It does, unless you explicitly close it.

From your code snippet, the printf should go to the stdout stream ... However, note that printfs are buffered while writes are not ... It could be a buffering issue ... try flushing the buffer

fflush(stdout);

> that output never shows up anywhere
Double check that ... output might not show in the order you expect but it should show up UNLESS you did close stdout somewhere ... What does printf return? It should be returning a +ve value
0
 
LVL 22

Assisted Solution

by:NovaDenizen
NovaDenizen earned 140 total points
ID: 13709277
printf() buffers should be automatically flushed when your exit().  Other than that, it's a bad idea to mix stdio functions (printf, fwrite, fputs, etc) and write() on the same file descriptor since you won't be sure which order they come out in.

Is there a reason you're using _exit() and not exit()?
0
 

Author Comment

by:AgentFriday
ID: 13711782
> Is there a reason you're using _exit() and not exit()?

All the documentation I've read says that you should use _exit() to end a child process, not exit().
0
 

Author Comment

by:AgentFriday
ID: 13711948
>  try flushing the buffer

Actually, I did discover fflush(stdout) yesterday... and it does seem to cause printf() output to go to the screen.  I had heard that printf output was line buffered, but the level of buffering and out-of-order output I am seeing is far more than 1 line at a time.  I changed the child process to only use write(), since that works.  I also added a printf() message just before and after the child process gets created.  The child process output (from write()) shows up 20 or 30 lines _before_ the pre-child printf().

> > that output never shows up anywhere
> Double check that ...
I did.  It doesn't show up anywhere... until I add the fflush() before the child calls _exit().  Apparently, unlike exit, _exit will NOT flush the stdio buffers.

I had given up on fflush() earlier, since I tried it with the descriptor (STDOUT_FILENO), and it wants a FILE*.  Didn't know stdout worked on the same file.  Also got side-tracked on sync(), which supposed flushes all the buffers, but doesn't seem to apply to printf output.

I think this just about takes care of this one...  I have a few more reality checks to do to make sure things are working right.

Can you suggest a good reference for the C standard library that spells out some of the caveats?  The information I've found by googling has been sparse and incomplete.

Thanks!
0
 

Author Comment

by:AgentFriday
ID: 13721267
CONCLUSIONS:

  - printf() output to stdout is buffered, while write() is not.
  - Piping output to anoth app (such as with myprog | sort) increases the amount of buffering to more than one line at a time.
  - _exit() does not flush the printf output, but discards it.  Call fflush(stdout) before _exit() when ending a child process
  - If you do not call fflush(stdout) just before spawning the child (using fork()), then the child will get a copy of all the buffered output of the parent, causing a certain amount of duplicated output.

I would not have run into many of these problems if I had not been piping the output of the program through another one, since the printf output would have been flushed after each line.

0

Featured Post

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question