LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-29-2009, 12:08 PM   #1
anshu33331
LQ Newbie
 
Registered: Jul 2009
Posts: 1
Blog Entries: 1

Rep: Reputation: 0
fork() not giving desired result


main()
{
printf("LINUX");
fork();
printf("UNIX");
}

expected result is LINUX UNIX UNIX.
actual result: LINUX UNIX LINUX UNIX

But if i write
printf("LINUX\n"); in line 3

it is giving expected output.

can anybody give the justified answer..
 
Old 07-29-2009, 12:56 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
It's called "buffering".

"Printf()" doesn't necessarily write to the screen as soon as you call it; the I/O was "buffered" until later in the program (as it happens, until *after* you called "fork()" in the first program).
 
Old 07-29-2009, 01:08 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by anshu33331 View Post
main()
{
printf("LINUX");
fork();
printf("UNIX");
}

expected result is LINUX UNIX UNIX.
actual result: LINUX UNIX LINUX UNIX

But if i write
printf("LINUX\n"); in line 3

it is giving expected output.

can anybody give the justified answer..
It's caused by buffering. Lines are only written to the terminal when a newline is encountered or when the process exits. The parent writes LINUX to buffer, forks (giving the child a copy of its unflushed buffer containing LINUX), writes UNIX to buffer and then exits, flushing the buffer containing LINUX UNIX to terminal. The child writes UNIX to the buffer already containing LINUX then exits, flushing the buffer containing LINUX UNIX to terminal. See http://www.linuxforums.org/forum/147741-post6.html for a fuller explanation including how to avoid the "problem".

Best

Charles

Last edited by catkin; 07-29-2009 at 01:09 PM.
 
Old 07-29-2009, 01:37 PM   #4
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
using

Code:
fprintf(stderr, "LINUX");
instead of

Code:
printf("LINUX");
should give you the expected results, as printing to stderr is by default unbuffered.
 
Old 07-29-2009, 01:48 PM   #5
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by anshu33331 View Post
can anybody give the justified answer..
This has been answered to death, but here's some sample code:
Code:
[user@machine:~]:cat blah.c
#include <stdio.h>

int main(void)
{
    fprintf(stderr, "LINUX\n");
    fork();
    fprintf(stderr, "UNIX\n");
}

[user@machine:~]:./blah
LINUX
UNIX
UNIX
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
am setting radius+ldap and its giving me this result jerykwajaffa Linux - Networking 1 09-08-2008 09:17 AM
Unable to get desired Resolution. FooB SUSE / openSUSE 1 11-03-2006 06:43 PM
Desired delay in potfix hotrodowner Linux - Software 2 03-11-2005 05:37 AM
"who" not giving the expected result rblampain Linux - Newbie 2 11-17-2004 04:06 AM
"ifconfig -a" giving odd result balajipriya Linux - Networking 0 08-11-2004 02:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration