Redirect Output Multi-Process


 
Thread Tools Search this Thread
Top Forums Programming Redirect Output Multi-Process
# 1  
Old 03-05-2008
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é
# 2  
Old 03-07-2008
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.
# 3  
Old 03-10-2008
Thnks Smilie

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

Thnks again
# 4  
Old 03-17-2008
Right, fflush().
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use the get and post method in the bash ( multi process)?

hi I want to call a lot of links with the post method What to do to speed it up?? ####This method is slow #!/bin/bash func2() { index1=0 while read line ; do index1=$(($index1+1)) url=$line done < tmp/url1.txt } (10 Replies)
Discussion started by: mnnn
10 Replies

2. 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

3. Programming

Multi process programming in C

So I am trying to learn C and am coding some scripts on my own. For a start I have decided to port the shell script developed by wisecracker into C.( Here is the link to that script A simple reminder script for beginners to shell scripting. | Unix Linux Forums | OS X (Apple) ) This is what I... (7 Replies)
Discussion started by: chacko193
7 Replies

4. Shell Programming and Scripting

Redirect script output to a file and mail the output

Hi Guys, I want to redirect the output of 3 scripts to a file and then mail the output of those three scripts. I used below but it is not working: OFILE=/home/home1/report1 echo "report1 details" > $OFILE =/home/home1/1.sh > $OFILE echo... (7 Replies)
Discussion started by: Vivekit82
7 Replies

5. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

6. High Performance Computing

What is it about OpenMosix that supports multi-process applications?

I read that 'Any single program that can run as multiple processes can benefit from OpenMosix: "The GIMP" photo editor and the "kandel" fractal generator are known to do this. Are there other load-balancing clusters that do support multi-process applications? (1 Reply)
Discussion started by: Advice Pro
1 Replies

7. Programming

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

8. 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

9. Programming

message queues and multi-process

Hi, Am supposed to use message queues to send and receive messages between the processes. when i was working on that i realised that the message qid and the message queue related data should be maintained in a shared memory so that it can be accessed by all the processes. Could anybody refer... (10 Replies)
Discussion started by: rvan
10 Replies

10. Shell Programming and Scripting

How to redirect std err and out to log file for multi-commands?

The following command does not work under cygwin bash. ant debug >log 2>&1 && ant image >>log 2>>&1 & pid=$! It gives the error "-bash: sysntax error near unexpected token '&'". Is there a way I can redirect std output and std error to file "log" for both the commands "ant debug" and "ant... (1 Reply)
Discussion started by: siegfried
1 Replies
Login or Register to Ask a Question