fwrite problem

fwrite problem

Post by Marc » Sun, 13 Apr 2003 00:05:38



Hi

I've a strange behavior using fwrite.
My program writes 5 MB on the disk with buffer of 50000 bytes, but
sometime a single fwrite waits for more than 1 seconds.

System features :
- reiserfs
- DMA actived.

This is the code I used to test fwrite

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

const __int64 TIME_T_ZERO_TIME  = 116444700000000000;  

longlong UTime64()
{
  struct timeval tv;
  struct timezone tz;
  gettimeofday(&tv,&tz);

  long long Ret = (__int64)tv.tv_sec*10000000+TIME_T_ZERO_TIME+(__int64)tv.tv_usec*10
+ (__int64)36000000000;

Quote:}

int main(int argc, char** argv)
{
  char buffer[100000];
  int size;
  __int64 tTime1;

  int Count = 0;
  char Name[2014];
  while (1)
  {
    sprintf(Name,"/tmp/prova_fwrite_%d.tmp",Count++);
    FILE* f = fopen(Name,"w");

    int Size = 50000;
    int SizeWritten = 0;

    while (SizeWritten < 5000000)
    {
      tTime1 = UTime64();
      size = fwrite(buffer,1,Size,f);
      tTime2 = UTime64();

      float fl = (float)(tTime2-tTime1) / size;
      if (fl>50 && tTime2-tTime1 > 10000000) // > 1 sec
      {
        printf("Ratio = %f - %d - %llu\n",fl,size,tTime2-tTime1);
      }

      SizeWritten[i] += size;
    }

    fclose(f);

    usleep(1000);
  }

  return 1;

Quote:}

And this is the output
(idx)    (delta T / size)     (size) (delta T sec)

15730 - Ratio2 = 305.085602 - 50000 - 1,5254330
19665 - Ratio2 = 559.221008 - 50000 - 2,7961100
23154 - Ratio2 = 490.265991 - 50000 - 2,4513350
24989 - Ratio2 = 430.956207 - 50000 - 2,1547860
27135 - Ratio2 = 283.720795 - 50000 - 1,4186090
31161 - Ratio2 = 238.803802 - 50000 - 1,1940240
42781 - Ratio2 = 263.786591 - 50000 - 1,3189380
....

 
 
 

1. Problem fread/fwriting sockets

I'm doing a lot of client-server stuff here, using both Inet and UNIX
sockets.  For portability reasons, I'm fdopen()ing connected sockets,
and then using fread() and fwrite() to do the i/o.  I'm having a
problem where a client writes 512 bytes to the server, and, rather than
receiving that same data back (which is what should happen), it
receives it with the last byte it sent prepended.  That is, it's like
the last byte doesn't quite make it out the other end (although the
server reads it just fine), and is pushed back to the client in front
of the data being written by the server.  Everything looks like it's
working fine on the server end.  All these socket connections have a
setvbuf(FILE *,(char *) NULL,_IONBF,(size_t) 0) call immediately after
the fdopen(), so the i/o should be completely unbuffered.  The
fread/fwrite's are of the form fread(void *, 1, 512, FILE *).  I've
found a workaround by calling fseek(FILE *, 0, SEEK_CUR) after each
fread and fwrite, but this is obviously a hack masking a problem which
I don't understand.  Any suggestions as to what the problem might be
would be greatly appreciated.  I love a mystery, but this one's gone on
too long, and I'm out of suspects.
--
Jim Tomlinson                         (206)865-6578  \  "falling snow

Boeing Computer Services   ...uunet!bcstec!voodoo!jdt  \  - Anderson/Gabriel

2. UNP problem about FD_SET using select fuction ??

3. Problem with fwrite()

4. expand disk space

5. problem in C using fwrite

6. multiple readers, one writer on shared memory

7. weird problem with (fseek/ftell/fwrite/putc)

8. help with dvips/slackware 96 please

9. Problem: fwrite and fseek.

10. How do you use fread and fwrite in c with unix

11. fwrite error 32 - what to do ?

12. How to fwrite/write file exceed 2gig on solaris 2.6?

13. fwrite doesn't seem to work right