![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| AwK printf question | whatisthis | UNIX for Dummies Questions & Answers | 4 | 12-10-2007 10:17 AM |
| printf question | hankooknara | Shell Programming and Scripting | 3 | 06-18-2007 10:28 PM |
| Question on forks and pipes | Phantom12345 | UNIX for Advanced & Expert Users | 3 | 08-29-2006 05:59 AM |
| Question: pthread_cancel() and printf() | prankster | High Level Programming | 4 | 10-13-2005 07:26 AM |
| forks....HELP!!! someone anyone? | richardspence2 | UNIX for Advanced & Expert Users | 3 | 04-06-2004 12:36 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
multiple forks and printf question
Hello *NIX gurus,
I have a slight perplexing problem with multiple forks giving different results... Here is the deal. From what I undestand, a fork() call starts executing from the next instruction that follows the fork() call. That means it inherits the PC counter register value of the parent, right? For example ---------- Code:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("Hello\n");
fork();
fork();
return 0;
}
produces Hello Now if I remove the new line character from printf, as follows: -------------- Code:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("Hello");
fork();
fork();
return 0;
}
It produces: HelloHelloHelloHello Why does this happen? I tried fptintf to stderr (unbuffered output) and it produces just one hello, so I am thinking it has to do with when the output is flushed? But not sure how... Could someone please shed some light on this. Last edited by blowtorch; 01-07-2007 at 04:34 PM. Reason: code tags |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Hmm.... that seems to be right. It is the buffer flushing that is responsible for that. Try replacing the printf with a write(2) and you won't see this behaviour.
It is the exit(2) syscall that causes this (by flushing the buffers). Here is a post from linuxforums.org that describes something similar to what you have here. Last edited by blowtorch; 01-07-2007 at 04:56 PM. |
|
#3
|
||||
|
||||
|
Output to a tty is line buffered so the /n causes a flush. In this case, no data remains buffered and you get 4 copies of an empty buffer. Remove the /n and the data stays in the buffer. The buffer (and data) is then quadruplicated and all 4 processes flush their buffers at program exit.
|
|
#4
|
|||
|
|||
|
Hmmmm, Thanks you guys. i read the link and your explanaitons and it makes sense to me now. What I understand now is that the data sections get copied to child processes, which means any unflushed buffers will be copied. Then on exit, every process flushes the output, hence producing a four printouts of Hello.
Thanks blowtorch for modifying the post for code clarity, i'll keep that in mind for future posts... |
|
#5
|
||||
|
||||
|
flushing without \n
to flush the output buffer without using \n, after the printf use fflush(stdout) ;
|
||||
| Google The UNIX and Linux Forums |