-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
sumit1680@rediffmail.com wrote:[color=blue]
> Hi everyone,
> I am using the below listed code
>
> The code is
>
> #include<stdio.h>
>
> #include<stdlib.h>
>
> #include<string.h>
>
> int main()
>
> {
>
> char *obuf=NULL;
>
> FILE *stream=NULL;
>
> int i=0;
>
> stream=fopen("top","wb");
>
> obuf= (char*)malloc(8192);
>
> while(i<50)
>
> {
>
> memset(obuf,'a',8192);
>
> fwrite(obuf,sizeof(char),8192,stream);
>
> printf("single\n");
>
> i++;
>
> }
>
> fclose(stream);
>
> return 0;
>
> }
>
>
>
>
>
> And my strace is
>
> ................
>
> stat64(3, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
>
> mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
> 0) = 0xf7055000
>
> write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 8192) = 8192
>
> fstat64(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(136, 221), ...}) = 0
>
> mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
> 0) = 0xf7054000
>
> write(1, "single\n", 7) = 7
>
> write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096
>
> write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096
>
> write(1, "single\n", 7) = 7
>
> write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096
>
> write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096
>
> write(1, "single\n", 7) = 7
>
> write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096
>
> write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096
>
> write(1, "single\n", 7) = 7
>
> write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096
>
> write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096
>
> write(1, "single\n", 7) = 7
>
> write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096
>
> write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096
>
> write(1, "single\n", 7) = 7
>
> .............................
>
> As you can see the first write call is of 8192 bytes but later onthe
> subsequent fwrite calls breaks in two calls of write of size 4096 and
> 4096 bytes. Can anyone explain me the reason behind this behaviour of
> fwrite function???[/color]
Looks to me like you are seeing the platform and implementation-specific
behaviour of the implementation of the C standard library.
Consider this: how big is the buffer that C stream I/O will use to fwrite() to
a file fopen()ed as "wb"? Is it possible that the underlying stream I/O
implementation in the C runtime could make changes to the buffer size when
switching from fwrite() to printf()? Is it possible that the underlying stream
I/O implementation in the C runtime does not make changes to the buffer size
when switching from printf() to fwrite()?
Here's what I see from your strace:
You start off with a 8k buffer filled with the data from the first fwrite()
Immediately after the 1st fwrite(), something allocates a 4K buffer
Next, 7 bytes of data are written from the 1st printf()
Following that, from then on, the fwrite() output is written in 2 chunks of 4k
apiece.
Looks to me like the stdio library changed the buffersize from 8k to 4k
immediately before performing the printf().
- --
Lew Pitcher
Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (
http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)
iD8DBQFDwGUEagVFX4UWr64RAulCAJ9CyIpTUBC1CtERP443k3 wcvVYplQCgsdaC
0ewVL644vudonhg+j6h/4M4=
=CHLb
-----END PGP SIGNATURE-----