Redirect Standard Output Multi-Process


 
Thread Tools Search this Thread
Top Forums Programming Redirect Standard Output Multi-Process
# 1  
Old 03-04-2008
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é
# 2  
Old 09-29-2008
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.
# 3  
Old 09-29-2008
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.)
# 4  
Old 09-29-2008
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);
}

Smilie
then the result will be ok Smilie
# 5  
Old 10-01-2008
I think fflush() will not work for here. As standard output uses line buffer, printf() is automatically flushed using "\n"
# 6  
Old 10-01-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Soft kill a process to redirect the last kbytes output to a file

Hey guys, I have a python script that I call with this line: python mypythonscript.py >> results.csv &The problem is that the redirection from the stdout to the file results.csv only writes 4096 kbyte blocks. So if i kill this process with kill the last kbytes that the script produce will... (6 Replies)
Discussion started by: Mastaer
6 Replies

2. Shell Programming and Scripting

How redirect standard output to a file

Hi guys, i have a script named purgeErrors.ksh, when i execute this script i need to redirect the output to a log file in the same directory, how can i do that ?? -- Aditya (5 Replies)
Discussion started by: chaditya
5 Replies

3. UNIX for Dummies Questions & Answers

Redirect Standard output and standard error into spreadsheet

Hey, I'm completely new at this and I was wondering if there is a way that I would be able to redirect the log files in a directories standard output and standard error into and excel spreadsheet in anyway? Please remember don't use too advanced of terminology as I just started using shell... (6 Replies)
Discussion started by: killaram
6 Replies

4. Shell Programming and Scripting

Redirect standard error to input of other process, 2| ?

Hello, I would like to know if there is a shell in which operations such as 2| (redirect standard error of one process to the standard input of another one) exist? I know it is possible to do it in bash with things like: (process 2>&1) | other_process but I find it a bit intricate when... (3 Replies)
Discussion started by: chlorine
3 Replies

5. Shell Programming and Scripting

Redirect bg process output to within the script

Hi, I have a process running in the background, which throws up some output to the terminal when I run my script. How can I read this output from my script? Thank you. (5 Replies)
Discussion started by: Theju
5 Replies

6. Programming

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");... (3 Replies)
Discussion started by: djodjo
3 Replies

7. Shell Programming and Scripting

[BASH] redirect standard error and use it inside

Hi all, Maybe my question is too simple but till now i couldn't figure about a solution :( I have a bash script scheduled in cron: <cron time parameters> my_script.sh > result.log 2>&1 By this way i can have standard output and standard error in my result.log file Now i want my script... (2 Replies)
Discussion started by: Pescator
2 Replies

8. Shell Programming and Scripting

redirect only the standard error output to mail

I'm writing a script using file descriptor 2 (std error) to send an email only if the command fails or errors out but the script always emails me irrepective of whether it fails or not. It will not email the /tmp/check.error file output if doesn't error out just the mail with the subject "Cannot... (3 Replies)
Discussion started by: barkath
3 Replies

9. UNIX for Dummies Questions & Answers

Question from a newbie. How to redirect standard output

I have a program that is sending error text to the console and I need to redirect that output to a log file. I'm brand new to Unix and don't know how to do this. Any direction would be greatly appreciated. (1 Reply)
Discussion started by: ndemos
1 Replies

10. UNIX for Dummies Questions & Answers

redirect standard error into log file

Hi, I am new in shell scripting. Can anyone point out what wrong of below script. If I want the error output to "sqlerror.log" and database pool data output to "bulk_main.dat". Right now, the below script, if successful execute, the data will output to bulk_main.dat && sqlerror.log both... (7 Replies)
Discussion started by: epall
7 Replies
Login or Register to Ask a Question