The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AwK printf question whatisthis UNIX for Dummies Questions & Answers 4 12-10-2007 10:17 AM
printf question hankooknara Shell Programming and Scripting 3 06-18-2007 10:28 PM
Question on forks and pipes Phantom12345 UNIX for Advanced & Expert Users 3 08-29-2006 05:59 AM
Question: pthread_cancel() and printf() prankster High Level Programming 4 10-13-2005 07:26 AM
forks....HELP!!! someone anyone? richardspence2 UNIX for Advanced & Expert Users 3 04-06-2004 12:36 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-07-2007
Registered User
 

Join Date: Jan 2007
Posts: 2
multiple forks and printf question

Hello *NIX gurus,

I have a slight perplexing problem with multiple forks giving different results... Here is the deal.

From what I undestand, a fork() call starts executing from the next instruction that follows the fork() call. That means it inherits the PC counter register value of the parent, right?

For example

----------
Code:
#include <stdio.h>
#include <unistd.h>

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

}
-----------------

produces Hello

Now if I remove the new line character from printf, as follows:
--------------
Code:
#include <stdio.h>
#include <unistd.h>

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

It produces: HelloHelloHelloHello


Why does this happen? I tried fptintf to stderr (unbuffered output) and it produces just one hello, so I am thinking it has to do with when the output is flushed? But not sure how...

Could someone please shed some light on this.

Last edited by blowtorch; 01-07-2007 at 04:34 PM. Reason: code tags
Reply With Quote
Forum Sponsor
  #2  
Old 01-07-2007
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
Hmm.... that seems to be right. It is the buffer flushing that is responsible for that. Try replacing the printf with a write(2) and you won't see this behaviour.

It is the exit(2) syscall that causes this (by flushing the buffers).

Here is a post from linuxforums.org that describes something similar to what you have here.

Last edited by blowtorch; 01-07-2007 at 04:56 PM.
Reply With Quote
  #3  
Old 01-07-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
Output to a tty is line buffered so the /n causes a flush. In this case, no data remains buffered and you get 4 copies of an empty buffer. Remove the /n and the data stays in the buffer. The buffer (and data) is then quadruplicated and all 4 processes flush their buffers at program exit.
Reply With Quote
  #4  
Old 01-07-2007
Registered User
 

Join Date: Jan 2007
Posts: 2
Hmmmm, Thanks you guys. i read the link and your explanaitons and it makes sense to me now. What I understand now is that the data sections get copied to child processes, which means any unflushed buffers will be copied. Then on exit, every process flushes the output, hence producing a four printouts of Hello.

Thanks blowtorch for modifying the post for code clarity, i'll keep that in mind for future posts...
Reply With Quote
  #5  
Old 01-10-2007
elzalem's Avatar
Registered User
 

Join Date: Nov 2006
Location: Lebanon
Posts: 33
flushing without \n

to flush the output buffer without using \n, after the printf use fflush(stdout) ;
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0