bug in glibc?

krishnaakishore at gmail.com krishnaakishore at gmail.com
Fri Jan 18 12:26:13 UTC 2008


Hi all,

I have a query regarding the output of the following code:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
main(){
	int a=0;
	while(1){
		printf("XXX(%d)",++a);
		sleep(1);
		if(fork()==0){printf("\n");exit(0);}
		//you can remove the printf here,
		//its just to enhance the readability of output

 	}
}

The output should actually be:
XXX(1)
XXX(2)
XXX(3)
XXX(4)
XXX(5)
XXX(6) (and so on)

But the output actually is:
XXX(1)
XXX(1)XXX(2)
XXX(1)XXX(2)XXX(3)
XXX(1)XXX(2)XXX(3)XXX(4)
XXX(1)XXX(2)XXX(3)XXX(4)XXX(5)
XXX(1)XXX(2)XXX(3)XXX(4)XXX(5)XXX(6) (and so on)

The fork() call is triggering the flushing the buffers but not
clearing them. Hence the data in the buffers is getting accumulated.
Appending a '\n' to "XXX(%d)" will generate the expected output.

Is this a bug in glibc? I wanted to file a bug at glibc site but i was
asked to confirm it in my distribution mailing list (as they may have
customized the libraries)

For information -

my 'uname -a': Linux kishore 2.6.23.1-21.fc7 #1 SMP Thu Nov 1 21:09:24
EDT 2007 i686 i686 i386 GNU/Linux

'ldd a.out' (a.out is executable of above code):
linux-gate.so.1 => (0×00110000)
libc.so.6 => /lib/libc.so.6 (0×00111000)
/lib/ld-linux.so.2 (0×00319000)

'rpm -q glibc': glibc-2.6-3

Regards,
KK.




More information about the redhat-list mailing list