![]() |
|
|
|
|
|||||||
| 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 |
| Fork and \n | xyz123456 | UNIX for Advanced & Expert Users | 1 | 05-15-2008 10:19 AM |
| fork() problem | ddx08 | High Level Programming | 3 | 03-01-2007 06:06 AM |
| rc=127 can't fork | BG_JrAdmin | Shell Programming and Scripting | 1 | 08-01-2006 02:30 PM |
| fork() | MKSRaja | High Level Programming | 2 | 02-07-2005 08:55 AM |
| fork() and IPC | TelePlayer | High Level Programming | 1 | 05-30-2001 12:57 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
fork problem
Hi,
Consider the following piece of code: int main(void) { int i; pid_t pidp; for (i=0;i<4;i++) { switch (pidp=fork()) { case -1: fprintf(stdout, "Error during fork.\n"); exit (1); case 0: fprintf(stdout, "From child: I am born.\n"); exit (0); default: fprintf(stdout, "From parent: Child %d has ID %d.\n", i+1, pidp); } } exit(0); } I understand that this program is not protected while it is writing to stdout. Therefore, certain inconsistencies are bound to happen. But I am not able to comprehend the following observation. When I run this program, the output is on the standard output and is the following (as expected): From child: I am born. From parent: Child 1 has ID 6412. From parent: Child 2 has ID 6413. From child: I am born. From child: I am born. From parent: Child 3 has ID 6414. From child: I am born. From parent: Child 4 has ID 6415. However, when the output is redirected to a file (even if "tee" is used), the output has a lot of duplicated lines: From child: I am born. From parent: Child 1 has ID 6454. From child: I am born. From parent: Child 1 has ID 6454. From parent: Child 2 has ID 6455. From parent: Child 3 has ID 6456. From child: I am born. From parent: Child 1 has ID 6454. From parent: Child 2 has ID 6455. From child: I am born. From parent: Child 1 has ID 6454. From parent: Child 2 has ID 6455. From parent: Child 3 has ID 6456. From parent: Child 4 has ID 6457. I figure that it is due to the stdout stream becuase using fflush before the print statements in the code fixes this inconsistency. But could somebody explain what's exactly happening? regards, Quantum Teleporter! |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
When you write to a file, it is buffered. Each time you fork, the child inherits that buffer with the parents output still in it. The child adds a line to the inherited buffer and exits, which flushes the buffer. Also the children are not guaranteed to exit in the order they were created.
|
|
#3
|
|||
|
|||
|
Thanks a lot for your reply! I appreciate it very much!
I understand that the order in which the children finish is uncertain in this program. However, why doesn't the same thing (duplication of lines) happen when the output of the program is not redirected to a file? |
|
#4
|
||||
|
||||
|
stdio looks at the output device (stdout). If it is a tty, output is line buffered.
|
|
#5
|
|||
|
|||
|
try this peice of code
The buffer from parent will be flushed all before the child will be called. int main(void) { int i; pid_t pidp; for (i=0;i<4;i++) { switch (pidp=fork()) { case -1: fprintf(stdout, "Error during fork.\n"); fflush(NULL); exit (1); case 0: fprintf(stdout, "From child: I am born.\n"); fflush(NULL); exit (0); default: fprintf(stdout, "From parent: Child %d has ID %d.\n", i+1, pidp); fflush(NULL); } } exit(0); } rajesh |
|||
| Google The UNIX and Linux Forums |