multiple forks and printf question


 
Thread Tools Search this Thread
Top Forums Programming multiple forks and printf question
# 1  
Old 01-07-2007
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 07:34 PM.. Reason: code tags
# 2  
Old 01-07-2007
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 07:56 PM..
# 3  
Old 01-07-2007
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  
Old 01-07-2007
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  
Old 01-10-2007
flushing without \n

to flush the output buffer without using \n, after the printf use fflush(stdout) ;
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shells, forks, subprocesses... oh my

all, i've been reading to try and get an abstract idea of the process effeciency of commands , sed, bash, perl, awk, find, grep, etc which processes will spawn?, fork?, launch subshell?, etc and under what conditions? how do you know which commands have the faster and better stdio... (2 Replies)
Discussion started by: f77hack
2 Replies

2. Shell Programming and Scripting

Printf question: getting padded zero in decimal plus floating point together.

Hi Experts, Quick question: I am trying to get the output with decimal and floating point but not working: echo "20.03" | awk '{printf "%03d.2f\n" , $0 }' 020.2f How to get the output as : 020.03 Thank you. (4 Replies)
Discussion started by: rveri
4 Replies

3. Shell Programming and Scripting

AWK printf command question

Hi, I am using below awk code to convert a csv file data into fixed file format. awk 'BEGIN { FS = "," fmt = "%10s%010d%10s%d%1d\n" } NR>1 { printf fmt, $1, $2, $3, $4*100, $5 }' /data/mydata.csv > /data/fixed.dat Data in mydata.csv ================... (2 Replies)
Discussion started by: kbmkris
2 Replies

4. UNIX for Dummies Questions & Answers

AWK printf command question

Hi, I am using below awk code to convert a csv file data into fixed file format. awk 'BEGIN { FS = "," fmt = "%10s%010d%10s%d%1d\n" } NR>1 { printf fmt, $1, $2, $3, $4*100, $5 }' /data/mydata.csv > /data/fixed.dat Data in mydata.csv ================... (1 Reply)
Discussion started by: kbmkris
1 Replies

5. UNIX for Dummies Questions & Answers

AwK printf question

Hi, Does anyone know a easy way to printf $3,$4, ... all the way to the last field in the file? I will need to modify $1 and $2 and then printf modified $1 and $2 and the rest of the fields(which are not changed). I know I can use NF as the total number of field. Do I use a for next statement to... (4 Replies)
Discussion started by: whatisthis
4 Replies

6. Shell Programming and Scripting

printf question

can you take input from another command and do printf? such as awk '{print $2,$1}' | sort -k1,1 -k2,2 | printf "%-10s,%15s" this does not work.. but there must be a way.. please help me.. thank you. (3 Replies)
Discussion started by: hankooknara
3 Replies

7. Programming

read from file using forks

i'm tring to make 2 processes each read from the same file but only one of them read the file. FILE * fileptr1; fileptr1 = fopen("file1.txt","rt"); pid2=fork(); while(1) { fscanf(fileptr1,"%s",temp1); if(feof(fileptr1)==0) { printf("%i",getpid()); //id of current process ... (6 Replies)
Discussion started by: ddx08
6 Replies

8. UNIX for Advanced & Expert Users

Question on forks and pipes

I am trying to figure out why when i have the following code int main( { printf("0\n"); fork(); printf("1\n"); exit(0);} and type in the shell a.out | cat the output of this program is 0 1 0 1 instead of 0 1 1 does anyone know? (3 Replies)
Discussion started by: Phantom12345
3 Replies

9. Programming

Question: pthread_cancel() and printf()

Hello! First of all, sorry for my English, I'm not a native English speaker. I know, that printf() function uses write() function. "man cancellation" says that write() function is a cancellation point. But when I call pthread_cancel() for my thread, which calls printf() in infinite cycle, it... (4 Replies)
Discussion started by: prankster
4 Replies

10. UNIX for Advanced & Expert Users

forks....HELP!!! someone anyone?

Hey guys, I'm given this bit of code, but, I'm having some problems executing it with the functions I've defined so far. I'm suppose to define the funtions "parse" and "execute." Parse splits the command in buf into individual arguments. It strips whitespace, replacing those it finds with NULLS... (3 Replies)
Discussion started by: richardspence2
3 Replies
Login or Register to Ask a Question