LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 02-12-2003, 12:33 PM   #1
Uzair
LQ Newbie
 
Registered: Feb 2003
Posts: 1

Rep: Reputation: 0
Question Strange behaviour of fork call in Linux


Hi all,
I cannot understand why there is a difference in output of the following 2 programs, can any one help?

int main(void)
{
fork();
printf("Hello\n");
fork();
}

The above program prints Hello two times.

int main(void)
{
fork();
printf("Hello");
fork();
}

The above program (i.e. without "\n" in the printf) prints Hello 4 times....
Please help me in understanding this difference , I shall be grateful.

Thank you,
Uzair
 
Old 02-12-2003, 01:37 PM   #2
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
It's something to do with the way stdio.h works (it does some quite complicated buffering and stuff behind the scenes, which would be sensitive to newlines, which might cause a buffer to be flushed or something).

If you replace the call to printf with one to write, like so:
Code:
write(1, "Hello\n", 6);
/* OR */
write(1, "Hello", 5);
Both versions output Hello twice.

Alex
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Strange behaviour Anmol Suse/Novell 2 10-27-2005 11:05 PM
shutdown(2) after fork(2) behaviour changed or undefined? MScheffer Linux - Networking 3 07-15-2005 09:11 AM
Changing the fork system call seshuch Programming 0 10-02-2004 05:18 AM
Wierd behaviour using fork() and pipe() cybermix Programming 4 01-08-2004 03:44 AM
Strange Behaviour mikeyt_3333 Linux - General 4 08-06-2001 03:07 PM


All times are GMT -5. The time now is 09:14 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration