S
sumit1680
Hi everyone,
I am using the below listed code
#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 on the
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???
I am using the below listed code
#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 on the
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???