APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Home > News Posts > fork and stdio buffering
Printer Friendly Version




News Group Posts

fork and stdio buffering



From: Lew.Pitcher@td.com (Lew Pitcher)
Subject: Re: fork
References: <60147538.0211280514.2e1544f2@posting.google.com> 
Date: Thu, 28 Nov 2002 13:51:23 GMT

On 28 Nov 2002 05:14:22 -0800, rijomundassery@yahoo.com (Rijo Mundassery) wrote:

>when i gave 
>printf("something");
>fork();
>
>it printed something twice
>
>when i changed it into printf("something\n");
>it printed once , why this and what fork is doing by seeing a \n
>
>please help me!



It's a byproduct of how stdio buffering works

Your printf() statement is buffered into a block of memory, and will only be
written to the character special output device when the program either issues a
printf("\n") or a fflush(stdout). (If your stdout is directed to something other
than a character special output device, the buffer gets flushed with each
printf() statement.)

So, when you start a program with stdout directed to your terminal, you can only
expect to see printf() output when the printf() prints a newline, or the program
issues an explicit fflush(). At all other times, this program will buffer the
data in memory, waiting for one of those two conditions to occur.

Now, your program performs it's
  printf("something");
and nothing comes out

Next, your program performs it's
  fork();
and still nothing comes out. However, you now have two programs running, each
with the "something" stashed in a stdout buffer (remember, in essence, fork()
copies the current state of the forking program, so that both the original and
the new processes have the same values in all their variables, including
buffers).

When the first program terminates, the stdio library flushes that program's
stdout buffer prior to the exit, so you see the first "something", coming from
the buffer in the first program.














Now, the second program terminates, and the stdio library flushes _that_
program's stdout buffer prior to the exit. You now see the second "something",
coming from the buffer in the second program.

And, all this changes when you run your program with stdout redirected to a
file, because, since stdout is pointing to a file, the stdio library ensures
that the printf() before the fork() is flushed immediately, rather than being
deferred.

Lew Pitcher
IT Consultant, Development Services
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
 

If this page was useful to you, please click to help others find it:  

Your +1's can help friends, contacts, and others on the web find the best stuff when they search.

1 comment



Click here to add your comments





Thu Jul 7 04:57:11 2005:   anonymous


superb article for beginners


Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar


cartoon
Versatile Site Map Generator $59.00
A1 Sitemap Generator

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide


 I sell and support
 Kerio Mail server
g_face.jpg

This post tagged:

       - Programming




Unix/Linux Consultants

Skills Tests

Guest Post Here