fwrite problems...

S

sumit1680

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???
 
S

Spacen Jasset

(e-mail address removed) wrote:

....
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???


The standard C library is allowed to do this in a manner of it's
choosing. If you want to try and change the buffering arrangements it
makes, look at the setvbuf function, but know that still; more than one
write may be made to the underling OS functions.

It is a good idea to use a reasonable sized buffer with fwrite, since
you want to reduce the number of calls to it to save time. Using fwrite(
buf, 1, 1, stream ) will usually take a long time to write a large file
because of the call overhead.
 
M

Mark McIntyre

On 6 Jan 2006 03:47:01 -0800, in comp.lang.c ,
Hi everyone,
I am using the below listed code
(snip code which tries repeatedly to write 8192 bytes to a file)
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???

The library is allowed to perform buffered IO any way it likes, as
long as by the time the next statement is executed, the IO is complete
from your programme's point of view. This allows it to efficiently use
hardware-specific writing routines. Note that it might not even have
written anything to disk at this stage - your disk controller might
have cached all 50 calls to fwrite, and do the whole lot later.


Mark McIntyre
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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???

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)

iD8DBQFDwGUEagVFX4UWr64RAulCAJ9CyIpTUBC1CtERP443k3wcvVYplQCgsdaC
0ewVL644vudonhg+j6h/4M4=
=CHLb
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top