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
Fork and \n xyz123456 UNIX for Advanced & Expert Users 1 05-15-2008 10:19 AM
fork() problem ddx08 High Level Programming 3 03-01-2007 06:06 AM
rc=127 can't fork BG_JrAdmin Shell Programming and Scripting 1 08-01-2006 02:30 PM
fork() MKSRaja High Level Programming 2 02-07-2005 08:55 AM
fork() and IPC TelePlayer High Level Programming 1 05-30-2001 12:57 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-24-2006
Registered User
 

Join Date: Jan 2006
Posts: 4
fork problem

Hi,

Consider the following piece of code:

int main(void) {

int i;
pid_t pidp;

for (i=0;i<4;i++) {
switch (pidp=fork()) {
case -1:
fprintf(stdout, "Error during fork.\n");
exit (1);
case 0:
fprintf(stdout, "From child: I am born.\n");
exit (0);
default:
fprintf(stdout, "From parent: Child %d has ID %d.\n", i+1, pidp);
}
}

exit(0);
}

I understand that this program is not protected while it is writing to stdout. Therefore, certain inconsistencies are bound to happen. But I am not able to comprehend the following observation.

When I run this program, the output is on the standard output and is the following (as expected):

From child: I am born.
From parent: Child 1 has ID 6412.
From parent: Child 2 has ID 6413.
From child: I am born.
From child: I am born.
From parent: Child 3 has ID 6414.
From child: I am born.
From parent: Child 4 has ID 6415.

However, when the output is redirected to a file (even if "tee" is used), the output has a lot of duplicated lines:

From child: I am born.
From parent: Child 1 has ID 6454.
From child: I am born.
From parent: Child 1 has ID 6454.
From parent: Child 2 has ID 6455.
From parent: Child 3 has ID 6456.
From child: I am born.
From parent: Child 1 has ID 6454.
From parent: Child 2 has ID 6455.
From child: I am born.
From parent: Child 1 has ID 6454.
From parent: Child 2 has ID 6455.
From parent: Child 3 has ID 6456.
From parent: Child 4 has ID 6457.

I figure that it is due to the stdout stream becuase using fflush before the print statements in the code fixes this inconsistency.

But could somebody explain what's exactly happening?

regards,
Quantum Teleporter!
Reply With Quote
Forum Sponsor
  #2  
Old 01-24-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
When you write to a file, it is buffered. Each time you fork, the child inherits that buffer with the parents output still in it. The child adds a line to the inherited buffer and exits, which flushes the buffer. Also the children are not guaranteed to exit in the order they were created.
Reply With Quote
  #3  
Old 01-24-2006
Registered User
 

Join Date: Jan 2006
Posts: 4
Thanks a lot for your reply! I appreciate it very much!

I understand that the order in which the children finish is uncertain in this program.

However, why doesn't the same thing (duplication of lines) happen when the output of the program is not redirected to a file?
Reply With Quote
  #4  
Old 01-24-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
stdio looks at the output device (stdout). If it is a tty, output is line buffered.
Reply With Quote
  #5  
Old 01-25-2006
Registered User
 

Join Date: Jan 2002
Location: dusseldorf,germany
Posts: 10
try this peice of code

The buffer from parent will be flushed all before the child will be called.

int main(void) {

int i;
pid_t pidp;

for (i=0;i<4;i++) {
switch (pidp=fork()) {
case -1:
fprintf(stdout, "Error during fork.\n");
fflush(NULL);
exit (1);
case 0:
fprintf(stdout, "From child: I am born.\n");
fflush(NULL);
exit (0);
default:
fprintf(stdout, "From parent: Child %d has ID %d.\n", i+1, pidp);
fflush(NULL);
}
}

exit(0);
}

rajesh
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:37 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