免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2310 | 回复: 0
打印 上一主题 下一主题

[C] 使用O_DIRECT,读写数据不一致,请高手指教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-12 10:39 |只看该作者 |倒序浏览
使用O_DIRECT打开设备文件,结果写入的数据和读出的数据不一致,davinci的板子,linux内核版本 2.6.18,代码如下:


#define _GNU_SOURCE
#define _USE_GNU 1
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/times.h>
#include <time.h>

#define bufferSize 512
#define offset 512*10
#define CARDPATH "/dev/mmcblk0"
#define READ_SIZE 512

int main()
{

        int bResult,lseek_error;
        int i,j,p;
        char *data_buf, *data_out;
        unsigned short * lendata, len;
        long tryTimes = 0;
   int hDevice;
        void *buf,*buf1;

        //char filename[] = "/dev/sdd1";                                                                                                               
        
        if ((hDevice = open(CARDPATH, O_RDWR  | O_DIRECT)) < 0)
    {
                      printf("open error");
                return -1;
    }        

        printf("SD DEVICE OPEN SUCCESS!\r\n" );

        buf = (void *)memalign(0x1000, READ_SIZE);
        data_buf=(char *)buf;
        buf1 = (void *)memalign(0x1000, READ_SIZE);
        data_out=(char *)buf1;
        memset( data_buf, 0, 512 );                                                                                                                                                               
        memset( data_out, 0, 512 );

        for(i=0; i<100; i++)
        {

                memset(data_buf,0,512);        

                //printf("\n");
                //printf("****************************************\n");
                for(p=0; p<512; p++)
                {
                        data_buf[p] = rand() % 256;
                        /*printf("%2.2x ", data_buf[p]);
                        if((p+1) % 16 == 0)
                                printf("\n");*/
                }

        
   //lseek( hDevice,-512*2,SEEK_END);
        lseek( hDevice,512*520,SEEK_SET);
        bResult = write(hDevice, data_buf, 512);
        
        sync();/*同步磁盘操作*/
        
           if( bResult == -1 )  
        {   
                //lstError = GetLastError( );
                return -1;   
        }

        sleep(1);

        //lseek_error=lseek( hDevice,-512*2,SEEK_END);        
        lseek_error=lseek( hDevice,512*520,SEEK_SET);
        if(lseek_error==-1)
        {
                        printf("\n lseek error!\n");
        }

        read(hDevice,data_out,512);

        if(memcmp(data_buf, data_out, 512))
        {
                printf("error %d\n", i);
                /*for(p=0; p<512; p++)
                {
                        printf("%2.2x ", data_out[p]);
                        if((p+1) % 16 == 0)
                                printf("\n");
                }*/
        }

        }

        return 0;
}

请高手指教!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP