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 Standard Output Multi-Process djodjo High Level Programming 5 10-01-2008 02:09 AM
message queues and multi-process rvan High Level Programming 10 09-04-2007 11:06 PM
Redirect output mpang_ Shell Programming and Scripting 1 08-15-2007 05:57 AM
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
redirect output of FTP kcaluwae Shell Programming and Scripting 4 06-23-2005 05:45 AM

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

Join Date: Mar 2008
Posts: 3
Redirect 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 03-07-2008
Registered User
 

Join Date: Feb 2005
Posts: 53
Hi,

You are right, this is due to buffering.
Add flush(stdout) before fork() to see the same results.

Also consider parent PID is 1.
Add sleep(1) before 'return' to see the true PPID.

Regards.
Reply With Quote
  #3  
Old 03-10-2008
Registered User
 

Join Date: Mar 2008
Posts: 3
Thnks

flush() doesn't seem to work, can't find that function.
But fflush() works! Did u ment fflush?

Thnks again
Reply With Quote
  #4  
Old 03-17-2008
Registered User
 

Join Date: Feb 2005
Posts: 53
Right, fflush().
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:26 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