背景:
要录像,数据从网络上接收,写数据帧,每帧100字节-140K不等,每秒约有20帧;
一台机器要同时录1-64个这样的文件。
问题:
现在有两个方案,
1 接收1帧写入磁盘一帧,这样案平均算,每秒要访问磁盘32*20次
2 接收数据帧后,先放在内存,等达到一定的长度(如1M)再写入磁盘,这样每秒访问磁盘的次数可能不到1,但每次写入的时间较长大家评价一下这两种方法,或者有没有其它可先方案.

解决方案 »

  1.   

    用IOCP写磁盘,是最好的办法.
    你不再需要考虑顶楼的问题.
      

  2.   

    1.对于小数据分片,最好在内存当中进行缓存,一来可以减少磁盘IO次数,提升效率,二来也可以减少频繁的磁盘IO延长磁盘的寿命
    2.可以考虑使用Overlapped模式异步写入。相对效率会更高一点。
      

  3.   

    谢谢各位支持!
    IOCP和异步写文件我还没有用过呢,只用过WriteFile、fwrite写过简单的文件,
    能不能更多点提示啊
      

  4.   

    写示例是需要时间的,并且到时候还得写注释,还得讲解...麻烦,自己看MSDN,然后下载Microsoft Platform SDK看里面的Demo.
    网上包括SDK示例,基本上以操作Socket的示例为主,写文件是一样的。不同的只是一个用SOCKET,另一个使用文件HANDLE
      

  5.   

        if (opened) { 
            DWORD written_bytes;        OVERLAPPED Overlapped;
            Overlapped.Offset = nat8_low_part(pos);
            Overlapped.OffsetHigh = nat8_high_part(pos);
            Overlapped.hEvent = NULL;
            return WriteFile(fd, buf, size, &written_bytes, &Overlapped)
                    ? size == written_bytes ? ok : end_of_file
                    : get_system_error();     } else { 
            return not_opened;
        }
      

  6.   

    http://www.codeproject.com/KB/files/fileex.aspx
      

  7.   

    If hFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the write operation starts at the offset specified in the OVERLAPPED structure and WriteFile may return before the write operation has been completed. In this case, WriteFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING. This allows the calling process to continue processing while the write operation is being completed. The event specified in the OVERLAPPED structure is set to the signaled state upon completion of the write operation.这个不是太明白,特别是那个may,是不是用异步WriteFile就一定反回的是FALSE,然后等待event被set才算文件写完毕.还有,在同一个线程做异步写入操作时应该不需要等上一次写入操作完成吧,不然异步就没有意义了
      

  8.   

    异常调用返回值可能是True也可能是False,True就不用说了,当出现False的时候就要判断GetLastError是不是ERROR_IO_PENDING,如果是的话,也表示成功,其它表示失败,需要立即回收资源