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
Redirect Output Multi-Process djodjo High Level Programming 3 03-17-2008 02:55 PM
redirect only the standard error output to mail barkath Shell Programming and Scripting 3 02-08-2008 05:41 PM
How to redirect std err and out to log file for multi-commands? siegfried Shell Programming and Scripting 1 08-10-2007 05:43 PM
Question from a newbie. How to redirect standard output ndemos UNIX for Dummies Questions & Answers 1 07-27-2007 07:28 AM
redirect standard error into log file epall UNIX for Dummies Questions & Answers 7 05-09-2006 03:29 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-04-2008
Registered User
 

Join Date: Mar 2008
Posts: 3
Redirect Standard Output Multi-Process

Hi,

I'm trying to compile the following code:

/************** Begin <test.c> ***************/

/*
* Compiled with: gcc -Wall -o test test.c
*/

#include <stdio.h>
#include <unistd.h>

int main(void)
{
printf("I'm process %d, son of %d \n", getpid(), getppid());
printf("Hello \n");
fork();
printf("Everyone \n");
printf("I'm process %d, son of %d \n", getpid(), getppid());
return(0);
}

/************** End <test.c> ***************/


I compile it like:
$ gcc -Wall -o test test.c

Then, if i test it leaving the output to standard output i have:
$ ./test
I'm process 1568, son of 1541
Hello
Everyone
I'm process 1568, son of 1541
Everyone
I'm process 1569, son of 1

If i redirect the output to a file i have:
$ ./test > file.txt
$ cat file.txt
I'm process 1571, son of 1541
Hello
Everyone
I'm process 1572, son of 1571
I'm process 1571, son of 1541
Hello
Everyone
I'm process 1571, son of 1541


My question is if it should happen like this.
How can i redirect the output to a file and have the same as if i
leave it to standard output.
Why is the son process executing the print before the fork? Is it buffered text?
Would like your help on this subject.

Best regards,
João José
Reply With Quote
Forum Sponsor
  #2  
Old 09-28-2008
Registered User
 

Join Date: Sep 2008
Posts: 2
RE!!Redirect Standard Output Multi-Process

Hi!!

>> printf("I'm process %d, son of %d \n", getpid(), getppid());
>> printf("Hello \n");

This statements are buffered while parent execute them and redirected to a file. As you preformed fork () and redirect it to a file, this line buffers are also copied for child process.
Thus u get "Hello " for child process also.
Reply With Quote
  #3  
Old 09-28-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
There is no way to predict in which order the output of two processes will be intermixed, it will vary from one invocation to the next. If you get the same result ten times in a row, that's pure chance. (Still happens.)
Reply With Quote
  #4  
Old 09-29-2008
Registered User
 

Join Date: May 2008
Posts: 10
try to put fflush(stdout) after printf

#include <stdio.h>
#include <unistd.h>

int main(void)
{
printf("I'm process %d, son of %d \n", getpid(), getppid());
printf("Hello \n");
fflush(stdout);
fork();
printf("Everyone \n");
printf("I'm process %d, son of %d \n", getpid(), getppid());
fflush(stdout);
return(0);
}


then the result will be ok
Reply With Quote
  #5  
Old 09-30-2008
Registered User
 

Join Date: Sep 2008
Posts: 2
I think fflush() will not work for here. As standard output uses line buffer, printf() is automatically flushed using "\n"
Reply With Quote
  #6  
Old 10-01-2008
Registered User
 

Join Date: Sep 2008
Location: Germany
Posts: 24
Seems to me that this is exactly what changes, when stdout is redirected to disk. But an fflush should always flush buffers, so a printf done before forking should not be written twice anymore.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:28 PM.


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